JS實(shí)現(xiàn)多物體緩沖運(yùn)動實(shí)例代碼
更新時間:2013年11月29日 16:49:38 作者:
這篇文章主要介紹了JS實(shí)現(xiàn)多物體緩沖運(yùn)動實(shí)例代碼,有需要的朋友可以參考一下
效果:

思路:
利用setInterval計(jì)時器進(jìn)行運(yùn)動,offsetWidth實(shí)現(xiàn)寬度的變動,在用onmouseover將終點(diǎn)和所選中的DIV放入?yún)?shù)再進(jìn)行緩沖運(yùn)動。
代碼:
復(fù)制代碼 代碼如下:
<head runat="server">
<title></title>
<style type="text/css">
div
{
width: 100px;
height: 50px;
background: #0000FF;
margin: 10px;
}
</style>
<script type="text/javascript">
window.onload = function () {
var oDiv = document.getElementsByTagName('div');
for (var i = 0; i < oDiv.length; i++) {
oDiv[i].timer = null; //給買個DIV做個標(biāo)記,用以關(guān)閉相應(yīng)DIV的定時器
oDiv[i].onmouseover = function () {
move(this, 400); //給定時器輸出參數(shù)
}
oDiv[i].onmouseout = function () {
move(this, 100);
}
}
};
function move(div, end) {
clearInterval(div.timer);
div.timer = setInterval(function () {
var speed = (end - div.offsetWidth) / 5; //(終點(diǎn)-要走的寬度)/縮放系數(shù)=DIV移動的速度
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); //小數(shù)取整,也就是進(jìn)位取整
if (div.offsetWidth == end) { //當(dāng)?shù)竭_(dá)終點(diǎn)時關(guān)閉計(jì)時器
clearInterval(div.timer);
}
else {
div.style.width = div.offsetWidth + speed + 'px'; //移動DIV的寬度
}
}, 30)
}
</script>
</head>
<body>
<div>
</div>
<div>
</div>
<div>
</div>
</body>
您可能感興趣的文章:
- JavaScript實(shí)現(xiàn)多個物體同時運(yùn)動
- JS實(shí)現(xiàn)多物體運(yùn)動的方法詳解
- JS運(yùn)動改變單物體透明度的方法分析
- JS實(shí)現(xiàn)物體帶緩沖的間歇運(yùn)動效果示例
- JS多物體實(shí)現(xiàn)緩沖運(yùn)動效果示例
- js多個物體運(yùn)動功能實(shí)例分析
- javascript多物體運(yùn)動實(shí)現(xiàn)方法分析
- Javascript 多物體運(yùn)動的實(shí)現(xiàn)
- JS多物體 任意值 鏈?zhǔn)?緩沖運(yùn)動
- JS實(shí)現(xiàn)多物體運(yùn)動
相關(guān)文章
動態(tài)創(chuàng)建script在IE中緩存js文件時導(dǎo)致編碼的解決方法
這篇文章主要介紹了動態(tài)創(chuàng)建script在IE中緩存js文件時導(dǎo)致編碼的解決方法,需要的朋友可以參考下2014-05-05
laydate 顯示結(jié)束時間不小于開始時間的實(shí)例
下面小編就為大家?guī)硪黄猯aydate 顯示結(jié)束時間不小于開始時間的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
BootStrap模態(tài)框閃退問題實(shí)例代碼詳解
這篇文章主要介紹了BootStrap模態(tài)框閃退問題實(shí)例代碼詳解,需要的朋友可以參考下2018-12-12
淺析webpack 如何優(yōu)雅的使用tree-shaking(搖樹優(yōu)化)
這篇文章主要介紹了webpack 如何使用tree-shaking(搖樹優(yōu)化),本文介紹了什么是tree-shaking,commonJS 模塊,es6 模塊,怎么使用tree-shaking等,具體操作步驟大家可查看下文的詳細(xì)講解,感興趣的小伙伴們可以參考一下。2017-08-08

