标题:求助:这个JS如何修改自动移动等待时间让其实现连续滚动。
只看楼主
兵乓
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-11-17
结帖率:100%
已结贴  问题点数:20 回复次数:3 
求助:这个JS如何修改自动移动等待时间让其实现连续滚动。
(function($){
    $.extend($.easing,{
        easeInSine: function (x, t, b, c, d) {
            return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
        }
    });

    $.fn.Xslider=function(settings){
        settings=$.extend({},$.fn.Xslider.sn.defaults,settings);
        this.each(function(){
            var scrollobj=settings.scrollobj ? $(this).find(settings.scrollobj) : $(this).find("ul"),
                viewedSize=settings.viewedSize || (settings.dir=="H" ? scrollobj.parent().width() : scrollobj.parent().height()),//length of the wrapper visible;
                scrollunits=scrollobj.find("li"),//units to move;
                unitlen=settings.unitlen || (settings.dir=="H" ? scrollunits.eq(0).outerWidth() : scrollunits.eq(0).outerHeight()),
                unitdisplayed=settings.unitdisplayed,//units num displayed;
                numtoMove=settings.numtoMove > unitdisplayed ? unitdisplayed : settings.numtoMove,
                scrollobjSize=settings.scrollobjSize || scrollunits.length*unitlen,//length of the scrollobj;
                offset=0,//max width to move;
                offsetnow=0,//scrollobj now offset;
                movelength=unitlen*numtoMove,
                pos=settings.dir=="H" ? "left" : "top",
                moving=false,//moving now?;
                btnright=$(this).find("a.aright"),
                btnleft=$(this).find("a.aleft");
            
            btnright.unbind("click");
            btnleft.unbind("click");
                    
            settings.dir=="H" ? scrollobj.css("left","0px") : scrollobj.css("top","0px");
                           
            if(scrollobjSize>viewedSize){
                if(settings.loop=="cycle"){
                    btnleft.removeClass("agrayleft");
                    if(scrollunits.length<2*numtoMove+unitdisplayed-numtoMove){
                        scrollobj.find("li").clone().appendTo(scrollobj);   
                    }
                }else{
                    btnleft.addClass("agrayleft");
                    offset=scrollobjSize-viewedSize;
                }
                btnright.removeClass("agrayright");
            }else{
                btnleft.addClass("agrayleft");
                btnright.addClass("agrayright");
            }

            btnleft.click(function(){
                if($(this).is("[class*='agrayleft']")){return false;}
               
                if(!moving){
                    moving=true;
                    
                    if(settings.loop=="cycle"){
                        scrollobj.find("li:gt("+(scrollunits.length-numtoMove-1)+")").prependTo(scrollobj);
                        scrollobj.css(pos,"-"+movelength+"px");
                        $.fn.Xslider.sn.animate(scrollobj,0,settings.dir,settings.speed,function(){moving=false;});
                    }else{
                        offsetnow-=movelength;
                        if(offsetnow>unitlen*unitdisplayed-viewedSize){
                            $.fn.Xslider.sn.animate(scrollobj,-offsetnow,settings.dir,settings.speed,function(){moving=false;});
                        }else{
                            $.fn.Xslider.sn.animate(scrollobj,0,settings.dir,settings.speed,function(){moving=false;});
                            offsetnow=0;
                            $(this).addClass("agrayleft");
                        }
                        btnright.removeClass("agrayright");
                    }
                }

                return false;
            });
            btnright.click(function(){
                if($(this).is("[class*='agrayright']")){return false;}
               
                if(!moving){
                    moving=true;
                    
                    if(settings.loop=="cycle"){
                        var callback=function(){
                            scrollobj.find("li:lt("+numtoMove+")").appendTo(scrollobj);
                            scrollobj.css(pos,"0px");
                            moving=false;
                        }
                        $.fn.Xslider.sn.animate(scrollobj,-movelength,settings.dir,settings.speed,callback);
                    }else{
                        offsetnow+=movelength;
                        if(offsetnow<offset-(unitlen*unitdisplayed-viewedSize)){
                            $.fn.Xslider.sn.animate(scrollobj,-offsetnow,settings.dir,settings.speed,function(){moving=false;});
                        }else{
                            $.fn.Xslider.sn.animate(scrollobj,-offset,settings.dir,settings.speed,function(){moving=false;});//滚动到最后一个位置;
                            offsetnow=offset;
                            $(this).addClass("agrayright");
                        }
                        btnleft.removeClass("agrayleft");
                    }
                }
               
                return false;
            });
            
            if(settings.autoscroll){
                $.fn.Xslider.sn.autoscroll($(this),settings.autoscroll);
            }
        })
    }
   
    $.fn.Xslider.sn={
        defaults:{
            dir:"H",
            speed:500
        },
        animate:function(obj,w,dir,speed,callback){
            if(dir=="H"){
                obj.animate({
                    left:w
                },speed,"easeInSine",callback);
            }else if(dir=="V"){
                obj.animate({
                    top:w
                },speed,"easeInSine",callback);   
            }   
        },
        autoscroll:function(obj,time){
            var  vane="right";
            function autoscrolling(){
                if(vane=="right"){
                    if(!obj.find("a.agrayright").length){
                        obj.find("a.aright").trigger("click");
                    }else{
                        vane="left";
                    }
                }
                if(vane=="left"){
                    if(!obj.find("a.agrayleft").length){   
                        obj.find("a.aleft").trigger("click");
                    }else{
                        vane="right";
                    }
                }
            }
            var scrollTimmer=setInterval(autoscrolling,time);
            obj.hover(function(){
                clearInterval(scrollTimmer);
            },function(){
                scrollTimmer=setInterval(autoscrolling,time);
            });
        }
    }
})(jQuery);
搜索更多相关主题的帖子: 连续 时间 function settings return 
2012-11-17 16:29
兵乓
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2012-11-17
得分:0 
求高手啊,求指点啊
2012-11-17 16:31
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
得分:14 
回复 2楼 兵乓
膜拜大作
2012-11-19 16:22
xdsnet
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2013-2-27
得分:0 
你的代码不完整,缺少对应的HTML,所以不好调试判断。
修改速度的话应该是
$.fn.Xslider.sn={
        defaults:{
            dir:"H",
            speed:500//这里修改
        },

2013-02-28 09:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-387395-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.173981 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved