// start auto slideshow if applicable if (delay) begin();
// add event listeners if (browser.addEventListener) { // set touchstart event on element if (browser.touch) { element.addEventListener('touchstart', events, false); element.addEventListener('touchforcechange', function() {}, false); }
// set resize event on window window.addEventListener('resize', events, false); } else { window.onresize = function() { setup(); }; // to play nice with old IE }
// reposition elements before and after index if (continuous && browser.transitions) { move(circle(index - 1), -width, 0); move(circle(index + 1), width, 0); }
if (!browser.transitions) element.style.left = index * -width + widthOfSiblingSlidePreview + 'px';
function slide(to, slideSpeed) { // do nothing if already on requested slide if (index == to) return;
if (browser.transitions) { var direction = Math.abs(index - to) / (index - to); // 1: backward, -1: forward
// get the actual position of the slide if (continuous) { var natural_direction = direction; direction = -slidePos[circle(to)] / width;
// if going forward but to < index, use to = slides.length + to // if going backward but to > index, use to = -slides.length + to if (direction !== natural_direction) to = -direction * slides.length + to; }
var diff = Math.abs(index - to) - 1;
// move all the slides between index and to in the right direction while (diff--) move( circle((to > index ? to : index) - diff - 1), width * direction, 0 );
if (continuous) move(circle(to - direction), -(width * direction), 0); // we need to get the next in place } else { to = circle(to); animate(index * -width, to * -width, slideSpeed || speed); //no fallback for a circular continuous if the browser does not accept transitions }
index = to; offloadFn(options.callback && options.callback(index, slides[index])); }
move: function(event) { // ensure swiping with one touch and not pinching if (event.touches.length > 1 || (event.scale && event.scale !== 1)) return;
if (options.disableScroll) return;
var touches = event.touches[0];
// measure change in x and y delta = { x: touches.pageX - start.x, y: touches.pageY - start.y };
// determine if scrolling test has run - one time test if (typeof isScrolling == 'undefined') { isScrolling = !!( isScrolling || Math.abs(delta.x) < Math.abs(delta.y) ); }
// if user is not trying to scroll vertically if (!isScrolling) { // prevent native scrolling event.preventDefault();
// stop slideshow stop();
// increase resistance if first or last slide if (continuous) { // we don't add resistance at the end
translate( circle(index - 1), delta.x + slidePos[circle(index - 1)], 0 ); translate(index, delta.x + slidePos[index], 0); translate( circle(index + 1), delta.x + slidePos[circle(index + 1)], 0 ); } else { delta.x = delta.x / ((!index && delta.x > 0) || // if first slide and sliding left (index == slides.length - 1 && // or if last slide and sliding right delta.x < 0) // and if sliding at all ? Math.abs(delta.x) / width + 1 // determine resistance level : 1); // no resistance if false
// determine if slide attempt triggers next/prev slide var isValidSlide = (Number(duration) < 250 && // if slide duration is less than 250ms Math.abs(delta.x) > 20) || // and if slide amt is greater than 20px Math.abs(delta.x) > width / 2; // or if slide amt is greater than half the width
// determine if slide attempt is past start and end var isPastBounds = (!index && delta.x > 0) || // if first slide and slide amt is greater than 0 (index == slides.length - 1 && delta.x < 0); // or if last slide and slide amt is less than 0
if (continuous) isPastBounds = false;
// determine direction of swipe (true:right, false:left) var direction = delta.x < 0;
// if not scrolling vertically if (!isScrolling) { if (isValidSlide && !isPastBounds) { if (direction) { if (continuous) { // we need to get the next in this direction in place
move(index, slidePos[index] - width, speed); move( circle(index + 1), slidePos[circle(index + 1)] - width, speed ); index = circle(index + 1); } else { if (continuous) { // we need to get the next in this direction in place