example08.html 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <style>
  7. #adv {
  8. width: 200px;
  9. height: 200px;
  10. color: yellow;
  11. position: fixed;
  12. right: 10px;
  13. top: 10px;
  14. background-color: blue;
  15. }
  16. #adv button {
  17. float: right;
  18. border: none;
  19. outline: none;
  20. color: white;
  21. background-color: gray;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="adv">
  27. 此广告位招租
  28. <button>关闭</button>
  29. </div>
  30. <script>
  31. +function() {
  32. var advDiv = document.querySelector('#adv');
  33. var button = document.querySelector('#adv button');
  34. var counter = 0;
  35. button.addEventListener('click', function() {
  36. counter += 1;
  37. if (counter < 3) {
  38. var currentStyle =
  39. document.defaultView.getComputedStyle(advDiv);
  40. var newTop = parseInt(currentStyle.top) + 20;
  41. var newRight = parseInt(currentStyle.right) + 20;
  42. advDiv.style.top = newTop + 'px';
  43. advDiv.style.right = newRight + 'px';
  44. } else {
  45. advDiv.style.display = 'none';
  46. }
  47. });
  48. }();
  49. // 鼠标按下 - mousedown
  50. // 鼠标移动 - mousemove
  51. // 鼠标松开 - mouseup
  52. // clientX / clientY - 鼠标的横纵坐标
  53. </script>
  54. </body>
  55. </html>