JavaScript動(dòng)畫原理之如何使用js進(jìn)行動(dòng)畫效果的實(shí)現(xiàn)
前言
動(dòng)畫對于我們來說都不陌生,css里面就有很多動(dòng)畫,2d,3d等各種動(dòng)畫,本篇主要是如何使用js實(shí)現(xiàn)動(dòng)畫效果,如果本篇文章對你有幫助,點(diǎn)贊支持一下吧!
1.動(dòng)畫原理
1.獲得盒子當(dāng)前位置
2.讓盒子在當(dāng)前位置加上1個(gè)移動(dòng)距離
3.利用定時(shí)器不斷重復(fù)這個(gè)操作
4.加一個(gè)結(jié)束定時(shí)器的條件
5.注意該元素需要添加定位,才能使用element.style.left
<body>
<div>
</div>
<script>
var div = document.querySelector('div');
var timer = setInterval(function () {
if (div.offsetLeft >= 500) {
clearInterval(timer);
}
div.style.left = div.offsetLeft + 2 + 'px';
}, 30)
</script>
</body>主要核心就是利用定時(shí)器進(jìn)行動(dòng)畫的實(shí)現(xiàn)

2.動(dòng)畫函數(shù)的封裝
<script>
// 簡單動(dòng)畫函數(shù)封裝
function animate(obj, rug) {
var timer = setInterval(function () {
if (obj.offsetLeft >= rug) {
clearInterval(timer);
}
obj.style.left = obj.offsetLeft + 2 + 'px';
}, 30)
}
var div = document.querySelector('div');
animate(div,300);
</script>把這個(gè)動(dòng)畫封裝成一個(gè)函數(shù),方便以后的使用,該封裝函數(shù)里的obj是哪個(gè)元素要進(jìn)行動(dòng)畫的實(shí)現(xiàn)rug是該元素要移動(dòng)多少距離
3.給不同元素添加定時(shí)器
<body>
<div>
</div>
<button>點(diǎn)擊走</button>
<script>
// 簡單動(dòng)畫函數(shù)封裝
// 給不同元素添加定時(shí)器
function animate(obj, rug) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
if (obj.offsetLeft >= rug) {
clearInterval(obj.timer);
} else {
obj.style.left = obj.offsetLeft + 2 + 'px';
}
}, 30)
}
var div = document.querySelector('div');
var but = document.querySelector('button');
but.addEventListener('click', function () {
animate(div, 200);
})
</script>這樣就能實(shí)現(xiàn)多個(gè)元素進(jìn)行動(dòng)畫的使用了,并且每個(gè)元素都有屬于自己的定時(shí)器
4.緩動(dòng)動(dòng)畫原理
公式:目標(biāo)值-現(xiàn)在的位置/10 ,作為每次的移動(dòng)距離
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
div {
position: absolute;
left: 0;
width: 100px;
height: 100px;
background-color: pink;
}
</style>
</head>
<body>
<button>點(diǎn)擊</button>
<div></div>
<script>
function animate(obj, rug) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
// 步長值
var step = (rug - obj.offsetLeft) / 10;
if (obj.offsetLeft == rug) {
clearInterval(obj.timer);
} else {
obj.style.left = obj.offsetLeft + step + 'px';
}
}, 15)
}
var div = document.querySelector('div');
var but = document.querySelector('button');
but.addEventListener('click', function () {
animate(div, 500);
})
</script>
</body>
</html>
5.給動(dòng)畫添加回調(diào)函數(shù)
回調(diào)函數(shù)原理:函數(shù)可以作為一個(gè)參數(shù)。將這個(gè)函數(shù)作為參數(shù)傳到另一個(gè)函數(shù)里面 ,當(dāng)那個(gè)函數(shù)執(zhí)行完之后,再執(zhí)行傳進(jìn)去的這個(gè)函數(shù),這個(gè)過程就叫做回調(diào)。

當(dāng)跑完800米后,會(huì)彈出一個(gè)框“hello”,這個(gè)就是在執(zhí)行完800米這個(gè)動(dòng)畫后再次進(jìn)行的函數(shù),這就是回調(diào)函數(shù)

6.動(dòng)畫函數(shù)的使用
實(shí)現(xiàn)側(cè)邊欄滑動(dòng)效果
當(dāng)鼠標(biāo)經(jīng)過slider就會(huì)讓con這 個(gè)盒子滑動(dòng)到左側(cè)
當(dāng)鼠標(biāo)離開slider就會(huì)讓con這 個(gè)盒子滑動(dòng)到右側(cè)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="./js/animate.js"></script>
<style>
.silder {
margin-left: 1600px;
text-align: center;
position: relative;
line-height: 100px;
width: 100px;
height: 100px;
background-color: aqua;
}
span {}
.con {
position: absolute;
top: 0;
left: 0;
z-index: -1;
width: 200px;
height: 100px;
background-color: rgb(132, 0, 255);
}
</style>
</head>
<body>
<div class="silder">
<span>←</span>
<div class="con">問題反饋</div>
</div>
<script>
var silder = document.querySelector('.silder');
var con = document.querySelector('.con');
var span = document.querySelector('span');
silder.addEventListener('mouseenter', function () {
animate(con, -200, function () {
span.innerHTML = '→';
});
})
silder.addEventListener('mouseleave', function () {
animate(con, 0, function () {
span.innerHTML = '←';
});
})
</script>
</body>
</html>
寫在最后
到此這篇關(guān)于JavaScript動(dòng)畫原理之如何使用js進(jìn)行動(dòng)畫效果的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)js實(shí)現(xiàn)動(dòng)畫效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實(shí)現(xiàn)簡單的九宮格抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)簡單的九宮格抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
JavaScript相等運(yùn)算符的九條規(guī)則示例詳解
這篇文章主要給大家介紹了關(guān)于JavaScript相等運(yùn)算符的九條規(guī)則,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用JavaScript具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
javascript數(shù)組去重3種方法的性能測試與比較
面試題中有一題數(shù)組去重,首先想到的是對象存鍵值的方法可是遇到不同類型又能轉(zhuǎn)換成同樣的字符串的就完了接下來為大家介紹下雙重循環(huán)/存鍵值和類型實(shí)現(xiàn)去重,感興趣的各位可以參考下哈2013-03-03
javascript使用location.search的示例
本文介紹javascript 使用location.search獲取當(dāng)前地址欄參數(shù)的實(shí)例2013-11-11

