将样式表包含在您的 head:
<link rel="stylesheet" href="BeerSlider.css">
构建您的标记,如下例所示。确保保留类名称,如示例中所示。该data-beer-label属性定义在滑块的上角中显示的标签的文本。如果您不需要标签,请将其取下或留空。
<div id="beer-slider" class="beer-slider" data-beer-label="before"> <img src="man-hold-beer.jpg" alt="Original - Man holding beer"> <div class="beer-reveal" data-beer-label="after"> <img src="man-hold-beer-after.jpg" alt="Processed - Man holding beer"> </div> </div>
在结束body标记之前包含脚本:
<script src="BeerSlider.js"></script> <script> var slider = new BeerSlider( document.getElementById( "beer-slider" ) ); </script>
您还可以使用start0到100之间的参数将起始位置设置在除中间之外的其他位置。
<script> new BeerSlider( document.getElementById( "beer-slider" ), { start: 25 } ); </script>
与jQuery或Zepto一起使用
如果您在项目中使用jQuery或Zepto并在页面上有一些前后滑块,则可以执行以下操作:
<script> $.fn.BeerSlider = function ( options ) { options = options || {}; return this.each(function() { new BeerSlider(this, options); }); }; $('.beer-slider').BeerSlider({start: 25}); </script>
要么:
<script> $.fn.BeerSlider = function ( options ) { options = options || {}; return this.each(function() { new BeerSlider(this, options); }); }; $('.beer-slider').each( (function( index, el ) { $(el).BeerSlider({start: $(el).data('beer-start')}) }); </script>
有:
<div id="beer-slider" class="beer-slider" data-beer-label="before" data-beer-start="25"> <img src="man-hold-beer.jpg" alt="Original - Man holding beer"> <div class="beer-reveal" data-beer-label="after"> <img src="man-hold-beer-after.jpg" alt="Processed - Man holding beer"> </div> </div> <div id="beer-slider2" class="beer-slider" data-beer-label="before" data-beer-start="75"> <img src="man-hold-beer.jpg" alt="Original - Man holding beer"> <div class="beer-reveal" data-beer-label="after"> <img src="man-hold-beer-after.jpg" alt="Processed - Man holding beer"> </div> </div>