Skip to content

Instantly share code, notes, and snippets.

@timmarinin
Created May 26, 2014 11:59
Show Gist options
  • Save timmarinin/82943bccffd440924649 to your computer and use it in GitHub Desktop.
Save timmarinin/82943bccffd440924649 to your computer and use it in GitHub Desktop.
infinity horizontal scroller
infinityScroller = function() {
this.el = $(/*wrapper-class*/);
this.blocks = function() { return this.el.children(/*item class*/) }; //function to prevent jquery snapshot
this.maxShift = this.blocks.length - 3; //only 3 at screen
this.indent(this.indent() - this.shiftPx); //make last first and move off screen
this.blocks().last().detach().insertBefore(this.blocks().first());
};
feedTagsPromo.prototype = {
shiftNum: 0, //counter for left displayed item
shiftPx: 282, //item's width with all margins
//shorthand func for text-indent
indent: function(value) {
if (typeof value === 'number') return this.el.css({'text-indent': value});
else return parseInt(this.el.css('text-indent'),10);
},
//shorthand animate. direction is 1 (right) or -1 (left)
move: function(direction) {
this.el.animate({
'text-indent': this.indent() - (direction * this.shiftPx)
});
},
//move to next (slide all to left)
next: function() {
if(!this.el.is(':animated')) {
if (this.shiftNum >= this.maxShift) {
this.indent(this.indent() + this.shiftPx);
this.blocks().first().detach().insertAfter(this.blocks().last());
}
this.shiftNum++;
this.move(1);
if(this.shiftNum >= this.blocks().length) this.shiftNum = 0 + (this.shiftNum - this.blocks().length);
console.log(this.shiftNum);
}
},
//move to previous (slide all to right)
prev: function() {
if(!this.el.is(':animated')) {
if (this.shiftNum < (this.blocks().length + 1)) {
this.indent(this.indent() - this.shiftPx);
this.blocks().last().detach().insertBefore(this.blocks().first());
}
this.shiftNum--;
this.move(-1);
if(this.shiftNum < 0) this.shiftNum = this.blocks().length - 1;
}
}
}
infinityScroller = new infinityScroller();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment