I have a quick fiddle here . I want it to be able to endlessly scroll left or right (carousel style) and just repeat the elements (ie, “Roll off” the right edge and reappear on the left and vice versa). I can fix where I am in the scroll, but I'm not sure what is best done after that. It seems to me that there is a very simple trick that is before I go out of the way to dynamically move elements.
CSS
#main {
overflow-x:scroll;
overflow-y:hidden;
white-space:nowrap;
}
HTML
<div id="main">
<img src="http://dummyimage.com/150x100/aaa/00f">
<img src="http://dummyimage.com/150x100/000/fff">
<img src="http://dummyimage.com/150x100/000/fff">
<img src="http://dummyimage.com/150x100/000/fff">
<img src="http://dummyimage.com/150x100/000/fff">
<img src="http://dummyimage.com/150x100/000/fff">
<img src="http://dummyimage.com/150x100/000/fff">
<img src="http://dummyimage.com/150x100/000/fff">
<img src="http://dummyimage.com/150x100/000/fff">
<img src="http://dummyimage.com/150x100/ccc/f00">
</div>
Js
$('#main').scroll(function (event) {
var width = $(this)[0].scrollWidth - $(this).width();
console.log('location: ' + $(this).scrollLeft() + ' out of ' + width);
});
source
share