JS onmousemove鼠標(biāo)移動(dòng)坐標(biāo)接龍DIV效果實(shí)例
效果:
思路:
利用onmousemove事件,然后獲取鼠標(biāo)的坐標(biāo),之后把DIV挨個(gè)遍歷,最后把鼠標(biāo)的坐標(biāo)賦給DIV。
代碼:
<head runat="server">
<title></title>
<style type="text/css">
div
{
width: 20px;
height: 20px;
background: #00FFFF;
position: absolute;
}
</style>
<script type="text/javascript">
document.onmousemove = function (ev) {
var div = document.getElementsByTagName('div');
var oEvent = ev || event; //判斷兼容性
var pos = GetMouse(oEvent); //確定兼容性后,利用鼠標(biāo)移動(dòng)坐標(biāo)的函數(shù)來(lái)取得橫縱坐標(biāo)
for (var i = div.length - 1; i > 0; i--) { //遍歷DIV,從最后一個(gè)開(kāi)始。
div[i].style.left = div[i - 1].offsetLeft + 'px'; //將前一個(gè)的offsetLeft給后一個(gè)
div[i].style.top = div[i - 1].offsetTop + 'px'; //將前一個(gè)的offsetTop給后一個(gè)
}
div[0].style.left = pos.x + 'px'; //將鼠標(biāo)的橫坐標(biāo)給第一個(gè)
div[0].style.top = pos.y + 'px'; //將鼠標(biāo)的縱坐標(biāo)給第一個(gè)
}
function GetMouse(ev) { //獲取鼠標(biāo)移動(dòng)的坐標(biāo)
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
return { x: ev.clientX + scrollLeft, y: ev.clientY + scrollTop }
}
</script>
</head>
<body>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
<div>
</div>
</body>
- js實(shí)現(xiàn)鍵盤(pán)控制DIV移動(dòng)的方法
- js+html+css實(shí)現(xiàn)鼠標(biāo)移動(dòng)div實(shí)例
- 利用javascript移動(dòng)div層-javascript 拖動(dòng)層
- javascript DIV實(shí)現(xiàn)跟隨鼠標(biāo)移動(dòng)
- JavaScript與Div對(duì)層定位和移動(dòng)獲得坐標(biāo)的實(shí)現(xiàn)代碼
- javascript實(shí)現(xiàn)鼠標(biāo)點(diǎn)擊頁(yè)面 移動(dòng)DIV
- js拖動(dòng)div 當(dāng)鼠標(biāo)移動(dòng)時(shí)整個(gè)div也相應(yīng)的移動(dòng)
- js實(shí)現(xiàn)鍵盤(pán)操作實(shí)現(xiàn)div的移動(dòng)或改變的原理及代碼
- javascript實(shí)現(xiàn)點(diǎn)擊按鈕讓DIV層彈性移動(dòng)的方法
- JavaScript利用鍵盤(pán)碼控制div移動(dòng)
相關(guān)文章
實(shí)現(xiàn)抖音兩個(gè)旋轉(zhuǎn)小球的loading技巧實(shí)例
這篇文章主要為大家介紹了實(shí)現(xiàn)抖音兩個(gè)旋轉(zhuǎn)小球的loading技巧實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
javascript 組合按鍵事件監(jiān)聽(tīng)實(shí)現(xiàn)代碼
這篇文章主要介紹了javascript 組合按鍵事件監(jiān)聽(tīng)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-02-02
JavaScript遍歷查找數(shù)組中最大值與最小值的方法示例
這篇文章主要介紹了JavaScript遍歷查找數(shù)組中最大值與最小值的方法,結(jié)合實(shí)例形式分析了javascript基于數(shù)組遍歷、判斷實(shí)現(xiàn)最大值與最小值計(jì)算的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
JavaScript NodeTree導(dǎo)航欄(菜單項(xiàng)JSON類型/自制)
利用閑暇時(shí)間自己做了個(gè)JavaScript NodeTree,網(wǎng)上有很多類似的效果,本例主要是練練手,鞏固下知識(shí),感興趣的朋友可以了解下,或許對(duì)你學(xué)習(xí)NodeTree導(dǎo)航欄有所幫助,認(rèn)為好的就分享嘍2013-02-02
ionic js 復(fù)選框 與普通的 HTML 復(fù)選框到底有沒(méi)區(qū)別
本文通過(guò)實(shí)例給大家演示ionic js 復(fù)選框 與普通的 HTML 復(fù)選框到底有沒(méi)區(qū)別的相關(guān)知識(shí),非常不錯(cuò)具有參考借鑒價(jià)值,感興趣的朋友一起學(xué)習(xí)吧2016-06-06
javascript自動(dòng)生成包含數(shù)字與字符的隨機(jī)字符串
這篇文章主要介紹了javascript自動(dòng)生成包含數(shù)字與字符的隨機(jī)字符串,涉及Math.random()和Math.floor()兩個(gè)函數(shù)的使用技巧,需要的朋友可以參考下2015-02-02

