资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

jquery中怎么封装无缝图片轮播组件

小编给大家分享一下jquery中怎么封装无缝图片轮播组件,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!

为乌拉特中等地区用户提供了全套网页设计制作服务,及乌拉特中网站建设行业解决方案。主营业务为成都做网站、网站制作、乌拉特中网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

dom结构

首先是dom结构,将所有内容放入一个大盒子,应用ul标签存放图片列表,圆点定位图片位置。


  
   
   
   
   
   
  
  
   
  
   <
   >

下面是盒子样式,这里使用百分比自适应,大盒子使用overflow: hidden;防止图片溢出,将ul宽设置为图片总宽度+1(这里+1后面会有用到)。

 #box{
 width:100%;
 height:40.0em;
 overflow: hidden;
 position: absolute;
}
 #box #banners{
 width:500%;
 position: relative;
}
 #box .banners-img{
 float: left;
 width:20%;
 height:40.0em;
}
 #box .banners-img img{
 width:100%;
 height: 100%;
}
 .num{
 width:10%;
 height:2.0em;
 position: relative;
 top: 82%;
 left: 40%;
 -webkit-transform: rotate(-90deg);
 -ms-transform: rotate(-90deg);
 transform:rotate(-90deg);
}
 .num li{
 width:2.0em;
 height:100%;
 position: relative;
 border-radius: 50%;
 background-color: grey; 
}
 .num .on{
 background-color: black;
}
 
 .btns{
 width:1.0em;
 height:1.0em;
 background-color: rgba(0,0,0,0.5);
 position:relative;
 top:50%;
 color: white;
 font-size: 3.0em;
 text-align: center;
 line-height: 1.0em;
 cursor: pointer;
 display: none;
}
 #box:hover .btns{
 display: block;
}
 .btn_l{
 left: 0;
}
 .btn_r{
 right:0;
}

组件使用jquery编写

function addImg(item,arrSrc){ //添加图片,这里需同时修改样式,读者可自行修改
   for(let i = 0;i");
  }
  
 //获取图片宽度
 var imgWidth = parseInt($("#banners .banners-img").css("width"));
 //因宽度为百分比,窗口大小变化时需重新获取
 window.onresize = function(){
  var newWidth = $("#banners .banners-img").css("width");
      imgWidth = parseInt(newWidth);
  }
 //鼠标移到按钮时轮播
 $(".num li").hover(function(){
  var index = $(this).index();
  i=index;
  $("#box #banners").stop().animate({left:-i*imgWidth},500);
  $("#box .num li").eq(i).addClass("on")
  .siblings().removeClass("on");
  
  })
  //自动轮播
  var t = setInterval(function(){
   i++;
   move();
  },3000);
  //鼠标移入时停止自动轮播
  $("#box").hover(function(){
   clearInterval(t);
  },function(){
   t = setInterval(function(){
   i++;
   move();
  },3000);
  })
  
  //手动轮播
  $("#box .btn_l").click(function(){
   i--;
   move();
  });
  
  $("#box .btn_r").click(function(){
   i++;
   move();
  });
 
  //封装一个运动函数
  // alert(imgWidth);
  function move(){
   if(i==Size){ //当轮播到最后一张时过渡到第一张图片
   $("#box #banners").css({left:0});
   i=1;
   }
   if(i==-1){
   $("#box #banners").css({left:-(Size-1)*imgWidth});
   i=Size-2;
   }
   $("#box #banners").stop().animate({left:-i*imgWidth},500);
   if(i==Size-1){
   $("#box .num li").eq(0).addClass("on")
   .siblings().removeClass("on");
   }else{
   $("#box .num li").eq(i).addClass("on")
   .siblings().removeClass("on");
   }
   
  }

看完了这篇文章,相信你对“jquery中怎么封装无缝图片轮播组件”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


网站名称:jquery中怎么封装无缝图片轮播组件
URL分享:http://cdkjz.cn/article/jeooep.html
返回首页 了解更多建站资讯
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220