JS實(shí)現(xiàn)添加緩動(dòng)畫的方法
本文實(shí)例為大家分享了JS實(shí)現(xiàn)添加緩動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
在看這篇博客之前需要了解JS實(shí)現(xiàn)給不同元素設(shè)置不同的定時(shí)器
需要實(shí)現(xiàn)的效果:點(diǎn)擊移動(dòng)到600按鈕之后下面的div會(huì)由快到慢移動(dòng)到600px,點(diǎn)擊移動(dòng)到800之后又會(huì)移動(dòng)到800px,又點(diǎn)擊移動(dòng)600的時(shí)候會(huì)倒回去移動(dòng)到600px。
首先需要實(shí)現(xiàn)第一個(gè)功能:
1.緩動(dòng)畫實(shí)現(xiàn),緩動(dòng)畫實(shí)現(xiàn)思路如下:
2.需要避免小數(shù)的出現(xiàn),如果直接將上面的公式作為距離的話會(huì)出現(xiàn)小數(shù),如果移動(dòng)的距離是正數(shù)的話需要向上取整,如果移動(dòng)的距離是負(fù)數(shù)(比如由800px移動(dòng)到600px)的話需要向下取整。
完整代碼:
<!DOCTYPE html> <html lang="en"> ? <head> ? ? <meta charset="UTF-8"> ? ? <meta http-equiv="X-UA-Compatible" content="IE=edge"> ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0"> ? ? <title>緩動(dòng)畫</title> ? ? <style> ? ? ? ? div { ? ? ? ? ? ? position: absolute; ? ? ? ? ? ? top: 50px; ? ? ? ? ? ? left: 0px; ? ? ? ? ? ? width: 200px; ? ? ? ? ? ? height: 200px; ? ? ? ? ? ? background-color: pink; ? ? ? ? } ? ? </style> ? ? <script> ? ? ? ? window.addEventListener('load', function() { ? ? ? ? ? ? function move(obj, target, callback) { ? ? ? ? ? ? ? ? window.clearInterval(obj.timer); ? ? ? ? ? ? ? ? obj.timer = window.setInterval(function() { ? ? ? ? ? ? ? ? ? ? // 把步長值改為整數(shù),不要出現(xiàn)小數(shù)的情況 往上取整 ? ? ? ? ? ? ? ? ? ? ? var step = (target - obj.offsetLeft) / 10; ? ? ? ? ? ? ? ? ? ? step = step > 0 ? Math.ceil(step) : Math.floor(step); ? ? ? ? ? ? ? ? ? ? // 回調(diào)函數(shù)寫在清除定時(shí)器里面 ? ? ? ? ? ? ? ? ? ? if (obj.offsetLeft >= target) { ? ? ? ? ? ? ? ? ? ? ? ? window.clearInterval(obj.timer); ? ? ? ? ? ? ? ? ? ? ? ? if (callback) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? callback(); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? ? ? obj.style.left = obj.offsetLeft + step + 'px'; ? ? ? ? ? ? ? ? ? ? ? ? // console.log(obj.style.left); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }, 15) ? ? ? ? ? ? } ? ? ? ? ? ? var div = document.querySelector('div'); ? ? ? ? ? ? var btn600 = document.querySelector('.btn600'); ? ? ? ? ? ? btn600.addEventListener('click', function() { ? ? ? ? ? ? ? ? move(div, 600) ? ? ? ? ? ? }) ? ? ? ? ? ? var btn600 = document.querySelector('.btn800'); ? ? ? ? ? ? btn600.addEventListener('click', function() { ? ? ? ? ? ? ? ? move(div, 800, function() { ? ? ? ? ? ? ? ? ? ? div.style.backgroundColor = 'violet' ? ? ? ? ? ? ? ? }) ? ? ? ? ? ? }) ? ? ? ? }) ? ? </script> </head> ? <body> ? ? <button class="btn600">點(diǎn)擊移動(dòng)600</button> ? ? <button class="btn800">點(diǎn)擊移動(dòng)800</button> ? ? <div></div> </body> ? </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 原生js實(shí)現(xiàn)頁面滾動(dòng)動(dòng)畫
- 十個(gè)利用JavaScript實(shí)現(xiàn)的愛心動(dòng)畫特效
- js實(shí)現(xiàn)帶翻轉(zhuǎn)動(dòng)畫圖片時(shí)鐘
- 關(guān)于JavaScript實(shí)現(xiàn)動(dòng)畫時(shí)動(dòng)畫抖動(dòng)的原因與解決方法
- JavaScript實(shí)現(xiàn)瀑布動(dòng)畫
- JS使用window.requestAnimationFrame()實(shí)現(xiàn)逐幀動(dòng)畫
- JavaScript實(shí)現(xiàn)扯網(wǎng)動(dòng)畫效果的示例代碼
- 如何利用JS實(shí)現(xiàn)時(shí)間軸動(dòng)畫效果
- JavaScript+CSS實(shí)現(xiàn)唯美蝴蝶動(dòng)畫
相關(guān)文章
js?html5獲取input焦點(diǎn)的輸入框并賦值實(shí)例
這篇文章主要為大家介紹了js?html5獲取input焦點(diǎn)的輸入框并賦值實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10JS.GetAllChild(element,deep,condition)使用介紹
JS.GetAllChild()獲取所有子節(jié)點(diǎn),想必大家都知道吧,具體的使用方法如下,感興趣的朋友可以參考下2013-09-09three.js 利用uv和ThreeBSP制作一個(gè)快遞柜功能
這篇文章主要介紹了three.js 利用uv和ThreeBSP制作一個(gè)快遞柜,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08JS路由跳轉(zhuǎn)的簡單實(shí)現(xiàn)代碼
本文給大家分享一個(gè)簡單的js路由跳轉(zhuǎn)功能,非常不錯(cuò),需要的朋友參考下吧2017-09-09JavaScript實(shí)現(xiàn)圖片DIV豎向滑動(dòng)的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)圖片DIV豎向滑動(dòng)的方法,涉及javascript操作div層的相關(guān)技巧,需要的朋友可以參考下2015-04-04