国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

jquery無縫圖片輪播組件封裝

2024-05-06 15:42:28
字體:
供稿:網(wǎng)友

圖片輪播在我們的前端開發(fā)中是非常常見的,下面是自己寫的一個圖片輪播組件,支持自動輪播,手動輪播,無縫銜接。

dom結(jié)構(gòu)

首先是dom結(jié)構(gòu),將所有內(nèi)容放入一個大盒子,應(yīng)用ul標(biāo)簽存放圖片列表,圓點(diǎn)定位圖片位置。

<div id="box">  <ul id="banners">   <li class="banners-img"><img src="img/DSC_1913.JPG" /></li>   <li class="banners-img"><img src="img/DSC_1913.JPG" /></li>   <li class="banners-img"><img src="img/DSC_1913.JPG" /></li>   <li class="banners-img"><img src="img/DSC_1913.JPG" /></li>     </ul>  <ul class="num">   <li class="on"></li>  </ul>   <div class="btns btn_l"><</div>   <div class="btns btn_r">></div></div>

下面是盒子樣式,這里使用百分比自適應(yīng),大盒子使用overflow: hidden;防止圖片溢出,將ul寬設(shè)置為圖片總寬度+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<item;i++){     item[i].attr('src',arrSrc[i]);            }          }        var srcs = ['img/DSC_1913.JPG','img/DSC_1914.JPG','img/DSC_1915.JPG','img/DSC_1916.JPG'];        addImg($('.banners-img img'),srcs);  var Clone = $("#banners li").first().clone(); //追加一張圖片,輪到最后一張時能夠自然過渡,這里初學(xué)者需慢慢體會 $("#banners").append(Clone); var Size = $("#banners li").size(); //alert(Size); for(var j=0;j<Size-2;j++){  $(".num").append("<li></li>");  }   //獲取圖片寬度 var imgWidth = parseInt($("#banners .banners-img").css("width")); //因?qū)挾葹榘俜直龋翱诖笮∽兓瘯r需重新獲取 window.onresize = function(){  var newWidth = $("#banners .banners-img").css("width");      imgWidth = parseInt(newWidth);  } //鼠標(biāo)移到按鈕時輪播 $(".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);  //鼠標(biāo)移入時停止自動輪播  $("#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();  });   //封裝一個運(yùn)動函數(shù)  // alert(imgWidth);  function move(){   if(i==Size){ //當(dāng)輪播到最后一張時過渡到第一張圖片   $("#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");   }     }            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 蒙阴县| 麻城市| 安陆市| 青河县| 朝阳区| 蒙阴县| 中阳县| 蕉岭县| 扎赉特旗| 衢州市| 宜川县| 唐山市| 卓尼县| 峨眉山市| 晋江市| 武城县| 新河县| 禹城市| 德钦县| 泰宁县| 南投市| 庄浪县| 广宗县| 革吉县| 浦东新区| 彭泽县| 宁远县| 阳信县| 宕昌县| 古田县| 葫芦岛市| 桃源县| 莱芜市| 大石桥市| 长顺县| 阿合奇县| 濮阳县| 潞西市| 西吉县| 陈巴尔虎旗| 镇原县|