jquery如何绑定和解除绑定事件:轮播脚本修改

发布于 2025-01-02 23:50:29 字数 6840 浏览 1 评论 0原文

我有一个 jQuery Carousel 脚本,我想稍微修改它的行为。这是jsfiddle中的脚本和测试用例: http://jsfiddle.net/A4vHf/1/ 。下面是JS代码。

(function($) {

    $.fn.easyPaginate = function(options){

        var defaults = {                
            step: 4,
            delay: 100,
            numeric: true,
            nextprev: true,
            auto:false,
            pause:4000,
            clickstop:true,
            controls: 'pagination',
            current: 'current' 
        }; 

        var options = $.extend(defaults, options); 
        var step = options.step;
        var lower, upper;
        var children = $(this).children();
        var count = children.length;
        var obj, next, prev;        
        var page = 1;
        var timeout;
        var clicked = false;

        function show(){
            clearTimeout(timeout);
            lower = ((page-1) * step);
            upper = lower+step;
            $(children).each(function(i){
                var child = $(this);
                child.hide();
                if(i>=lower && i<upper){ setTimeout(function(){ child.fadeIn('fast') }, ( i-( Math.floor(i/step) * step) )*options.delay ); }
                if(options.nextprev){
                    if(upper >= count) { next.fadeOut('fast'); } else { next.fadeIn('fast'); };
                    if(lower >= 1) { prev.fadeIn('fast'); } else { prev.fadeOut('fast'); };
                };
            }); 
            $('li','#'+ options.controls).removeClass(options.current);
            $('li[data-index="'+page+'"]','#'+ options.controls).addClass(options.current);

            if(options.auto){
                if(options.clickstop && clicked){}else{ timeout = setTimeout(auto,options.pause); };
            };
        };

        function auto(){
            if(upper <= count){ page++; show(); }           
        };

        this.each(function(){ 

            obj = this;

            if(count>step){

                var pages = Math.floor(count/step);
                if((count/step) > pages) pages++;

                var ol = $('<ol id="'+ options.controls +'"></ol>').insertAfter(obj);

                if(options.nextprev){
                    prev = $('<li class="prev">Previous</li>')
                        .hide()
                        .appendTo(ol)
                        .click(function(){
                            clicked = true;
                            page--;
                            show();
                        });
                };

                if(options.numeric){
                    for(var i=1;i<=pages;i++){
                    $('<li data-index="'+ i +'">'+ i +'</li>')
                        .appendTo(ol)
                        .click(function(){  
                            clicked = true;
                            page = $(this).attr('data-index');
                            show();
                        });                 
                    };              
                };

                if(options.nextprev){
                    next = $('<li class="next">Next</li>')
                        .hide()
                        .appendTo(ol)
                        .click(function(){
                            clicked = true;         
                            page++;
                            show();
                        });
                };

                show();
            };
        }); 

    };  

})(jQuery);

我的目标是使 class="prev"class="next" 按钮始终可见。我从代码中删除了 hide(); 方法,但我还需要删除单击事件处理程序,因为否则即使我位于最后一页或第一页,我仍然可以单击按钮- 这就是事情变得混乱的地方。我必须绑定和取消绑定点击事件,但不知道如何正确执行。所以这是我的修改版本:

(function($) {

$.fn.easyPaginate = function(options){

    var defaults = {                
        step: 4,
        delay: 100,
        numeric: true,
        nextprev: true,
        auto:false,
        pause:4000,
        clickstop:true,
        controls: 'pagination',
        current: 'current' 
    }; 

    var options = $.extend(defaults, options); 
    var step = options.step;
    var lower, upper;
    var children = $(this).children();
    var count = children.length;
    var obj, next, prev;        
    var page = 1;
    var timeout;
    var clicked = false;

    function show(){
        clearTimeout(timeout);
        lower = ((page-1) * step);
        upper = lower+step;
        $(children).each(function(i){
            var child = $(this);
            child.hide();
            if(i>=lower && i<upper){ setTimeout(function(){ child.fadeIn('fast') }, ( i-( Math.floor(i/step) * step) )*options.delay ); }
            if(options.nextprev){
                if(upper >= count) { next.fadeOut('fast'); } else { next.fadeIn('fast'); };
                if(lower >= 1) { prev.fadeIn('fast'); } else { prev.fadeOut('fast'); };
            };
        }); 
        $('li','#'+ options.controls).removeClass(options.current);
        $('li[data-index="'+page+'"]','#'+ options.controls).addClass(options.current);

        if(options.auto){
            if(options.clickstop && clicked){}else{ timeout = setTimeout(auto,options.pause); };
        };
    };

    function auto(){
        if(upper <= count){ page++; show(); }           
    };

    this.each(function(){ 

        obj = this;

        if(count>step){

            var pages = Math.floor(count/step);
            if((count/step) > pages) pages++;

            var ol = $('<ol id="'+ options.controls +'"></ol>').insertAfter(obj);

            if(options.nextprev){
                prev = $('<li class="prev">Previous</li>')
                    .appendTo(ol)
                    .bind('click', function() {
                        clicked = true;
                        page--;
                        show();
                    });
            };

            if(options.numeric){
                for(var i=1;i<=pages;i++){
                $('<li data-index="'+ i +'">'+ i +'</li>')
                    .appendTo(ol)
                    .click(function(){  
                        clicked = true;
                        page = $(this).attr('data-index');
                        show();
                    });                 
                };              
            };

            if(options.nextprev){
                next = $('<li class="next">Next</li>')
                    .appendTo(ol)
                    .bind('click', function() {
                        clicked = true;         
                        page++;
                        show();
                    });
            };

            show();
        };
    }); 

};  

})(jQuery);

I have a jQuery Carousel script and I want to modify it's behavior slightly. Here is the script and test case in jsfiddle: http://jsfiddle.net/A4vHf/1/ . Below is the JS code.

(function($) {

    $.fn.easyPaginate = function(options){

        var defaults = {                
            step: 4,
            delay: 100,
            numeric: true,
            nextprev: true,
            auto:false,
            pause:4000,
            clickstop:true,
            controls: 'pagination',
            current: 'current' 
        }; 

        var options = $.extend(defaults, options); 
        var step = options.step;
        var lower, upper;
        var children = $(this).children();
        var count = children.length;
        var obj, next, prev;        
        var page = 1;
        var timeout;
        var clicked = false;

        function show(){
            clearTimeout(timeout);
            lower = ((page-1) * step);
            upper = lower+step;
            $(children).each(function(i){
                var child = $(this);
                child.hide();
                if(i>=lower && i<upper){ setTimeout(function(){ child.fadeIn('fast') }, ( i-( Math.floor(i/step) * step) )*options.delay ); }
                if(options.nextprev){
                    if(upper >= count) { next.fadeOut('fast'); } else { next.fadeIn('fast'); };
                    if(lower >= 1) { prev.fadeIn('fast'); } else { prev.fadeOut('fast'); };
                };
            }); 
            $('li','#'+ options.controls).removeClass(options.current);
            $('li[data-index="'+page+'"]','#'+ options.controls).addClass(options.current);

            if(options.auto){
                if(options.clickstop && clicked){}else{ timeout = setTimeout(auto,options.pause); };
            };
        };

        function auto(){
            if(upper <= count){ page++; show(); }           
        };

        this.each(function(){ 

            obj = this;

            if(count>step){

                var pages = Math.floor(count/step);
                if((count/step) > pages) pages++;

                var ol = $('<ol id="'+ options.controls +'"></ol>').insertAfter(obj);

                if(options.nextprev){
                    prev = $('<li class="prev">Previous</li>')
                        .hide()
                        .appendTo(ol)
                        .click(function(){
                            clicked = true;
                            page--;
                            show();
                        });
                };

                if(options.numeric){
                    for(var i=1;i<=pages;i++){
                    $('<li data-index="'+ i +'">'+ i +'</li>')
                        .appendTo(ol)
                        .click(function(){  
                            clicked = true;
                            page = $(this).attr('data-index');
                            show();
                        });                 
                    };              
                };

                if(options.nextprev){
                    next = $('<li class="next">Next</li>')
                        .hide()
                        .appendTo(ol)
                        .click(function(){
                            clicked = true;         
                            page++;
                            show();
                        });
                };

                show();
            };
        }); 

    };  

})(jQuery);

My goal is to make the class="prev" and class="next" buttons always visible. I removed the hide(); methods from the code, but I also need to remove the click event handlers because otherwise I can still click on the buttons even when I'm on the last or first pages accordingly - and that's here things get messed up. I have to bind and unbind the click events but don't know how to do it properly. So here is my modified version:

(function($) {

$.fn.easyPaginate = function(options){

    var defaults = {                
        step: 4,
        delay: 100,
        numeric: true,
        nextprev: true,
        auto:false,
        pause:4000,
        clickstop:true,
        controls: 'pagination',
        current: 'current' 
    }; 

    var options = $.extend(defaults, options); 
    var step = options.step;
    var lower, upper;
    var children = $(this).children();
    var count = children.length;
    var obj, next, prev;        
    var page = 1;
    var timeout;
    var clicked = false;

    function show(){
        clearTimeout(timeout);
        lower = ((page-1) * step);
        upper = lower+step;
        $(children).each(function(i){
            var child = $(this);
            child.hide();
            if(i>=lower && i<upper){ setTimeout(function(){ child.fadeIn('fast') }, ( i-( Math.floor(i/step) * step) )*options.delay ); }
            if(options.nextprev){
                if(upper >= count) { next.fadeOut('fast'); } else { next.fadeIn('fast'); };
                if(lower >= 1) { prev.fadeIn('fast'); } else { prev.fadeOut('fast'); };
            };
        }); 
        $('li','#'+ options.controls).removeClass(options.current);
        $('li[data-index="'+page+'"]','#'+ options.controls).addClass(options.current);

        if(options.auto){
            if(options.clickstop && clicked){}else{ timeout = setTimeout(auto,options.pause); };
        };
    };

    function auto(){
        if(upper <= count){ page++; show(); }           
    };

    this.each(function(){ 

        obj = this;

        if(count>step){

            var pages = Math.floor(count/step);
            if((count/step) > pages) pages++;

            var ol = $('<ol id="'+ options.controls +'"></ol>').insertAfter(obj);

            if(options.nextprev){
                prev = $('<li class="prev">Previous</li>')
                    .appendTo(ol)
                    .bind('click', function() {
                        clicked = true;
                        page--;
                        show();
                    });
            };

            if(options.numeric){
                for(var i=1;i<=pages;i++){
                $('<li data-index="'+ i +'">'+ i +'</li>')
                    .appendTo(ol)
                    .click(function(){  
                        clicked = true;
                        page = $(this).attr('data-index');
                        show();
                    });                 
                };              
            };

            if(options.nextprev){
                next = $('<li class="next">Next</li>')
                    .appendTo(ol)
                    .bind('click', function() {
                        clicked = true;         
                        page++;
                        show();
                    });
            };

            show();
        };
    }); 

};  

})(jQuery);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中的蝴蝶 2025-01-09 23:50:29

如果用户已到达页面边界,只需检查事件处理程序内部怎么样?

        if(options.nextprev){
            prev = $('<li class="prev">Previous</li>')
                .appendTo(ol)
                .bind('click', function() {

                    //check to see if there are any more pages in the negative direction
                    if (page > 0) {
                        clicked = true;
                        page--;
                        show();
                    }
                });
        }

        if(options.nextprev){
            next = $('<li class="next">Next</li>')
                .appendTo(ol)
                .bind('click', function() {

                    //check to see if there are any pages in the positive direction
                    if ((page + 1) < count) {
                        clicked = true;         
                        page++;
                        show();
                    }
                });
        }

更新

如果您希望上一个和下一个按钮永远不会消失,那么您需要删除 show() 函数中使元素淡入和淡出的代码。该部分代码是这样的:

        if(options.nextprev){
            if(upper >= count) { next.fadeOut('fast'); } else { next.fadeIn('fast'); };
            if(lower >= 1) { prev.fadeIn('fast'); } else { prev.fadeOut('fast'); };
        };

一个简单的解决方法是仅注释此部分,这里是一个演示: http: //jsfiddle.net/A4vHf/18/

What about just checking inside your event handlers if the user has reached the bounds of the pages?

        if(options.nextprev){
            prev = $('<li class="prev">Previous</li>')
                .appendTo(ol)
                .bind('click', function() {

                    //check to see if there are any more pages in the negative direction
                    if (page > 0) {
                        clicked = true;
                        page--;
                        show();
                    }
                });
        }

        if(options.nextprev){
            next = $('<li class="next">Next</li>')
                .appendTo(ol)
                .bind('click', function() {

                    //check to see if there are any pages in the positive direction
                    if ((page + 1) < count) {
                        clicked = true;         
                        page++;
                        show();
                    }
                });
        }

Update

If you want the previous and next buttons to never dissapear then you need to remove the code in the show() function that fades the elements in and out. That section of code is this:

        if(options.nextprev){
            if(upper >= count) { next.fadeOut('fast'); } else { next.fadeIn('fast'); };
            if(lower >= 1) { prev.fadeIn('fast'); } else { prev.fadeOut('fast'); };
        };

A simple fix is to just comment this section-out, here is a demo: http://jsfiddle.net/A4vHf/18/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文