Js implements a simple method for mouse follow, js implements mouse follow

Source: Internet
Author: User

Js implements a simple method for mouse follow, js implements mouse follow

This article describes how to implement simple mouse-following in JavaScript. Share it with you for your reference. The specific analysis is as follows:

As the name implies, when the mouse moves, an animation moves along with the mouse.
 
Key Aspect 1:

var oEvent = evt || window.event;

This is written to be compatible with ie and ff. in ie, window. event indicates the event object, while in ff, a parameter is passed to the event function, which indicates the event object.

Key Aspect 2:

document.onmousemove = function(evt)

Mouse following occurs when the mouse moves.
 
Key Aspect 3:

document.documentElement.scrollTop || document.body.scrollTop;

This is to be compatible with chrome and other browsers. the scroll bar is the scroll distance from the top. chrome uses the back one, and other browsers use the front one.

Key Aspect 4:

oTop.style.top=oEvent.clientY+scrolltop+10+"px";

When the mouse moves, the current position of the mouse is assigned to the position value of the element.

OEvent. clientY is the position of the current Y coordinate of the mouse. The scrolltop distance is to be written without changing the effect of the mouse following when the scroll is not the first screen.
 
The Code is as follows:

<! DOCTYPE html> 

I hope this article will help you design javascript programs.

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.