Div follows Mouse move

Source: Internet
Author: User

1, the goal is to achieve DIV follow the mouse and move, in three different situations to achieve

A) first get Div, to bind the mouse movement event, to the div to open the positioning function

The first implementation, if the body size is the same as the page size, you can use this method.

1) Get the coordinates of the mouse, and give the div coordinate assignment, realize as follows:

var left = Event.clientx;var top = Event.clienty;box1.style.left = left + "px"; box1.style.top = top + "px";

The second way, if the body height is greater than the visible height, then the scroll bar will appear, Clientxy get the visible size, then the extra parts will not be able to move, you need to use the following way to implement

However, this approach is incompatible with IE8, so if you want to be compatible with IE8, you cannot use it. Pagex is to get the entire page size

var left = Event.pagex;var top = event.pagey;

The third way is what can be compatible, covering the above two methods of feasibility, that is, with the visible window plus the height of the scrollbar can be

var st = Document.body.scrollTop | | Document.documentelement.scrolltop;var left = Event.clientx;var top = Event.clienty;box1.style.left = left + "px"; Box1.style.top = top + st+ "px";

Note that the left axis is the same, I just realized the y-axis here

Div follows Mouse move

Related Article

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.