Last active
January 22, 2018 11:59
-
-
Save porqz/41357120c7e7130d58b5e169fddc1bca to your computer and use it in GitHub Desktop.
Simple slider
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
var Slider = (function($) { | |
function Slider(options) { | |
this.viewport = $(options.viewport); | |
this.leftLink = $(options.leftLink); | |
this.rightLink = $(options.rightLink); | |
this.slides = $(options.slides); | |
this.init(); | |
} | |
Slider.prototype = { | |
init: function() { | |
this.currentSlideIndex = 0; | |
this.initUI(); | |
}, | |
initUI: function() { | |
var slider = this; | |
slider.leftLink.bind('click', function(e) { | |
slider.previous(); | |
e.preventDefault(); | |
}); | |
slider.rightLink.bind('click', function(e) { | |
slider.next(); | |
e.preventDefault(); | |
}); | |
}, | |
goToSlide: function(index) { | |
if (index >= 0 && index < this.slides.length) { | |
this.currentSlideIndex = index; | |
} | |
this.updateSlides(); | |
}, | |
previous: function() { | |
this.currentSlideIndex = (this.currentSlideIndex > 0) ? | |
this.currentSlideIndex - 1 : | |
this.slides.length - 1; | |
this.updateSlides(); | |
}, | |
next: function() { | |
this.currentSlideIndex = (this.currentSlideIndex < this.slides.length - 1) ? | |
this.currentSlideIndex + 1 : | |
0; | |
this.updateSlides(); | |
}, | |
updateSlides: function() { | |
var offset = 0; | |
this.slides.eq(this.currentSlideIndex).prevAll().each(function() { | |
var slide = $(this); | |
offset = offset + slide.outerWidth(); | |
}); | |
this.slides.css('left', -offset); | |
} | |
}; | |
return Slider; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment