Advertisement
Guest User

fleeing button

a guest
Jan 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.14 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <title></title>
  5.     <meta charset="utf-8">
  6.  
  7.     <style>
  8.  
  9.     #button {
  10.         width:100px;
  11.         height:50px;
  12.         position:absolute;
  13.         top:100px;
  14.         left:100px;
  15.     }
  16.  
  17.     </style>
  18. </head>
  19. <body>
  20.  
  21.     <button id="button">Click Me!</button>
  22.  
  23. <script>
  24.     var button = document.getElementById("button");
  25.     var browserWidth = window.innerWidth || document.documentElement.clientWidth;
  26.     var browserHeight = window.innerHeight || document.documentElement.clientHeight;
  27.     var buttonWidth = button.offsetWidth;
  28.     var buttonHeight = button.offsetHeight;
  29.  
  30.     function move() {
  31.         button.style.left = Math.floor(Math.random()*(browserWidth-buttonWidth)) + "px";
  32.         button.style.top = Math.floor(Math.random()*(browserHeight-buttonHeight)) + "px";
  33.     }
  34.  
  35.     if(typeof addEventListener !== "undefined") {
  36.         button.addEventListener("mouseover", move, false);
  37.     } else if (typeof attachEvent !== "undefined") {
  38.         button.attachEvent("onmouseover", move);
  39.     } else {
  40.         button.onmousover = move;
  41.     }
  42.  
  43. </script>
  44. </body>
  45. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement