Eyes following the mouse using Javascript

Language

In this tutorial, I am going to teach you how to make a xavier eye or eyes following the eye using javascript. Just follow the instructions below.

Instructions

Writing our javascript code and name it as jseyes.js

  1. /* jseyes.js
  2.  
  3.  
  4. The classic Xeyes in JavaScript
  5. (c) PROPIX Ltd, Written by Pintér Gábor
  6. Székesfehérvár, Kriványi u. 15.
  7. H-8000, HUNGARY
  8. Tel: +36 30 3489752
  9. Fax: +36 22 304326
  10. Web: http://www.propix.hu
  11.  
  12. Revisions:
  13.   V1.0 10/14/2001 Original release
  14. V1.1 02/20/2008: Updated by JavaScriptKit.com to work in the latest browsers (IE7, FF etc)
  15.  
  16. Usage:
  17.   1. Include this file from the head of your page
  18.   2. Define parameters or accept the defaults
  19.   3. Insert the image
  20.  
  21. This script requires Internet Explorer 5+ or Nescape Navigator 6+! In other browsers it does nothing.
  22.  
  23.  
  24.  
  25. 1. Include jseyes.js from the head of your page
  26. Insert the following line into the head of your page:
  27.   <script src="jseyes.js"></script>
  28.  
  29.  
  30. 2. Define parameters
  31. You can accept the defaults or assign new values to these variables:
  32.  
  33. jseyesimg="jseyes.gif"
  34.   The main image. Please do not edit.
  35.  
  36. jseyeimg="jseyeblue.gif"
  37.   The image of the eye. Must be a 21x29 solid ellipse with transparent background.
  38.  
  39.  
  40. 4. Insert the image
  41. Call jseyes() where you want to see the image:
  42.   <script>
  43.   jseyes();
  44.   </script>
  45.  
  46. Or call jseyes(x, y) to show the image at absolute position:
  47.   <script>
  48.   jseyes(100,100);
  49.   </script>
  50.  
  51.  
  52.  
  53. Example: http://www.propix.hu/www/jseyes/jseyes.html
  54.  
  55. */
  56.  
  57.  
  58.  
  59.  
  60. // Defaults
  61. var jseyesimg="images/jseyes.gif";
  62. var jseyeimg="images/jseyeblue.gif";
  63. var jseyeslink="http://www.javascriptkit.com";
  64.  
  65.  
  66. // Internal
  67. var jseyeso=null, jseye1=null, jseye2=null;
  68. var standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
  69.  
  70. // General utils
  71.  
  72. // Find object by name or id
  73. function jseyesobj(id) {
  74. var i, x;
  75. x= document[id];
  76. if (!x && document.getElementById) x= document.getElementById(id);
  77. for (i=0; !x && i<document.forms.length; i++) x= document.forms[i][id];
  78. return(x);
  79. }
  80.  
  81.  
  82. // Move eyes
  83. function jseyesmove(x, y) {
  84. var ex, ey, dx, dy;
  85. if (jseyeso && jseye1 && jseye2 && jseyeso.style) {
  86. ex=jseyeso.offsetLeft+46; ey=jseyeso.offsetTop+58;
  87. dx=x-ex; dy=y-ey;
  88. r=(dx*dx/49+dy*dy/289<1) ? 1 : Math.sqrt(49*289/(dx*dx*289+dy*dy*49));
  89. jseye1.style.left= r*dx+36.5+'px'; jseye1.style.top= r*dy+44+'px';
  90. ex+=56; dx-=56;
  91. r=(dx*dx/49+dy*dy/289<1) ? 1 : Math.sqrt(49*289/(dx*dx*289+dy*dy*49));
  92. jseye2.style.left= r*dx+92.5+'px'; jseye2.style.top= r*dy+44+'px';
  93. }
  94. }
  95.  
  96.  
  97.  
  98. // Main
  99. function jseyes() {
  100. var img;
  101. var x, y, a=false;
  102.  
  103. if (arguments.length==2) {
  104. x= arguments[0];
  105. y= arguments[1];
  106. a= true;
  107. }
  108.  
  109. img= "<div id='jseyeslayer' style='position:"+
  110. (a ? "absolute; left:"+x+"; top:"+y : "relative")+
  111. "; z-index:5; width:150; height:150 overflow:hidden'>"+
  112. "<div id='jseye1' style='position:absolute; left:36; top:44; z-index:6; width:21; height:29'>"+
  113. "<img src='"+jseyeimg+"' width=21 height=29 onClick=\"location.href='"+jseyeslink+"'\">"+
  114. "</div>"+
  115. "<div id='jseye2' style='position:absolute; left:92; top:44; z-index:6; width:21; height:29'>"+
  116. "<img src='"+jseyeimg+"' width=21 height=29 onClick=\"location.href='"+jseyeslink+"'\">"+
  117. "</div>"+
  118. "<img src='"+jseyesimg+"' width=150 height=150 onClick=\"location.href='"+jseyeslink+"'\">"+
  119. "</div>";
  120. document.write(img);
  121. jseyeso=jseyesobj('jseyeslayer');
  122. jseye1=jseyesobj('jseye1');
  123. jseye2=jseyesobj('jseye2');
  124.  
  125. document.onmousemove=jseyesmousemove;
  126. }
  127.  
  128.  
  129. // Mouse move events
  130.  
  131. function jseyesmousemove(e) {
  132. var mousex=(e)? e.pageX : event.clientX+standardbody.scrollLeft
  133. var mousey=(e)? e.pageY : event.clientY+standardbody.scrollTop
  134. jseyesmove(mousex, mousey);
  135. //return(false);
  136. }

Writing our html code

Note: Don't forget to link the javascript code named jseyes.js inside the head tag.
  1. <!DOCTYPE html>
  2.  
  3. Xavier Eye
  4.  
  5. <style type="text/css">
  6. #styl {
  7. float: left;
  8. margin-left: 580px;
  9. margin-top: 200px;
  10. }
  11.  
  12. <script src="js/jseyes.js"></script>
  13.  
  14. </head>
  15.  
  16.  
  17.  
  18. <div id="styl">
  19. <p>
  20. Move the cursor around into the<br /> image to see what the effect.
  21. </p>
  22. jseyes();
  23. </div>
  24.  
  25. </body>
  26.  
  27. </html>
Congratulations, you have finally created xavier eyes or eyes following mouse that you can use in your systems or projects. For comment and suggestions, feel free to comment below or email me at [email protected]

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment