JavaScript拖拽、碰撞、重力及彈性運(yùn)動(dòng)實(shí)例分析
本文實(shí)例講述了JavaScript拖拽、碰撞、重力及彈性運(yùn)動(dòng)實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
js拖拽、碰撞與重力實(shí)現(xiàn)代碼:
window.onload=function ()
{
var oDiv=document.getElementById('div1');
var lastX=0;
var lastY=0;
oDiv.onmousedown=function (ev)
{
var oEvent=ev||event;
var disX=oEvent.clientX-oDiv.offsetLeft;
var disY=oEvent.clientY-oDiv.offsetTop;
document.onmousemove=function (ev)
{
var oEvent=ev||event;
var l=oEvent.clientX-disX;
var t=oEvent.clientY-disY;
oDiv.style.left=l+'px';
oDiv.style.top=t+'px';
iSpeedX=l-lastX;
iSpeedY=t-lastY;
lastX=l;
lastY=t;
document.title='x:'+iSpeedX+', y:'+iSpeedY;
};
document.onmouseup=function ()
{
document.onmousemove=null;
document.onmouseup=null;
startMove();
};
clearInterval(timer);
};
};
var timer=null;
var iSpeedX=0;
var iSpeedY=0;
function startMove()
{
clearInterval(timer);
timer=setInterval(function (){
var oDiv=document.getElementById('div1');
iSpeedY+=3;
var l=oDiv.offsetLeft+iSpeedX;
var t=oDiv.offsetTop+iSpeedY;
if(t>=document.documentElement.clientHeight-oDiv.offsetHeight)
{
iSpeedY*=-0.8;
iSpeedX*=0.8;
t=document.documentElement.clientHeight-oDiv.offsetHeight;
}
else if(t<=0)
{
iSpeedY*=-1;
iSpeedX*=0.8;
t=0;
}
if(l>=document.documentElement.clientWidth-oDiv.offsetWidth)
{
iSpeedX*=-0.8;
l=document.documentElement.clientWidth-oDiv.offsetWidth;
}
else if(l<=0)
{
iSpeedX*=-0.8;
l=0;
}
if(Math.abs(iSpeedX)<1)
{
iSpeedX=0;
}
if(Math.abs(iSpeedY)<1)
{
iSpeedY=0;
}
if(iSpeedX==0 && iSpeedY==0 && t==document.documentElement.clientHeight-oDiv.offsetHeight)
{
clearInterval(timer);
alert('停止');
}
else
{
oDiv.style.left=l+'px';
oDiv.style.top=t+'px';
}
document.title=iSpeedX;
}, 30);
}
js彈性運(yùn)動(dòng)實(shí)現(xiàn)代碼:
var left=0; //用left變量存儲(chǔ)賦給obj.style.left的值,以防每次系統(tǒng)都省略小數(shù),所導(dǎo)致最后結(jié)果的細(xì)微差異
var iSpeed=0;
function startMove(obj,iTarget)
{
clearInterval(obj.timer);
obj.timer=setInterval(function(){
iSpeed+=(iTarget-obj.offsetLeft)/5; //速度
iSpeed*=0.7; //考慮阻力
left+=iSpeed;
if(Math.abs(iSpeed)<1&&Math.abs(iTarget-obj.offsetLeft)<1) //停止條件 速度和距離絕對(duì)值小于1
{
clearInterval(obj.timer);
obj.style.left=iTarget+"px"; //清楚后,順便把目標(biāo)值賦給obj.style.left
}
else
{
obj.style.left=left+"px";
}
},30);
}
更多關(guān)于JavaScript運(yùn)動(dòng)效果相關(guān)內(nèi)容可查看本站專題:《JavaScript運(yùn)動(dòng)效果與技巧匯總》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
JS實(shí)現(xiàn)時(shí)間軸自動(dòng)播放
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)時(shí)間軸自動(dòng)播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Select標(biāo)簽下拉列表二級(jí)聯(lián)動(dòng)級(jí)聯(lián)實(shí)例代碼
這篇文章主要介紹了Select標(biāo)簽下拉列表二級(jí)聯(lián)動(dòng)級(jí)聯(lián)實(shí)例代碼,需要的朋友可以參考下2014-02-02
Ext JS 實(shí)現(xiàn)建議詞模糊動(dòng)態(tài)搜索功能
這篇文章主要介紹了Ext JS 實(shí)現(xiàn)建議詞模糊動(dòng)態(tài)搜索功能,需要的朋友可以參考下2017-05-05
認(rèn)識(shí)Knockout及如何使用Knockout綁定上下文
Knockout簡(jiǎn)稱ko,是一個(gè)輕量級(jí)的javascript類庫,采用MVVM設(shè)計(jì)模式(即Model、view、viewModel),簡(jiǎn)單優(yōu)雅的實(shí)現(xiàn)了雙向綁定,實(shí)時(shí)更新,幫助您使用干凈的數(shù)據(jù)模型來創(chuàng)建豐富的、響應(yīng)式的用戶界面2015-12-12
JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列原理與用法實(shí)例詳解
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列原理與用法,較為詳細(xì)的說明了隊(duì)列的概念、原理,并結(jié)合實(shí)例形式分析了javascript實(shí)現(xiàn)與使用隊(duì)列的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2017-11-11
關(guān)于IE瀏覽器以及Firefox下的javascript冒泡事件的響應(yīng)層級(jí)
原來是由于IE瀏覽器以及Firefox對(duì)于冒泡型事件的支持層次不同造成的。(如對(duì)冒泡事件不是很了解可先查詢相關(guān)資料)2010-10-10
uni-app使用swiper實(shí)現(xiàn)輪播圖的方法
做音樂播放器小程序時(shí),因?yàn)閟wiper的問題耽誤不少時(shí)間,所以下面這篇文章主要給大家介紹了關(guān)于uni-app使用swiper實(shí)現(xiàn)輪播圖的相關(guān)資料,需要的朋友可以參考下2022-11-11

