Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($){
- $.fn.mySlider = function(options) {
- let settings = $.extend({
- 'speed': 2000,
- 'itemSelector': '.slider__item',
- 'dotSelector': '.slider__dot',
- 'itemActiveClass': 'slider__item_active',
- 'dotActiveClass': 'slider__dot_active',
- 'nextBtnSelector': '.slider__arrow-right',
- 'prevBtnSelector': '.slider__arrow-left'
- }, options);
- return this.each(function() {
- let items = $(this).find(settings.itemSelector);
- let dots = $(this).find(settings.dotSelector);
- let cntSlides = items.length;
- let nowSlide = -1;
- function hidden(nowSlide){
- items.eq(nowSlide).removeClass(settings.itemActiveClass);
- dots.eq(nowSlide).removeClass(settings.dotActiveClass);
- }
- function show(nowSlide){
- items.eq(nowSlide).addClass(settings.itemActiveClass);
- dots.eq(nowSlide).addClass(settings.dotActiveClass);
- }
- function goTo(newSlide){
- hidden(nowSlide);
- nowSlide = newSlide;
- show(nowSlide);
- }
- function next(){
- goTo(nowSlide + 1 < cntSlides ? nowSlide + 1 : 0);
- }
- function prev(){
- goTo(nowSlide - 1 < 0 ? cntSlides - 1 : nowSlide - 1);
- }
- show(0);
- let sliderIntervalId = setInterval(next, settings.speed);
- dots.click(function(){
- clearInterval(sliderIntervalId);
- goTo($(this).index());
- })
- $(this).find(settings.nextBtnSelector).click(() => {
- clearInterval(sliderIntervalId);
- next();
- });
- $(this).find(settings.prevBtnSelector).click(() => {
- clearInterval(sliderIntervalId);
- prev();
- });
- });
- };
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement