JS onmousemove Mouse Mobile coordinates solitaire div Effect example _javascript tips

Source: Internet
Author: User

Effect:

Ideas:

Use the OnMouseMove event, and then get the coordinates of the mouse, then the DIV traversal, and finally the mouse coordinates assigned to the Div.

Code:

Copy Code code as follows:

<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 Judging compatibility
var pos = getmouse (oevent); After determining the compatibility, use the mouse to move the coordinate function to obtain the horizontal ordinate
for (var i = div.length-1 i > 0; i--) {//Traversal div, starting from the last.
Div[i].style.left = div[i-1].offsetleft + ' px '; Offsetleft the previous one to the latter
Div[i].style.top = div[i-1].offsettop + ' px '; Offsettop the previous one to the latter
}
Div[0].style.left = pos.x + ' px '; The horizontal axis of the mouse is given to the first
Div[0].style.top = pos.y + ' px '; The ordinate of the mouse to the first
}
function Getmouse (EV) {//get the coordinates of the mouse movement
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>
<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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.