This article mainly introduces how to implement simple mouse-following effects in javascript, and involves javascript mouse events, coordinate acquisition, and other related techniques. It is of great practical value, for more information, see the examples in this article. 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:
Untitled documentScript window. onload = function () {var oTop = document. getElementById ("to_top"); document. onmousemove = function (evt) {var oEvent = evt | window. event; var scrollleft = document.doc umentElement. scrollLeft | document. body. scrollLeft; var scrolltop = document.doc umentElement. scrollTop | document. body. scrollTop; oTop. style. left = oEvent. clientX + scrollleft + 10 + "px"; oTop. style. top = oEvent. clientY + scrolltop + 10 + "px";} scriptTextMouse follow
I hope this article will help you design javascript programs.