练习8_一道杠.html 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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: 200px;
  10. height: 50px;
  11. background-color: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="box"></div>
  17. <script>
  18. var oBox = document.getElementsByClassName("box")[0];
  19. // 控制递增定时器
  20. var timer1 = null;
  21. // 控制递减定时器
  22. var timer2 = null;
  23. oBox.onmouseover = function () {
  24. clearInterval(timer2);
  25. var _width = oBox.offsetWidth;
  26. timer1 = setInterval(function () {
  27. if (_width >= 600) {
  28. clearInterval(timer1);
  29. } else {
  30. _width += 10;
  31. oBox.style.width = _width + "px";
  32. }
  33. }, 16);
  34. }
  35. oBox.onmouseout = function(){
  36. clearInterval(timer1);
  37. var _width = oBox.offsetWidth;
  38. timer2 = setInterval(function(){
  39. if(_width <= 200){
  40. clearInterval(timer2);
  41. }else{
  42. _width -= 10;
  43. oBox.style.width = _width + "px";
  44. }
  45. },16)
  46. }
  47. </script>
  48. </body>
  49. </html>