Following this article, we will learn how to create the mouse-follow effect today. When you open some web pages, there is an image following the mouse, and some of the images even are dynamic. This special effect can give the web viewers a deep impression and add the viewers. You must have been interested to this funny and special effect. Surely, it is very attracting. However, can we achieve this effect in our web pages? Yes, and it is very easy. Firstly, definite a doMouseMove() function; secondly, carry out it by document.Write. Now follow me.
Step1
Open your web page.
Step2
Because the effect is applied to the whole page, we put the codes in to the <head> sentence. The codes are like the following blue text.
- <script LANGUAGE="JavaScript">
- var newtop=0
- var newleft=0
- if (navigator.appName == "Netscape") {
- layerStyleRef="layer.";
- layerRef="document.layers";
- styleSwitch="";
- }
- else
- {
- layerStyleRef="layer.style.";
- layerRef="document.all";
- styleSwitch=".style";
- }
- function doMouseMove() {
- layerName = ‘iit’
- eval(’var curElement=’+layerRef+’["'+layerName+'"]‘)
- eval(layerRef+’["'+layerName+'"]‘+styleSwitch+’.visibility="hidden"’)
- eval(’curElement’+styleSwitch+’.visibility="visible"’)
- eval(’newleft=document.body.clientWidth-curElement’+styleSwitch+’.pixelWidth’)
- eval(’newtop=document.body.clientHeight-curElement’+styleSwitch+’.pixelHeight’)
- eval(’height=curElement’+styleSwitch+’.height’)
- eval(’width=curElement’+styleSwitch+’.width’)
- width=parseInt(width)
- height=parseInt(height)
- if (event.clientX > (document.body.clientWidth – 5 – width))
- {
- newleft=document.body.clientWidth + document.body.scrollLeft – 5 – width
- }
- else
- {
- newleft=document.body.scrollLeft + event.clientX
- }
- eval(’curElement’+styleSwitch+’.pixelLeft=newleft’)
- if (event.clientY > (document.body.clientHeight – 5 – height))
- {
- newtop=document.body.clientHeight + document.body.scrollTop – 5 – height
- }
- else
- {
- newtop=document.body.scrollTop + event.clientY
- }
- eval(’curElement’+styleSwitch+’.pixelTop=newtop’)
- }
- document.onmousemove = doMouseMove;
- </script>
You may be confused by the mass of codes. “var newtop” is to set the margin as 0. “function doMouseMove” is to definite a mouse-move function. Then there is a layer named as “iit”. And the mouse-move function is created in the layer named “iit”.
Step3
Now we need a “document.Write” sentence to carry it. Put the following codes into the <body> sentence.
- <script language="javascript">
- if (navigator.appName == "Netscape") {
- }
- else
- {
- document.write(’<div ID=OuterDiv>’)
- document.write(’<img ID=iit src="images/AP_42JJ9.png" STYLE="position:absolute;TOP:0pt;LEFT:0pt;width=61;height=65;Z-INDEX:2;visibility:hidden;"/>’)
- document.write(’</div>’)
- }
- </script>
Now let us learn about these codes. “src” is to set the image that follows mouse. here you can definite by yourself. But the image must be fitted for the style of the whole page.
Step4
Go back to the design view. Save the document and press F12 to preview in the Explorer. The mouse-follow effect is like the following picture showing:
Now you can see the beautiful and attracting effect that is created by yourself. You can practice it frequently. Of course, the codes in this article are a little boring. But we just need to remember how to use the doMouseMove() function and create a layer, then carry out it.

