使用js/jquery编写首尾衔接的图片轮播图

使用js/jquery编写首尾衔接的图片轮播图

#实现思想

编写两个存放图片列表的容器,两个容器并排放置,同时向左滚,等第一个滚出头后,把第一个放在第二个容器的后面,成为第二个容器,就这样达到首尾相接循环滚。

html模块

      <section class=" ad_section" >
               <!-- 此处的left:0px必须设置,否者第一次的滚动不会触发trasition,会直处处的 -->
               <figure class="ad_figure " style="left:0px;trasition:left 1s;">
                   <img src="/static/img/indexN_ad1.png"/>
                   <img src="/static/img/indexN_ad2.png"/>
                   <img src="/static/img/indexN_ad3.png"/>
                   <img  src="/static/img/indexN_ad4.png"/>
                   <img src="/static/img/indexN_ad5.png"/>
                   <img src="/static/img/indexN_ad6.png"/>
                   <img src="/static/img/indexN_ad7.png"/>
                   <img src="/static/img/indexN_ad8.png"/>
                   <img src="/static/img/indexN_ad9.png"/>
                   <img src="/static/img/indexN_ad10.png"/>
                   <img src="/static/img/indexN_ad11.png"/>
               </figure> 
               <figure class="ad_figure" >
                   <img src="/static/img/indexN_ad1.png"/>
                   <img src="/static/img/indexN_ad2.png"/>
                   <img src="/static/img/indexN_ad3.png"/>
                   <img  src="/static/img/indexN_ad4.png"/>
                   <img src="/static/img/indexN_ad5.png"/>
                   <img src="/static/img/indexN_ad6.png"/>
                   <img src="/static/img/indexN_ad7.png"/>
                   <img src="/static/img/indexN_ad8.png"/>
                   <img src="/static/img/indexN_ad9.png"/>
                   <img src="/static/img/indexN_ad10.png"/>
                   <img src="/static/img/indexN_ad11.png"/>
               </figure> 
       </section> 

css模块

.ad_section{
    display: block;
    overflow: hidden;
    height:80px;
    position:relative;
    z-index: 8;
}
 .ad_figure{
    width: auto;
    display: inline;
    white-space: nowrap;
    position: absolute;
    z-index: 9;
}
.ad_figure img{
    display: inline-block;
    vertical-align: middle;;
    margin:auto 79px auto 0px;
}

js模块

function loop(){
   this.sumlength = $(".ad_section figure:eq(0)").outerWidth();//获取要滚动容器的width
   this.currnettranslatex1=0;//第一个容器每次滚动的距离
   this.currnettranslatex2=this.sumlength;//第二个容器每次滚动的距离
   this.currentimg=0;//因为每张图片的width不一,利用一个变量保存当前图片的序号
   var intervalF = setInterval(function(){
        this.step = parseInt($(".ad_section figure:eq(0) img").eq( this.currentimg).outerWidth())+79;//滚动的步长
        this.currnettranslatex1 += (-this.step);//第一个容器每次滚动的距离
        this.currnettranslatex2 += (-this.step);//第二个容器每次滚动 的距离
          $('.ad_section figure:eq(0)').css({'left':this.currnettranslatex1+'px','transition':'left 1s'});//容器1利用left实现滚动,transition实现优美点的滚动
          $('.ad_section figure:eq(1)').css({'left':this.currnettranslatex2+'px','transition':'left 1s'}); //容器2同上
          if(this.currentimg<10){
             this.currentimg++;
          }else{//当第一个容器滚完后
            this.currentimg=0;//当前图片序号归0
            this.currnettranslatex1 = 0;//第一个容器的left归为初始值
            this.currnettranslatex2 =this.sumlength;//第二个容器的left归为初始值
             $($('.ad_section figure:eq(0)')).insertAfter($('.ad_section figure:eq(1)'));//把第一个滚玩的容器放到后面接着滚
             $('.ad_section figure:eq(0)').css('left',this.currnettranslatex1+'px');
             $('.ad_section figure:eq(1)').css('left',this.currnettranslatex2+'px');
          }   
   },1000); 
}

loop();

查看效果链接
此链接还在坐飞船,到了再更新。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值