JavaScript緩沖運動實現(xiàn)方法(2則示例)
本文實例講述了JavaScript緩沖運動實現(xiàn)方法。分享給大家供大家參考,具體如下:
實現(xiàn)原理:(目標(biāo)距離-當(dāng)前距離) / 基數(shù) = 速度(運動距離越大速度越小,運動距離和速度成反比)
需要注意:當(dāng)計算出來的速度有小數(shù)時需要取整;
例子1:滑塊緩沖運動
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>緩沖運動</title>
<style>
#div1{ width:100px; height:100px; background:red; position:absolute; top:50px; left:0;}
span{ width:1px; height:300px; background:black; position:absolute; left:300px; top:0; display:block;}
</style>
<script>
window.onload = function()
{
var oBtn = document.getElementById('btn1');
var oDiv = document.getElementById('div1');
oBtn.onclick = function()
{
startMove(oDiv, 300);
};
};
var timer = null;
function startMove(obj, iTarget)
{
clearInterval(timer);
timer = setInterval(function(){
var iSpeed = (iTarget - obj.offsetLeft)/8;
iSpeed = iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
if(iTarget==obj.offsetLeft){
clearInterval(timer);
}else{
obj.style.left = obj.offsetLeft + iSpeed + 'px';
}
}, 30);
}
</script>
</head>
<body>
<input id="btn1" type="button" value="移動" />
<div id="div1"></div>
<span></span>
</body>
</html>
例子2:側(cè)邊欄滑動
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>側(cè)邊欄滑動</title>
<style>
#div1{ width:100px; height:100px; background:red; position:absolute; right:0; top:0;}
</style>
<script>
window.onload = window.onscroll = function()
{
var oDiv = document.getElementById('div1');
var iScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
var clientHeight = document.documentElement.clientHeight;
var iH = (clientHeight - oDiv.offsetHeight)/2 + iScrollTop;
//oDiv.style.top = iH + 'px';
startMove(oDiv, parseInt(iH));
};
var timer = null;
function startMove(obj, iTarget)
{
clearInterval(timer);
timer = setInterval(function(){
var iSpeed = (iTarget - obj.offsetTop) / 8;
iSpeed = iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
if(obj.offsetTop == iTarget){
clearInterval(timer);
}else{
obj.style.top = obj.offsetTop + iSpeed + 'px';
}
}, 30);
}
</script>
</head>
<body style="height:2000px;">
<div id="div1"></div>
</body>
</html>
更多關(guān)于JavaScript運動效果相關(guān)內(nèi)容可查看本站專題:《JavaScript運動效果與技巧匯總》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
關(guān)于捕獲用戶何時點擊window.onbeforeunload的取消事件
關(guān)于捕獲用戶何時點擊window.onbeforeunload的取消事件的代碼,需要的朋友可以參考下。2011-03-03
詳解小程序中h5頁面onShow實現(xiàn)及跨頁面通信方案
這篇文章主要介紹了小程序中h5頁面onShow實現(xiàn)及跨頁面通信方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05
elementui更改el-dialog關(guān)閉按鈕的圖標(biāo)d的示例代碼
這篇文章主要介紹了elementui更改el-dialog關(guān)閉按鈕的圖標(biāo),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
JavaScript進制數(shù)之間的互相轉(zhuǎn)換
這篇文章主要介紹了JavaScript進制數(shù)之間的互相轉(zhuǎn)換,進制轉(zhuǎn)換是人們利用符號來計數(shù)的方法,下文基于JavaScript實現(xiàn)進制數(shù)之間的轉(zhuǎn)換,有一定的知識性參考價值,需要的小伙伴可以參考一下2022-05-05
javascript 設(shè)為首頁與加入收藏兼容多瀏覽器代碼
javascript 設(shè)為首頁與加入收藏兼容多瀏覽器代碼,不過由于ie7的特殊性,設(shè)為首頁不能使用,不過其它基于ie內(nèi)核的瀏覽器都是支持的。2011-01-01
javascript實現(xiàn)計算指定范圍內(nèi)的質(zhì)數(shù)示例
這篇文章主要介紹了javascript實現(xiàn)計算指定范圍內(nèi)的質(zhì)數(shù),涉及javascript數(shù)值計算與判斷相關(guān)操作技巧,需要的朋友可以參考下2018-12-12

