3.小例子.js 730 B

1234567891011121314151617181920212223242526272829
  1. var box = document.querySelector(".box");
  2. var btn1 = document.getElementById("btn1");
  3. var btn2 = document.getElementById("btn2");
  4. btn1.onclick = function() {
  5. var timer1 = setInterval(function(){
  6. if (box.offsetWidth >= 250) {
  7. clearInterval(timer1);
  8. } else {
  9. box.style.width = box.offsetWidth + 10 + 'px'
  10. }
  11. },1000)
  12. }
  13. box.onclick = function() {
  14. console.log(box.offsetWidth,'宽度')
  15. }
  16. btn2.onclick = function() {
  17. var timer2 = setInterval(function(){
  18. if(box.offsetWidth <= 200) {
  19. clearInterval(timer2);
  20. console.log(box.offsetWidth)
  21. } else {
  22. box.style.width = box.offsetWidth - 10 + 'px';
  23. }
  24. },1000)
  25. }