Skip to content

Commit

Permalink
Merge pull request thebird#246 from alicelieutier/master
Browse files Browse the repository at this point in the history
added an option "circular"
  • Loading branch information
thebird committed Apr 11, 2013
2 parents b0f8e38 + 45d63f6 commit c4ff1b4
Showing 1 changed file with 112 additions and 37 deletions.
149 changes: 112 additions & 37 deletions swipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function Swipe(container, options) {
// quit if no root element
if (!container) return;
var element = container.children[0];
var slides, slidePos, width;
var slides, slidePos, width, length;
options = options || {};
var index = parseInt(options.startSlide, 10) || 0;
var speed = options.speed || 300;
Expand All @@ -38,6 +38,17 @@ function Swipe(container, options) {

// cache slides
slides = element.children;
length = slides.length;

// set continuous to false if only one slide
if (slides.length < 2) options.continuous = false;

//special case if two slides
if (browser.transitions && options.continuous && slides.length < 3) {
element.appendChild(slides[0].cloneNode(true));
element.appendChild(element.children[1].cloneNode(true));
slides = element.children;
}

// create an array to store current positions of each slide
slidePos = new Array(slides.length);
Expand All @@ -63,6 +74,12 @@ function Swipe(container, options) {

}

// reposition elements before and after index
if (options.continuous && browser.transitions) {
move(circle(index-1), -width, 0);
move(circle(index+1), width, 0);
}

if (!browser.transitions) element.style.left = (index * -width) + 'px';

container.style.visibility = 'visible';
Expand All @@ -71,15 +88,22 @@ function Swipe(container, options) {

function prev() {

if (index) slide(index-1);
else if (options.continuous) slide(slides.length-1);
if (options.continuous) slide(index-1);
else if (index) slide(index-1);

}

function next() {

if (index < slides.length - 1) slide(index+1);
else if (options.continuous) slide(0);
if (options.continuous) slide(index+1);
else if (index < slides.length - 1) slide(index+1);

}

function circle(index) {

// a simple positive modulo using slides.length
return (slides.length + (index % slides.length)) % slides.length;

}

Expand All @@ -90,24 +114,40 @@ function Swipe(container, options) {

if (browser.transitions) {

var direction = Math.abs(index-to) / (index-to); // 1: backward, -1: forward

// get the actual position of the slide
if (options.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;
var direction = Math.abs(index-to) / (index-to); // 1:right -1:left

while (diff--) move((to > index ? to : index) - diff - 1, width * direction, 0);
// 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);

to = circle(to);

move(index, width * direction, slideSpeed || speed);
move(to, 0, slideSpeed || speed);

} else {

if (options.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]));

}

function move(index, dist, speed) {
Expand Down Expand Up @@ -141,7 +181,7 @@ function Swipe(container, options) {

// if not an animation, just reposition
if (!speed) {

element.style.left = to + 'px';
return;

Expand Down Expand Up @@ -272,19 +312,28 @@ function Swipe(container, options) {
stop();

// increase resistance if first or last slide
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

// translate 1:1
translate(index-1, delta.x + slidePos[index-1], 0);
translate(index, delta.x + slidePos[index], 0);
translate(index+1, delta.x + slidePos[index+1], 0);
if (options.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

// translate 1:1
translate(index-1, delta.x + slidePos[index-1], 0);
translate(index, delta.x + slidePos[index], 0);
translate(index+1, delta.x + slidePos[index+1], 0);
}

}

Expand All @@ -304,6 +353,8 @@ function Swipe(container, options) {
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 (options.continuous) isPastBounds = false;

// determine direction of swipe (true:right, false:left)
var direction = delta.x < 0;
Expand All @@ -315,27 +366,51 @@ function Swipe(container, options) {

if (direction) {

move(index-1, -width, 0);
move(index, slidePos[index]-width, speed);
move(index+1, slidePos[index+1]-width, speed);
index += 1;
if (options.continuous) { // we need to get the next in this direction in place

move(circle(index-1), -width, 0);
move(circle(index+2), width, 0);

} else {
move(index-1, -width, 0);
}

move(index, slidePos[index]-width, speed);
move(circle(index+1), slidePos[circle(index+1)]-width, speed);
index = circle(index+1);

} else {
if (options.continuous) { // we need to get the next in this direction in place

move(circle(index+1), width, 0);
move(circle(index-2), -width, 0);

} else {
move(index+1, width, 0);
}

move(index+1, width, 0);
move(index, slidePos[index]+width, speed);
move(index-1, slidePos[index-1]+width, speed);
index += -1;
move(circle(index-1), slidePos[circle(index-1)]+width, speed);
index = circle(index-1);

}

options.callback && options.callback(index, slides[index]);

} else {

move(index-1, -width, speed);
move(index, 0, speed);
move(index+1, width, speed);
if (options.continuous) {

move(circle(index-1), -width, speed);
move(index, 0, speed);
move(circle(index+1), width, speed);

} else {

move(index-1, -width, speed);
move(index, 0, speed);
move(index+1, width, speed);
}

}

Expand Down Expand Up @@ -430,7 +505,7 @@ function Swipe(container, options) {
getNumSlides: function() {

// return total number of slides
return slides.length;
return length;
},
kill: function() {

Expand Down

0 comments on commit c4ff1b4

Please sign in to comment.