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)畫開(kāi)始時(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)畫開(kāi)始時(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ì)大家有所幫助,如果大家有任何疑問(wèn)請(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ī)開(kāi)發(fā)之滾動(dòng)和慣性緩動(dòng)實(shí)現(xiàn)方法分析
- javascript中的緩動(dòng)效果實(shí)現(xiàn)程序
- tweenjs緩動(dòng)算法的使用實(shí)例分析
相關(guān)文章
JavaScript實(shí)現(xiàn)瀏覽器網(wǎng)頁(yè)自動(dòng)滾動(dòng)并點(diǎn)擊的示例代碼
這篇文章主要介紹了JavaScript實(shí)現(xiàn)瀏覽器網(wǎng)頁(yè)的自動(dòng)滾動(dòng)并點(diǎn)擊的示例代碼,幫助大家更好的理解和學(xué)習(xí)JavaScript的使用,感興趣的朋友可以了解下2020-12-12
js 禁止選擇功能實(shí)現(xiàn)代碼(兼容IE/Firefox)
有時(shí)候出于某種需要,不希望用戶可以選擇某個(gè)區(qū)域,進(jìn)行下面的操作,這里給出簡(jiǎn)單的代碼。2010-04-04
JavaScript中5個(gè)重要的Observer函數(shù)小結(jié)
瀏覽器為開(kāi)發(fā)者提供了功能豐富的Observer,本文主要介紹了JavaScript中5個(gè)重要的Observer函數(shù)小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
JavaScript的變量聲明提升問(wèn)題淺析(Hoisting)
大家應(yīng)該都只奧javascript的變量聲明具有hoisting機(jī)制,JavaScript引擎在執(zhí)行的時(shí)候,會(huì)把所有變量的聲明都提升到當(dāng)前作用域的最前面。網(wǎng)上關(guān)于JavaScript的變量聲明提升問(wèn)題的文章有很多,這篇文章將再次談?wù)勱P(guān)于這方面的問(wèn)題,有需要的朋友們可以參考借鑒。2016-11-11
js實(shí)現(xiàn)網(wǎng)頁(yè)圖片輪換播放
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)網(wǎng)頁(yè)圖片輪換播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
微信小程序自定義select下拉選項(xiàng)框組件的實(shí)現(xiàn)代碼
微信小程序中沒(méi)有select下拉選項(xiàng)框,所以只有自定義。這篇文章主要介紹了微信小程序自定義select下拉選項(xiàng)框組件,需要的朋友可以參考下2018-08-08
Javascript 構(gòu)造函數(shù),公有,私有特權(quán)和靜態(tài)成員定義方法
其中公有方法聲明的部分采用的兩種方式,在實(shí)際應(yīng)用中一般采取一種方式就可以了,如果兩種方式都要采用的話,應(yīng)注意順序,防止前面寫的方法被清空或覆蓋。2009-11-11
js實(shí)現(xiàn)圖片區(qū)域可點(diǎn)擊大小隨意改變(適用移動(dòng)端)代碼實(shí)例
這篇文章主要介紹了js實(shí)現(xiàn)圖片區(qū)域可點(diǎn)擊大小隨意改變(適用移動(dòng)端)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
JavaScript字符串轉(zhuǎn)數(shù)字的多種方法總結(jié)
在 JavaScript 開(kāi)發(fā)中,我們經(jīng)常需要將字符串轉(zhuǎn)換為數(shù)字,例如從輸入框獲取用戶輸入后進(jìn)行數(shù)學(xué)計(jì)算,JavaScript 提供了多種方法來(lái)實(shí)現(xiàn)這一功能,如 parseInt、parseFloat、Number 等,本文將詳細(xì)介紹這些方法的使用方式、適用場(chǎng)景以及可能的坑,需要的朋友可以參考下2025-03-03

