jq 实现无缝轮播

这段代码展示了如何利用jQuery创建一个无缝轮播效果。它包括了克隆第一个元素、设置轮播宽度、监听点击和鼠标悬停事件,以及平滑滚动到下一个或上一个项目。轮播会在鼠标离开时自动播放,进入时停止播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面的例子len是实际item的长度。


 lunbo:function (time){

                var topIn =$('#content_top');

                var oli = $('#content_top .top_item');

                var len = oli.length;

                var topPo = $('#top_po');

                var index = 0;

                var int = null;

                for(var i=0;i<len;i++){

                    topPo.append("<li></li>");

                };

                var first = oli.eq(0).clone();

                topIn.append(first);

 

                var minLi = topPo.find('li');

                minLi.eq(0).addClass('on')

                topIn.css('width',(oli.length+1)*1060)

 

                minLi.click(function(){

                    index = $(this).index();

                    topIn.css("left",index*1060*-1+"px");

                    $(this).addClass('on').siblings().removeClass('on')

                })

 

                topIn.mouseenter(function(){

                    clearInterval(int);

                }).mouseleave(function(){

                    int = setInterval(run,time);

                })

                topPo.mouseenter(function(){

                    clearInterval(int);

                }).mouseleave(function(){

                    int = setInterval(run,time);

                })

 

                int = setInterval(run,time)

                function run(){

                    index++;

                    if(index>len){

                        topIn.stop().css("left",0);

                        index =1;

                    }

                    if(index==len){      //5

                        topIn.animate({

                            left:parseInt(topIn.css('left'))-1060+"px"

                        },500)

                        minLi.eq(0).addClass('on').siblings().removeClass('on');

                    }

                    if(index<len){

                        topIn.animate({

                            left:parseInt(topIn.css('left'))-1060+"px"

                        },500)

                        minLi.eq(index).addClass('on').siblings().removeClass('on');

                    }

                }

            },




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值