JS使用tween.js動(dòng)畫庫(kù)實(shí)現(xiàn)輪播圖并且有切換功能
效果圖如下所示:
<!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"); //記錄圖片下標(biāo) var index = 0; var w = -500; var timer = null; //自動(dòng)播放 //放在計(jì)時(shí)器就是自動(dòng)播放,騎士就是下一張的操作 function autoImg(){ index++; if(index>3){ // console.log(index); index=0; // console.log(index); } //動(dòng)畫開始時(shí)間 var t = 0; //動(dòng)畫結(jié)束時(shí)間 var d = 30; //動(dòng)畫的起始位置 var b = box.offsetLeft; //動(dòng)畫的終止位置減去動(dòng)畫的起始位置,該變量為-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); } //關(guān)閉輪播 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; } //動(dòng)畫開始時(shí)間 var t = 0; //動(dòng)畫結(jié)束時(shí)間 var d = 30; //動(dòng)畫的起始位置 var b = box.offsetLeft; //動(dòng)畫的終止位置減去動(dòng)畫的起始位置 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; //動(dòng)畫結(jié)束時(shí)間 var d = 30; //動(dòng)畫的起始位置 var b = box.offsetLeft; //動(dòng)畫的終止位置減去動(dòng)畫的起始位置 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>
總結(jié)
以上所述是小編給大家介紹的JS使用tween.js動(dòng)畫庫(kù)實(shí)現(xiàn)輪播圖并且有切換功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- tween.js緩動(dòng)補(bǔ)間動(dòng)畫算法示例
- 詳解tween.js 中文使用指南
- 詳解tween.js的使用教程
- 原生JS實(shí)現(xiàn)移動(dòng)端web輪播圖詳解(結(jié)合Tween算法造輪子)
- js學(xué)習(xí)心得_一個(gè)簡(jiǎn)單的動(dòng)畫庫(kù)封裝tween.js
- JavaScript實(shí)現(xiàn)的Tween算法及緩沖特效實(shí)例代碼
- JavaScript Tween算法及緩動(dòng)效果
- JS Tween 顏色漸變
- JS+HTML5手機(jī)開發(fā)之滾動(dòng)和慣性緩動(dòng)實(shí)現(xiàn)方法分析
- javascript中的緩動(dòng)效果實(shí)現(xiàn)程序
- tweenjs緩動(dòng)算法的使用實(shí)例分析
相關(guān)文章
45個(gè)JavaScript編程注意事項(xiàng)、技巧大全
這篇文章主要介紹了45個(gè)JavaScript編程注意事項(xiàng)、技巧大全,在這篇文章里,我將分享一些JavaScript的技巧、秘訣和最佳實(shí)踐,除了少數(shù)幾個(gè)外,不管是瀏覽器的JavaScript引擎,還是服務(wù)器端JavaScript解釋器,均適用,需要的朋友可以參考下2015-02-02純javascript移動(dòng)優(yōu)先的幻燈片效果
這篇文章主要介紹了純javascript實(shí)現(xiàn)移動(dòng)優(yōu)先的幻燈片效果,感興趣的小伙伴們可以參考一下2015-11-11d3.js實(shí)現(xiàn)自定義多y軸折線圖的示例代碼
本篇文章主要介紹了d3.js實(shí)現(xiàn)自定義多y軸折線圖的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05JS數(shù)組操作(數(shù)組增加、刪除、翻轉(zhuǎn)、轉(zhuǎn)字符串、取索引、截取(切片)slice、剪接splice、數(shù)組合并)
這篇文章主要介紹了JS數(shù)組操作(數(shù)組增加、刪除、翻轉(zhuǎn)、轉(zhuǎn)字符串、取索引、截取(切片)slice、剪接splice、數(shù)組合并)的相關(guān)資料,需要的朋友可以參考下2016-05-05firefo xml 讀寫實(shí)現(xiàn)js代碼
firefo xml 讀寫實(shí)現(xiàn) 不過要是你的xml要編碼成功還得在str前面加上xml頭,千萬(wàn)別忘了啊。2009-06-06JavaScript中好用的數(shù)組對(duì)象排序方法分享
在日常工作中,我們經(jīng)常需要對(duì)數(shù)組對(duì)象進(jìn)行排序,尤其是在處理數(shù)據(jù)可視化需求中。本文將介紹一些簡(jiǎn)單而又實(shí)用的方法,幫助你實(shí)現(xiàn)對(duì)數(shù)組對(duì)象的某幾個(gè) key 進(jìn)行排序2023-05-05使用 Javascript 實(shí)現(xiàn)瀏覽器推送提醒功能的示例
本篇文章主要介紹了使用 Javascript 實(shí)現(xiàn)瀏覽器推送提醒功能的示例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-11-11