10
0

练习8_一道杠.html 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="box"></div>
  17. <script>
  18. var oBox = document.getElementsByClassName("box")[0];
  19. var timer1 = null;
  20. var timer2 = null;
  21. // 鼠标移入控制元素不断变长
  22. oBox.onmouseenter = function(){
  23. clearInterval(timer2);
  24. timer1 = setInterval(function(){
  25. oBox.style.width = (oBox.offsetWidth+5)+"px";
  26. if(oBox.offsetWidth>500){
  27. clearInterval(timer1);
  28. }
  29. },16)
  30. }
  31. // 鼠标移出后控制元素不断变短
  32. oBox.onmouseleave = function(){
  33. clearInterval(timer1);
  34. timer2 = setInterval(function(){
  35. oBox.style.width = (oBox.offsetWidth-5) + "px";
  36. if(oBox.offsetWidth<300){
  37. clearInterval(timer2)
  38. }
  39. },16)
  40. }
  41. </script>
  42. </body>
  43. </html>