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

首頁 > 編程 > JavaScript > 正文

JS使用tween.js動畫庫實現輪播圖并且有切換功能

2019-11-19 13:28:40
字體:
來源:轉載
供稿:網友

效果圖如下所示:

<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8"> <title>Document</title> <style> .wrap{  width: 500px;  height: 300px;  position: relative;  overflow: hidden; } .box{  width: 500%;  height: 100%;  position: absolute;  left: 0; } .box>div{  width: 500px;  height: 300px;  float: left;  font-size: 100px;  text-align: center;  line-height: 300px; } div:nth-child(1){  background-color: red; } div:nth-child(2){  background-color: green; } div:nth-child(3){  background-color: pink; } div:nth-child(4){  background-color: blue; } </style></head><body> <input type="button" value="last"> <input type="button" value="next"> <input type="button" value="按鈕1" class="ha"> <input type="button" value="按鈕2" class="ha"> <input type="button" value="按鈕3" class="ha"> <input type="button" value="按鈕4" class="ha"> <div class="wrap"> <div class="box">  <div id="one">div1</div>  <div>div2</div>  <div>div3</div>  <div>div4</div>  <div id="one">div1</div> </div> </div></body><script src="./tween.js"></script><script> //獲取元素 var inps = document.querySelectorAll("input"); var box = document.querySelector(".box"); var ha = document.querySelectorAll(".ha"); //記錄圖片下標 var index = 0; var w = -500; var timer = null; //自動播放 //放在計時器就是自動播放,騎士就是下一張的操作 function autoImg(){ index++; if(index>3){  // console.log(index);  index=0;  // console.log(index); } //動畫開始時間 var t = 0; //動畫結束時間 var d = 30; //動畫的起始位置 var b = box.offsetLeft; //動畫的終止位置減去動畫的起始位置,該變量為-500 // var c =index*w-b; console.log(c); var c = -500; if(b<=-1500){  b=0; } clearInterval(timer); timer = setInterval(function(){  t++;  box.style.left=Tween.Linear(t,b,c,d)+"px";  if(t>=d){  clearInterval(timer);  } },30); }//關閉輪播 function clearAuto(){ clearInterval(autotimer);  autotimer = setInterval(autoImg,3000); } var autotimer = setInterval(autoImg,3000); //下一張 inps[1].onclick = function(){  clearAuto();  autoImg(); } //上一張 function prevImg(){ index--; if(index<0){  index=3; } //動畫開始時間 var t = 0; //動畫結束時間 var d = 30; //動畫的起始位置 var b = box.offsetLeft; //動畫的終止位置減去動畫的起始位置 var c =index*w-b; clearInterval(timer); timer = setInterval(function(){  t++;  box.style.left=Tween.Linear(t,b,c,d)+"px";  if(t>=d){  clearInterval(timer);  } },30); } inps[0].onclick = function(){ clearAuto(); prevImg(); }function indexImg(n){  index = n; var t = 0; //動畫結束時間 var d = 30; //動畫的起始位置 var b = box.offsetLeft; //動畫的終止位置減去動畫的起始位置 var c =index*w-b; clearInterval(timer); timer = setInterval(function(){  t++;  box.style.left=Tween.Linear(t,b,c,d)+"px";  if(t>=d){  clearInterval(timer);  } },30); }  for(var i=0;i<ha.length;i++){  (function(i){  ha[i].onclick = function(){   // box.style.left = (-500*(i-2))+"px";   clearAuto();   indexImg(i);   console.log(i);  }  })(i); }</script></html>

總結

以上所述是小編給大家介紹的JS使用tween.js動畫庫實現輪播圖并且有切換功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 塔城市| 清远市| 栖霞市| 榆中县| 沛县| 新郑市| 六枝特区| 富宁县| 中方县| 福海县| 克什克腾旗| 于都县| 张掖市| 文成县| 蒙山县| 利津县| 宜章县| 洛扎县| 临潭县| 阜新| 江安县| 锡林郭勒盟| 错那县| 宿松县| 临桂县| 吐鲁番市| 成武县| 玉田县| 巴中市| 小金县| 旬阳县| 久治县| 克什克腾旗| 布拖县| 金乡县| 嫩江县| 大理市| 方正县| 资中县| 福泉市| 宝坻区|