Created
June 26, 2013 04:03
-
-
Save dongyuwei/5864686 to your computer and use it in GitHub Desktop.
infinite loop carousel(vertical or horizontal)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> | |
</script> | |
<meta charset='utf-8' /> | |
<title> | |
infinite loop carousel(horizontal) | |
</title> | |
<style> | |
.infinite-carousel .list{ | |
width:1400px; | |
height:200px; | |
overflow:visible; | |
} | |
.infinite-carousel .viewport{ | |
width:410px; | |
height:200px; | |
overflow:hidden; | |
} | |
.item{ | |
width:200px; | |
height:200px; | |
text-align: center; | |
vertical-align: middle; | |
line-height: 200px; | |
margin-right:10px; | |
float: left; | |
/*display: inline-block;*/ | |
} | |
.r{ | |
background-color: red; | |
} | |
.g{ | |
background-color: #cf5; | |
} | |
.b{ | |
background-color: blue; | |
} | |
</style> | |
</head> | |
<body> | |
<div class='infinite-carousel'> | |
<button class='pre'>pre</button> | |
<div class="viewport"> | |
<div class='list'> | |
<div class='item g'> | |
1 | |
</div> | |
<div class='item r'> | |
2 | |
</div> | |
<div class='item g'> | |
3 | |
</div> | |
<div class='item b'> | |
4 | |
</div> | |
<div class='item r'> | |
5 | |
</div> | |
<div class='item g'> | |
6 | |
</div> | |
<div class='item b'> | |
7 | |
</div> | |
</div> | |
</div> | |
<button class='next'>next</button> | |
</div> | |
<script src='infiniteCarousel.js'></script> | |
<script> | |
$('.infinite-carousel').infiniteCarousel({ | |
itemsPerMove : 2, | |
duration : 500 | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* infinite loop carousel | |
* @author [email protected] | |
* @param {Object} config | |
* @return {Object} this | |
*/ | |
$.fn.infiniteCarousel = function(config){ | |
config = $.extend({ | |
itemsPerMove : 2, | |
duration : 1000, | |
vertical : false | |
},config); | |
var viewportEl = this.find('.viewport'), listEl = viewportEl.find('.list'); | |
var first = listEl.children(":first"), last = listEl.children(":last"); | |
var distance, prevProp, nextProp; | |
if(config.vertical){ | |
distance = Math.max(first.outerHeight(true),last.outerHeight(true)) * config.itemsPerMove; | |
prevProp = { 'scrollTop' : "-=" + distance }; | |
nextProp = { 'scrollTop' : distance }; | |
}else{ | |
distance = Math.max(first.outerWidth(true),last.outerWidth(true)) * config.itemsPerMove; | |
prevProp = { 'scrollLeft' : "-=" + distance }; | |
nextProp = { 'scrollLeft' : '+=' + distance }; | |
} | |
function move(config) { | |
if (config.dir === 'next') { | |
viewportEl.stop().animate(nextProp,{ | |
duration : config.duration, | |
complete : function() { | |
config.vertical ? viewportEl.scrollTop(0) : viewportEl.scrollLeft(0); | |
repeatRun(function(){ | |
listEl.children(":last").after(listEl.children(":first")); | |
},config.itemsPerMove); | |
} | |
}); | |
} | |
if (config.dir === 'pre') { | |
for(var i = 0; i < config.itemsPerMove; i++){ | |
listEl.prepend(listEl.children(":last")); | |
} | |
viewportEl[config.vertical ? 'scrollTop' : 'scrollLeft'](distance) | |
.stop().animate(prevProp, { | |
duration : config.duration | |
}); | |
} | |
} | |
function repeatRun(func,times){ | |
for(var i = 0; i < times; i++){ | |
func(); | |
} | |
} | |
this.find('.pre').click(function() { | |
move($.extend(config,{ | |
dir: "pre" | |
})); | |
}); | |
this.find('.next').click(function() { | |
move($.extend(config,{ | |
dir: "next" | |
})); | |
}); | |
return this; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> | |
</script> | |
<meta charset='utf-8' /> | |
<title> | |
infinite loop carousel(vertical) | |
</title> | |
<style> | |
.infinite-carousel .list{ | |
width:200px; | |
height:1400px; | |
overflow:visible; | |
} | |
.infinite-carousel .viewport{ | |
width:200px; | |
height:410px; | |
overflow:hidden; | |
} | |
.item{ | |
width:200px; | |
height:200px; | |
text-align: center; | |
vertical-align: middle; | |
line-height: 200px; | |
float: left; | |
margin-bottom: 10px; | |
} | |
.r{ | |
background-color: red; | |
} | |
.g{ | |
background-color: #cf5; | |
} | |
.b{ | |
background-color: blue; | |
} | |
</style> | |
</head> | |
<body> | |
<div class='infinite-carousel'> | |
<button class='pre'>pre</button> | |
<div class="viewport"> | |
<div class='list'> | |
<div class='item g'> | |
1 | |
</div> | |
<div class='item r'> | |
2 | |
</div> | |
<div class='item g'> | |
3 | |
</div> | |
<div class='item b'> | |
4 | |
</div> | |
<div class='item r'> | |
5 | |
</div> | |
<div class='item g'> | |
6 | |
</div> | |
<div class='item b'> | |
7 | |
</div> | |
</div> | |
</div> | |
<button class='next'>next</button> | |
</div> | |
<script src='infiniteCarousel.js'></script> | |
<script> | |
$('.infinite-carousel').infiniteCarousel({ | |
itemsPerMove : 2, | |
duration : 500, | |
vertical : true | |
}); | |
</script> | |
</body> | |
</html> |
Thanks a ton!
hi @YayaBln
I just modified the above code to stop infinte loop , please refer the code below,
/**
* infinite loop carousel
* @author [email protected]
* @param {Object} config
* @return {Object} this
*/
$.fn.infiniteCarousel = function (config) {
config = $.extend({
itemsPerMove: 2,
duration: 1000,
vertical: false
}, config);
var isLast = false,
isFirst = true;
var divElement = this;
var viewportEl = this.find('.viewport'),
listEl = viewportEl.find('.list');
var first = listEl.children(":first"),
last = listEl.children(":last"),
lastButOne = listEl.children(":nth-last-child(2)");
disablePrevious(divElement);
var distance, prevProp, nextProp;
if (config.vertical) {
distance = Math.max(first.outerHeight(true), last.outerHeight(true)) * config.itemsPerMove;
prevProp = {
'scrollTop': "-=" + distance
};
nextProp = {
'scrollTop': distance
};
} else {
distance = Math.max(first.outerWidth(true), last.outerWidth(true)) * config.itemsPerMove;
prevProp = {
'scrollLeft': "-=" + distance
};
nextProp = {
'scrollLeft': '+=' + distance
};
}
function move(config) {
if (config.dir === 'next') {
viewportEl.stop().animate(nextProp, {
duration: config.duration,
complete: function () {
config.vertical ? viewportEl.scrollTop(0) : viewportEl.scrollLeft(0);
repeatRun(function () {
var lastEle = listEl.children(":last");
var firstEle = listEl.children(":first");
if (!isLast) {
lastEle.after(firstEle);
var afterChangeFirstEle = listEl.children(":first");
if (afterChangeFirstEle[0] == lastButOne[0]) {
isLast = true;
disableNext(divElement);
enablePrevious(divElement);
} else {
isFirst = false;
enableNext(divElement);
enablePrevious(divElement);
}
}
}, config.itemsPerMove);
}
});
}
if (config.dir === 'pre') {
for (var i = 0; i < config.itemsPerMove; i++) {
var lastEle = listEl.children(":last");
if (!isFirst) {
listEl.prepend(listEl.children(":last"));
var afterChangelastEle = listEl.children(":last")
if (afterChangelastEle[0] == last[0]) {
isFirst = true;
enableNext(divElement);
disablePrevious(divElement);
} else {
isLast = false;
enableNext(divElement);
enablePrevious(divElement);
}
}
}
viewportEl[config.vertical ? 'scrollTop' : 'scrollLeft'](distance)
.stop().animate(prevProp, {
duration: config.duration
});
}
}
function repeatRun(func, times) {
for (var i = 0; i < times; i++) {
func();
}
}
function disablePrevious(ele) {
var previousBtn = ele.find('.pre');
previousBtn.prop('disabled', true);
};
function enablePrevious(ele) {
ele.find('.pre').prop('disabled', false);
};
function disableNext(ele) {
ele.find('.next').prop('disabled', true);
};
function enableNext(ele) {
ele.find('.next').prop('disabled', false);
};
this.find('.pre').click(function () {
move($.extend(config, {
dir: "pre"
}));
});
this.find('.next').click(function () {
move($.extend(config, {
dir: "next"
}));
});
return this;
};
Hi, thanks for this! How would I enable scrolling vertically? .viewport and .infinite-carousel are not responding to overflow-y:scroll.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Thanks for this super & clear code. I was just wondering if it's possible to stop the infinite loop? If yes, what's your recommandation?
Regards,
Yannick