javascript 實(shí)現(xiàn) 原路返回
更新時(shí)間:2015年01月21日 09:55:07 投稿:hebedich
這篇文章主要介紹了javascript 實(shí)現(xiàn)原路返回的方法,需要的朋友可以參考下
css代碼
復(fù)制代碼 代碼如下:
<style type="text/css">
* {
margin: 0px;
padding: 0px;
font-family: "micsoft yahei","微軟雅黑";
font-size: 15px;
}
div{
width: 50px;
height: 50px;
background: #f00;
border-radius:5px ;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
cursor: pointer;
position: absolute;
top: 60px;
left: 30px;
}
input{
position: absolute;
top: 10px;
left: 10px;
padding: 3px;
cursor: pointer;
}
</style>
html代碼
復(fù)制代碼 代碼如下:
<body>
<input type="button" value="原路返回"/>
<div></div>
</body>
javascript代碼
復(fù)制代碼 代碼如下:
<script type="text/javascript">
//1、以鼠標(biāo)在div上點(diǎn)擊觸發(fā)為開(kāi)始
//2、點(diǎn)擊鼠標(biāo)移動(dòng)時(shí)觸發(fā)鼠標(biāo)移動(dòng)事件 向數(shù)組內(nèi)注入數(shù)據(jù)(移動(dòng)的坐標(biāo))
//3、以鼠標(biāo)從div上移開(kāi)為結(jié)束
//4、點(diǎn)擊“原路返回”按鈕遍歷數(shù)組(移動(dòng)的坐標(biāo)) 定時(shí)觸發(fā)數(shù)組內(nèi)的坐標(biāo)移動(dòng)div 達(dá)到”返回“的功能
window.onload=function(){
var oDiv=document.getElementsByTagName("div")[0];
var oBtn=document.getElementsByTagName("input")[0];
var time=null,arrTop=[],arrLeft=[];
oDiv.onmousedown=function(ev){
var event=ev || window.event;
//獲取鼠標(biāo)在div內(nèi)的坐標(biāo)
var disX=event.clientX-oDiv.offsetLeft;
var disY=event.clientY-oDiv.offsetTop;
arrTop=[60];
arrLeft=[30];
document.onmousemove=function(ev){
var event=ev || window.event;
//獲取拖拽后鼠標(biāo)的位置
var l=event.clientX;
var t=event.clientY;
//把拖拽后的位置存進(jìn)數(shù)組里面,用拖拽后鼠標(biāo)的位置減去鼠標(biāo)在物體上的位置,就是拖拽后物體的位置
arrLeft.push(l-disX);
arrTop.push(t-disY);
oDiv.style.left=l-disX +"px";
oDiv.style.top=t-disY +"px";
};
document.onmouseup=function(){
document.onmousemove=null;
document.onmouseup=null;
};
return false;
}
//原路返回的核心就是把移動(dòng)時(shí)的坐標(biāo)記錄下來(lái),然后進(jìn)行數(shù)組重排,并設(shè)定定時(shí)器把數(shù)組內(nèi)的搞寬賦值給物體。
oBtn.onclick=function(){
arrTop.reverse();//重排
arrLeft.reverse();//重排
var i=0;
oBtn.time=setInterval(function(){
oDiv.style.top=arrTop[i]+"px";
oDiv.style.left=arrLeft[i]+"px";
i++;
if(i==arrTop.length){
clearInterval(oBtn.time);
arrTop=[];
arrLeft=[];
}
},20);
}
}
</script>
相關(guān)文章
js對(duì)象內(nèi)部訪問(wèn)this修飾的成員函數(shù)示例
這篇文章主要介紹了js對(duì)象內(nèi)部訪問(wèn)this修飾的成員函數(shù)示例,需要的朋友可以參考下2014-04-04
JavaScript arguments 多參傳值函數(shù)
在一個(gè)函數(shù)體內(nèi),標(biāo)識(shí)符arguments引用了arguments對(duì)象的一個(gè)特殊屬性??梢园凑諗?shù)目(而不是名字)獲取傳遞給函數(shù)的參數(shù)值。2010-10-10
getElementById().innerHTML與getElementById().value的區(qū)別
這篇文章主要介紹了getElementById().innerHTML與getElementById().value的區(qū)別,因?yàn)榻?jīng)常有新手朋友問(wèn)到,特整理一下,需要的朋友可以參考下2016-10-10
javascript學(xué)習(xí)筆記(二) js一些基本概念
javascript學(xué)習(xí)筆記之js一些基本概念,學(xué)習(xí)js的朋友可以參考下2012-06-06
javascript幾個(gè)易錯(cuò)點(diǎn)記錄
本文記錄了幾個(gè)平時(shí)在項(xiàng)目中使用javascript的易錯(cuò)的點(diǎn),時(shí)刻提醒自己不要再犯相同的錯(cuò)誤。2014-11-11
JavaScript實(shí)現(xiàn)表單驗(yàn)證
這篇文章介紹了JavaScript實(shí)現(xiàn)表單驗(yàn)證的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
TypeScript開(kāi)發(fā)環(huán)境安裝
這篇文章介紹了TypeScript開(kāi)發(fā)環(huán)境的安裝方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

