练习9_三道杠.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. .box{
  9. width: 300px;
  10. height: 50px;
  11. background-color: red;
  12. margin-bottom: 20px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="box"></div>
  18. <div class="box"></div>
  19. <div class="box"></div>
  20. <script>
  21. var oBox = document.getElementsByClassName("box");
  22. var timer1 = null;
  23. var timer2 = null;
  24. // 方法一
  25. for(var i=0;i<3;i++){
  26. oBox[i].onmouseenter = function(){
  27. addWidthFun(this);
  28. }
  29. oBox[i].onmouseleave = function(){
  30. reduceWidthFun(this);
  31. }
  32. }
  33. function addWidthFun(obj){
  34. clearInterval(obj.timer2)
  35. obj.timer1 = setInterval(function(){
  36. if(obj.offsetWidth > 500){
  37. clearInterval(obj.timer1);
  38. return false;
  39. }
  40. obj.style.width = (obj.offsetWidth + 5) + "px";
  41. },16)
  42. }
  43. function reduceWidthFun(obj){
  44. clearInterval(obj.timer1)
  45. obj.timer2 = setInterval(function(){
  46. if(obj.offsetWidth < 300){
  47. clearInterval(obj.timer2);
  48. return false;
  49. }
  50. obj.style.width = (obj.offsetWidth - 5) +"px";
  51. },16)
  52. }
  53. // 方法二
  54. // 对象的方法内this指向的是当前对象,但如果方法内嵌套了函数那么函数内的this指向的是window
  55. // 当出现函数嵌套的时候, 内部函数可以使用外部函数的变量
  56. // for(var i=0;i<3;i++){
  57. // // 创建一个新属性timer给到每一个横向,用作控制变长的setInterval
  58. // oBox[i].timer = null;
  59. // oBox[i].onmouseenter = function(){
  60. // var that = this;
  61. // clearInterval(that.timer2)
  62. // // console.log(this);
  63. // that.timer = setInterval(function(){
  64. // // console.log(that);
  65. // that.style.width = (that.offsetWidth +5) +"px";
  66. // if(that.offsetWidth > 500){
  67. // clearInterval(that.timer)
  68. // }
  69. // },16)
  70. // }
  71. // }
  72. // for(var j=0;j<3;j++){
  73. // // 创建新属性timer2给到每一个横向,用作控制变短的setInterval
  74. // oBox[j].timer2 = null;
  75. // oBox[j].onmouseleave = function(){
  76. // var that = this;
  77. // clearInterval(that.timer)
  78. // that.timer2 = setInterval(function(){
  79. // that.style.width = (that.offsetWidth - 5) + "px";
  80. // if(that.offsetWidth<300){
  81. // clearInterval(that.timer2)
  82. // }
  83. // },16)
  84. // }
  85. // }
  86. // var a = 0;
  87. // a = 1;
  88. // a = 2;
  89. // a = 3;
  90. // console.log(a);
  91. // 鼠标移入控制元素不断变长
  92. // oBox.onmouseenter = function(){
  93. // clearInterval(timer2);
  94. // timer1 = setInterval(function(){
  95. // oBox.style.width = (oBox.offsetWidth+5)+"px";
  96. // if(oBox.offsetWidth>500){
  97. // clearInterval(timer1);
  98. // }
  99. // },16)
  100. // }
  101. // // 鼠标移出后控制元素不断变短
  102. // oBox.onmouseleave = function(){
  103. // clearInterval(timer1);
  104. // timer2 = setInterval(function(){
  105. // oBox.style.width = (oBox.offsetWidth-5) + "px";
  106. // if(oBox.offsetWidth<300){
  107. // clearInterval(timer2)
  108. // }
  109. // },16)
  110. // }
  111. </script>
  112. </body>
  113. </html>