Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

97 lines (60 sloc) 3.805 kb

Swipe 1.0

Swipe is a lightweight mobile slider with 1-to-1 touch movement. For more info read the blog post.

Guardian fork

We've forked this because we've added in some methods: enable() and disable(). These allow us to disable swiping while the animation is happening and enable it again once it's finished. The reason we needed this was because our use case (image galleries) needed their callback function to always fire when swiping completed, but we found that very fast swiping would cause this to fail and various bugs arose. This fix means that it should be impossible to swipe continuously without triggering the callback.

Demo

Visit here on a touch-enabled mobile device swipejs.com

Usage

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>
    <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. (more on the display:block/none reasoning below).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.

Config Options

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)

Example

window.mySwipe = new Swipe(document.getElementById('slider'), {
    startSlide: 2,
    speed: 400,
    auto: 3000,
    callback: function(event, index, elem) {

      // do something cool

    }
});

Swipe API

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 milliseconds)

Requirements

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.

Let's Make It Better

I would love to hear more about how to improve Swipe. Play with it and let me know how you use and please fork away. If you have any questions, contact me on Twitter or GitHub.

License

Swipe mobile slider is © 2011 Brad Birdsall and is licensed under the terms of GPL & MIT licenses.

Jump to Line
Something went wrong with that request. Please try again.