JS div勻速移動(dòng)動(dòng)畫與變速移動(dòng)動(dòng)畫代碼實(shí)例
1.勻速移動(dòng)代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
margin-top: 20px;
width: 200px;
height: 100px;
background-color: green;
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<input type="button" value="移動(dòng)到400px" id="btn1"/>
<input type="button" value="移動(dòng)到800px" id="btn2"/>
<div id="dv">
<script src="common.js"></script>
<script>
//點(diǎn)擊按鈕移動(dòng)div
my$("btn1").onclick = function () {
animate(my$("dv"), 400);
};
my$("btn2").onclick = function () {
animate(my$("dv"), 800);
};
//勻速動(dòng)畫
function animate(element, target) {
//清理定時(shí)器
clearInterval(element.timeId);
element.timeId = setInterval(function () {
//獲取元素的當(dāng)前位置
var current = element.offsetLeft;
//移動(dòng)的步數(shù)
var step = 10;
step = target > current ? step : -step;
current += step;
if (Math.abs(current - target) > Math.abs(step)) {
element.style.left = current + "px";
} else {
clearInterval(element.timeId);
element.style.left = target + "px";
}
}, 20);
}
</script>
</div>
</body>
</html>
2.變速移動(dòng)代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>title</title>
<style>
* {
margin: 0;
padding: 0;
}
div {
margin-top: 20px;
width: 200px;
height: 100px;
background-color: green;
position: absolute;
left: 0;
top: 0;
}
</style>
</head>
<body>
<input type="button" value="移動(dòng)到400px" id="btn1"/>
<input type="button" value="移動(dòng)到800px" id="btn2"/>
<div id="dv">
<script src="common.js"></script>
<script>
//點(diǎn)擊按鈕移動(dòng)div
my$("btn1").onclick = function () {
animate(my$("dv"), 400);
};
my$("btn2").onclick = function () {
animate(my$("dv"), 800);
};
//變速動(dòng)畫
function animate(element, target) {
//清理定時(shí)器
clearInterval(element.timeId);
element.timeId = setInterval(function () {
//獲取元素的當(dāng)前位置
var current = element.offsetLeft;
//移動(dòng)的步數(shù)
var step = (target-current)/10;
step = step>0?Math.ceil(step):Math.floor(step);
current += step;
element.style.left = current + "px";
if(current==target) {
//清理定時(shí)器
clearInterval(element.timeId);
}
}, 20);
}
</script>
</div>
</body>
</html>
common.js
/**
* 獲取指定標(biāo)簽對(duì)象
* @param id 標(biāo)簽的id屬性值
* @returns {Element}根據(jù)id屬性值返回指定標(biāo)簽對(duì)象
*/
function my$(id) {
return document.getElementById(id);
}
以上所述是小編給大家介紹的JS div勻速移動(dòng)動(dòng)畫與變速移動(dòng)動(dòng)畫詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
uniapp返回上一頁(yè)執(zhí)行上一頁(yè)方法解決方案
這篇文章主要給大家介紹了關(guān)于uniapp返回上一頁(yè)執(zhí)行上一頁(yè)方法解決方案,在UniApp中可以通過uni.navigateBack()方法返回上一個(gè)頁(yè)面,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
JavaScript中Cookies的相關(guān)使用教程
這篇文章主要介紹了JavaScript中Cookies的相關(guān)使用教程,包括Cookies的存儲(chǔ)和刪除等操作方法,需要的朋友可以參考下2015-06-06
javascript數(shù)組里的27個(gè)方法總合詳解
這篇文章主要為大家詳細(xì)介紹了javascript數(shù)組里的方法27個(gè)總合,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02
JavaScript設(shè)計(jì)模式開發(fā)中組合模式的使用教程
組合模式可以理解為樹狀結(jié)構(gòu),因此組合模式適合對(duì)大批對(duì)象的操作,特別是層次結(jié)構(gòu)分明的,下面我們就來看看號(hào)稱面向?qū)ο蟮腏avaScript設(shè)計(jì)模式開發(fā)中組合模式的使用教程2016-05-05
一個(gè)簡(jiǎn)單的網(wǎng)站訪問JS計(jì)數(shù)器 刷新1次加1次訪問
一個(gè)簡(jiǎn)單的網(wǎng)站訪問JS計(jì)數(shù)器,一般就是學(xué)習(xí)下原來,不建議使用,現(xiàn)在cnzz或百度統(tǒng)計(jì)多試不錯(cuò)的2012-09-09

