• We just launched and are currently in beta. Join us as we build and grow the community.

Eyes following the mouse using Javascript

urmomgay1231

Gaming Historian
Divine
U Rep
0
0
0
Rep
0
U Vouches
0
0
0
Vouches
0
Posts
94
Likes
92
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. The classic Xeyes in JavaScript
  3. (c) PROPIX Ltd, Written by Pintér Gábor
  4. Székesfehérvár, Kriványi u. 15.
  5. H-8000, HUNGARY
  6. Tel: +36 30 3489752
  7. Fax: +36 22 304326
  8. Email: [email protected]
  9. Web: http://www.propix.hu

  10. Revisions:
  11. V1.0 10/14/2001 Original release
  12. V1.1 02/20/2008: Updated by JavaScriptKit.com to work in the latest browsers (IE7, FF etc)

  13. Usage:
  14. 1. Include this file from the head of your page
  15. 2. Define parameters or accept the defaults
  16. 3. Insert the image

  17. This script requires Internet Explorer 5+ or Nescape Navigator 6+! In other browsers it does nothing.



  18. 1. Include jseyes.js from the head of your page
  19. Insert the following line into the head of your page:
  20. <script src="jseyes.js"></script>


  21. 2. Define parameters
  22. You can accept the defaults or assign new values to these variables:

  23. jseyesimg="jseyes.gif"
  24. The main image. Please do not edit.

  25. jseyeimg="jseyeblue.gif"
  26. The image of the eye. Must be a 21x29 solid ellipse with transparent background.


  27. 4. Insert the image
  28. Call jseyes() where you want to see the image:
  29. <script>
  30. jseyes();
  31. </script>

  32. Or call jseyes(x, y) to show the image at absolute position:
  33. <script>
  34. jseyes(100,100);
  35. </script>



  36. Example: http://www.propix.hu/www/jseyes/jseyes.html

  37. */




  38. // Defaults
  39. var

    jseyesimg=

    "images/jseyes.gif"

    ;
  40. var

    jseyeimg=

    "images/jseyeblue.gif"

    ;
  41. var

    jseyeslink=

    "http://www.javascriptkit.com"

    ;


  42. // Internal
  43. var

    jseyeso=

    null

    ,

    jseye1=

    null

    ,

    jseye2=

    null

    ;
  44. var

    standardbody=

    (

    document.compatMode

    ==

    "CSS1Compat"

    )

    ?

    document.documentElement

    :

    document.body

    //create reference to common "body" across doctypes

  45. // General utils

  46. // Find object by name or id
  47. function

    jseyesobj(

    id)

    {
  48. var

    i,

    x;
  49. x=

    document[

    id]

    ;
  50. if

    (

    !

    x &&

    document.getElementById

    )

    x=

    document.getElementById

    (

    id)

    ;
  51. for

    (

    i=

    0

    ;

    !

    x &&

    i<

    document.forms

    .length

    ;

    i++

    )

    x=

    document.forms

    [

    i]

    [

    id]

    ;
  52. return

    (

    x)

    ;
  53. }


  54. // Move eyes
  55. function

    jseyesmove(

    x,

    y)

    {
  56. var

    ex,

    ey,

    dx,

    dy;
  57. if

    (

    jseyeso &&

    jseye1 &&

    jseye2 &&

    jseyeso.style

    )

    {
  58. ex=

    jseyeso.offsetLeft

    +

    46

    ;

    ey=

    jseyeso.offsetTop

    +

    58

    ;
  59. dx=

    x-

    ex;

    dy=

    y-

    ey;
  60. r=

    (

    dx*

    dx/

    49

    +

    dy*

    dy/

    289

    <

    1

    )

    ?

    1

    :

    Math

    .sqrt

    (

    49

    *

    289

    /

    (

    dx*

    dx*

    289

    +

    dy*

    dy*

    49

    )

    )

    ;
  61. jseye1.style

    .left

    =

    r*

    dx+

    36.5

    +

    'px'

    ;

    jseye1.style

    .top

    =

    r*

    dy+

    44

    +

    'px'

    ;
  62. ex+=

    56

    ;

    dx-=

    56

    ;
  63. r=

    (

    dx*

    dx/

    49

    +

    dy*

    dy/

    289

    <

    1

    )

    ?

    1

    :

    Math

    .sqrt

    (

    49

    *

    289

    /

    (

    dx*

    dx*

    289

    +

    dy*

    dy*

    49

    )

    )

    ;
  64. jseye2.style

    .left

    =

    r*

    dx+

    92.5

    +

    'px'

    ;

    jseye2.style

    .top

    =

    r*

    dy+

    44

    +

    'px'

    ;
  65. }
  66. }



  67. // Main
  68. function

    jseyes(

    )

    {
  69. var

    img;
  70. var

    x,

    y,

    a=

    false

    ;

  71. if

    (

    arguments.length

    ==

    2

    )

    {
  72. x=

    arguments[

    0

    ]

    ;
  73. y=

    arguments[

    1

    ]

    ;
  74. a=

    true

    ;
  75. }

  76. img=

    "<div id='jseyeslayer' style='position:"

    +
  77. (

    a ?

    "absolute; left:"

    +

    x+

    "; top:"

    +

    y :

    "relative"

    )

    +
  78. "; z-index:5; width:150; height:150 overflow:hidden'>"

    +
  79. "<div id='jseye1' style='position:absolute; left:36; top:44; z-index:6; width:21; height:29'>"

    +
  80. "<img src='"

    +

    jseyeimg+

    "' width=21 height=29 onClick=\"

    location.href='"

    +

    jseyeslink+

    "'\"

    >"

    +
  81. "</div>"

    +
  82. "<div id='jseye2' style='position:absolute; left:92; top:44; z-index:6; width:21; height:29'>"

    +
  83. "<img src='"

    +

    jseyeimg+

    "' width=21 height=29 onClick=\"

    location.href='"

    +

    jseyeslink+

    "'\"

    >"

    +
  84. "</div>"

    +
  85. "<img src='"

    +

    jseyesimg+

    "' width=150 height=150 onClick=\"

    location.href='"

    +

    jseyeslink+

    "'\"

    >"

    +
  86. "</div>"

    ;
  87. document.write

    (

    img)

    ;
  88. jseyeso=

    jseyesobj(

    'jseyeslayer'

    )

    ;
  89. jseye1=

    jseyesobj(

    'jseye1'

    )

    ;
  90. jseye2=

    jseyesobj(

    'jseye2'

    )

    ;

  91. document.onmousemove

    =

    jseyesmousemove;
  92. }


  93. // Mouse move events

  94. function

    jseyesmousemove(

    e)

    {
  95. var

    mousex=

    (

    e)

    ?

    e.pageX

    :

    event.clientX

    +

    standardbody.scrollLeft
  96. var

    mousey=

    (

    e)

    ?

    e.pageY

    :

    event.clientY

    +

    standardbody.scrollTop
  97. jseyesmove(

    mousex,

    mousey)

    ;
  98. //return(false);
  99. }

Writing our html code

Note: Don't forget to link the javascript code named jseyes.js inside the head tag.
  1. <!DOCTYPE html>
  2. <head

    >

  3. <title

    >
  4. Xavier Eye
  5. </

    title

    >

  6. <style

    type

    =

    "text/css"

    >
  7. #styl {
  8. float: left;
  9. margin-left: 580px;
  10. margin-top: 200px;
  11. }
  12. </

    style

    >

  13. <script

    src

    =

    "js/jseyes.js"

    ></

    script

    >

  14. </

    head

    >

  15. <body

    >


  16. <div

    id

    =

    "styl"

    >
  17. <p

    >
  18. Move the cursor around into the<br

    /

    >

    image to see what the effect.
  19. </

    p

    >
  20. <script

    >
  21. jseyes();
  22. </

    script

    >
  23. </

    div

    >

  24. </

    body

    >

  25. </

    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.

 

442,401

317,942

317,951

Top