A CSS Snap-Points based carousel (and lightweight polyfill)
MIT License [c] 2015 Filament Group, Inc
- jQuery or similar DOM library
- The toss() extension (optional, for smoothened anchoring and scroll snapping). Run
$ npm install
to download a copy to./node_modules/fg-toss/src/toss.js
View the Snapper demos:
- Basic example with minimal controls
- Next/Prev nav buttons + dot nav
- Multi-image with responsive breakpoints
- Include dependencies, plus the css and js files in the src dir.
- Use the markup pattern below.
<div class="snapper">
<div class="snapper_pane">
<div class="snapper_items">
<div class="snapper_item" id="img-a">
<img src="a-image.jpg" alt="">
</div>
<div class="snapper_item" id="img-b">
<img src="b-image.jpg" alt="">
</div>
<div class="snapper_item" id="img-c">
<img src="c-image.jpg" alt="">
</div>
<div class="snapper_item" id="img-d">
<img src="d-image.jpg" alt="">
</div>
</div>
</div>
</div>
- Trigger an "enhance" event on a parent of the markup to initialize. You might do this on domready, as shown below:
$( function(){
$( document ).trigger( "enhance" );
});
To add thumbnail or graphic navigation to the carousel, you can append the following markup to the end of the snapper div (substituting your own styles, images, and hrefs to correspond to the IDs of their associated slides):
<div class="snapper_nav">
<a href="#img-a"><img src="a-thumb.jpg" alt=""></a>
<a href="#img-b"><img src="b-thumb.jpg" alt=""></a>
<a href="#img-c"><img src="c-thumb.jpg" alt=""></a>
<a href="#img-d"><img src="d-thumb.jpg" alt=""></a>
</div>
To add next and previous links that persist state, you can add a data-snapper-nextprev
attribute to the snapper div.
<div class="snapper" data-snapper-nextprev>
...
</div>
To make snapper respect the CSS-specified widths of items in a slider, you can add the data-snapper-setwidths
attribute to the snapper div. This allows you to show more than one image at a time, and even adjust that as viewport width allows. See example: http://master.origin.snapper.fgview.com/demo/breakpoints.html
<div class="snapper" data-snapper-setwidths>
...
</div>
In some browsers, native scrollbar handles can be pretty ugly across the bottom of the carousel. As long as you're using thumbnails or some means of advancing the slides, you can wrap the snapper_pane
in a div
with a class of snapper_pane_crop
and it'll hide the scrollbar from sight.
<div class="snapper">
<div class="snapper_pane_crop">
<div class="snapper_pane">
<div class="snapper_items">
<div class="snapper_item" id="img-a">
<img src="a-image.jpg" alt="">
</div>
<div class="snapper_item" id="img-b">
...
</div>
<div class="snapper_item" id="img-c">
...
</div>
<div class="snapper_item" id="img-d">
...
</div>
</div>
</div>
</div>
<div class="snapper_nav">
<a href="#img-a"><img src="a-thumb.jpg" alt=""></a>
<a href="#img-b"><img src="b-thumb.jpg" alt=""></a>
<a href="#img-c"><img src="c-thumb.jpg" alt=""></a>
<a href="#img-d"><img src="d-thumb.jpg" alt=""></a>
</div>
</div>
CSS Snap Points support can be found here: CSS Snap Points on Caniuse.com This plugin is tested to work broadly across modern browsers, and as long as you use thumbnail navigation, it will even work in browsers that do not support overflow scrolling properly (such as Android 2.x browser).