简体   繁体   English

jQuery自定义滑块上一个下一个导航跳过多个步骤

[英]jQuery custom slider previous next navigation jumping multiple steps

I am a jQuery (and general programming) learner and rather than using a plug-in I am trying to build my own Image slider/cycle, both to keep the code small, and to aid in learning. 我是jQuery(和通用程序设计)学习者,而不是使用插件,而是尝试构建自己的“图像”滑块/循环,以保持代码小巧并有助于学习。

My function cycles through the li items adding a '.show' class, then after a delay removes the class and adds to the next slide. 我的函数在li项之间循环,添加了一个'.show'类,然后在延迟后删除该类并添加到下一张幻灯片中。 This seems to work fine. 这似乎很好。

I have been struggling for a few days to add navigation which will either move previous or next and stop the timer. 我一直在努力添加导航,这将使上一个或下一个移动并停止计时器。

As it stands, if I click the navigation immediately as the script starts the navigation will work as expected, but once the automatic function to show another slide has started the navigation will jump multiple steps and I have no idea why this is. 就目前而言,如果我在脚本启动时立即单击导航,导航将按预期工作,但是一旦显示另一张幻灯片的自动功能启动,导航将跳多个步骤,我不知道为什么会这样。 I imaging somehow jQuery is caching the previous divs which have had a '.show' class perhaps? '.show' jQuery是否正在缓存以前具有'.show'类的div?

I have simplified my code and presentation to illustrate this working up in a CodePen: codepen.io/MattyBalaam/pen/vuhyJ 我简化了我的代码和演示,以说明在CodePen中的工作情况:codepen.io/MattyBalaam/pen/vuhyJ

Here is the complete script: 这是完整的脚本:

function gallery(galleryContainer) {

    $.fn.nextOrFirst = function(selector) {
      var next = this.next(selector);
      return (next.length) ? next : this.prevAll(selector).last();  
    };


    $.fn.prevOrLast = function(selector) {
        var prev = this.prev(selector);
        return (prev.length) ? prev : this.nextAll(selector).last();

    };

    galleryContainer.parent().prepend('<div class="previous">previous</div><div class="next">next</div>');

    var crossFade = function (){

        var slideTimeout;

        function slideWait() {
            galleryContainer.find('.show').removeClass('show').nextOrFirst().addClass('show');
            slideTimeout = setTimeout(crossFade, 800);
         }   

    function checkForClicks() {

        $('div.previous').on('click', function(){
            galleryContainer.find('.show').removeClass('show').prevOrLast().addClass('show');
            window.clearTimeout(slideTimeout);
        });

        $('div.next').on('click', function(){
            galleryContainer.find('.show').removeClass('show').nextOrFirst().addClass('show');
            window.clearTimeout(slideTimeout);
         });
    }

    checkForClicks();
    slideWait();

};

galleryContainer.children(':first-child').addClass('show');
setTimeout(crossFade, 800);
}

gallery($('ul'));

The problem is, that you are calling function checkForClicks multiple times (in each animation iteration) and the event listener is added to buttons multiple times, so on each click you move forwards/backwards not just once, but once for each animation step, that was already displayed. 问题是,您多次调用函数checkForClicks(在每个动画迭代中),并且事件侦听器被多次添加到按钮,因此,每次单击时,您不仅向前/向后移动一次,而且对于每个动画步骤都向前/向后移动一次,已经显示。 Move the checkForClicks outside the crossFade function and it will be ok. 将checkForClicks移动到crossFade函数之外,就可以了。

see the code: http://codepen.io/anon/pen/ptkea 看到代码: http : //codepen.io/anon/pen/ptkea

working code: 工作代码:

function gallery(galleryContainer) {

$.fn.nextOrFirst = function(selector) {
    var next = this.next(selector);
    return (next.length) ? next : this.prevAll(selector).last();
};

$.fn.prevOrLast = function(selector) {
    var prev = this.prev(selector);
    return (prev.length) ? prev : this.nextAll(selector).last();
};

    galleryContainer.parent().prepend('<div class="previous">previous</div><div class="next">next</div>');

    var slideTimeout;
    var crossFade = function (){
        function slideWait() {
              galleryContainer.find('.show').removeClass('show').nextOrFirst().addClass('show');
              slideTimeout = setTimeout(crossFade, 800);
        }   
        slideWait();
    };

    galleryContainer.children(':first-child').addClass('show');
    setTimeout(crossFade, 800);

    function initButtonEvents() {

            $('div.previous').on('click', function(){
                galleryContainer.find('.show').removeClass('show').prevOrLast().addClass('show');
            window.clearTimeout(slideTimeout);
            });

            $('div.next').on('click', function(){
                galleryContainer.find('.show').removeClass('show').nextOrFirst().addClass('show');
            window.clearTimeout(slideTimeout);

            });
    }
    initButtonEvents();

}

gallery($('ul'));

see my script: http://codepen.io/anon/pen/asvGc 看到我的脚本: http : //codepen.io/anon/pen/asvGc

If you declare the var's outside the functions it works fine. 如果在函数之外声明var,则可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM