Swipe is a lightweight mobile slider with 1:1 touch movement, resistant bounds, scroll prevention, and completely library agnostic.
Swipe brings content sliding to the mobile web to preserve space and allow for new types of interaction. Many mobile web sliders currently exist but few embody all the rules a slider like this should follow.
1:1 movement, as described above, is about tracking the position of the touch and moving the content exactly how the touch interacts. This interaction is very common in native apps.
When Swipe is at the left-most position, sliding any more left will increase the resistance to slide, making it obvious that you have reached the end. This detection/resistance occurs at both left and right bounds.
When a user rotates their device (in turn resizing the browser window), the slider resets to make sure the sliding elements are the right size. This is only necessary for sliders without declared widths.
Swipe allows you to set a width to the container, which designates the width of each slide element. This is important if you do not want a full width slider (the default).
Scroll prevention is one of the most overlooked pieces of mobile sliders. Swipe detects if you’re trying to scroll down the page or slide content left to right.
Swipe doesn’t rely on any library. Trust me this is a good thing. All you have to do is pass in the container element, set some parameters, and BOOM goes the dynamite– you're all set. You may choose to pass the element to Swipe with a library, but it’s not necessary.
Swipe only needs to follow a simple pattern. Here is an example:
<div id='slider'>
<ul>
<li style='display:block'></li>
<li style='display:none'></li>
<li style='display:none'></li>
</ul>
</div>
Above is the initial required structure– a series of elements wrapped in two containers. An unordered list makes sense here, but this can be any combination of elements that has the same structure. Place any content you want within the items.
The containing div will need to be passed to a new Swipe object like so:
window.mySwipe = new Swipe(
document.getElementById('slider')
);
I always place this at the bottom of the page, externally, to verify the page is ready.
Swipe can take an optional second parameter– an object of key/value settings:
startSlide Integer (default:0) index position Swipe should start at
speed Integer (default:300) speed of prev and next transitions in milliseconds
auto Integer begin with auto slideshow (time in milliseconds between slides)
callback Function runs at the end of any slide change. (effective for updating position indicators/counters)
Swipe exposes a few functions that can be useful for script control of your slider.
prev() // slide to prev
next() // slide to next
getPos() // returns current
slide index position
slide(index, duration)
// slide to set index position
(duration: speed of transition
in ms)
Swipe requires a device that supports CSS transforms and works best with devices that support touch. Both of these are not required for the code to run since Swipe does not include any feature detection in the core code. This decision was made due to the fact that all mobile web development should already have some sort of feature detection built into the page. I recommend using a custom build of Modernizr, don't recreate the wheel.
Sample use with Modernizr:
if ( Modernizr.csstransforms ) {
window.mySwipe = new Swipe(
document.getElementById('slider')
);
}
This is why I set all elements but the first list item to display:none– if the device doesn't pass the feature tests then it will fallback to displaying only the first item.
Swipe mobile slider is © 2011 Brad Birdsall and is licensed under the terms of GPL & MIT licenses.