19_js动画.html 899 B

12345678910111213141516171819202122232425262728293031323334
  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: 200px;
  11. background-color: red;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div class="box"></div>
  17. <button>点击动画</button>
  18. <script>
  19. // 获取元素
  20. var box = document.querySelector(".box");
  21. var btn = document.querySelector("button");
  22. // 绑定事件
  23. btn.onclick = function(){
  24. setInterval(function(){
  25. // 获取元素宽度
  26. var docWidth = box.offsetWidth;
  27. console.log(docWidth);
  28. box.style.width = (docWidth + 10) + "px";
  29. },16)
  30. // box.style.width = "400px";
  31. }
  32. </script>
  33. </body>
  34. </html>