index.js 517 B

123456789101112131415161718192021222324
  1. // 3s 把宽度从100px变为640px,即增加540px
  2. // 60帧/s, 3s 180帧,每次变化3px
  3. let curWidth = 100
  4. const maxWidth = 1000
  5. const $div1 = $("#div1")
  6. // setTimeout
  7. // function animate(){
  8. // curWidth += 3
  9. // $div1.css('width', curWidth)
  10. // if(curWidth < maxWidth) {
  11. // setTimeout(animate, 16.7); // 得自己控制时间
  12. // }
  13. // }
  14. // animate()
  15. function animate(){
  16. curWidth += 3
  17. $div1.css('width', curWidth)
  18. if(curWidth < maxWidth) {
  19. window.requestAnimationFrame(animate)
  20. }
  21. }
  22. animate()