2.Bom.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // 获取id命名的标签
  2. var btn1 = document.getElementById("btn1");
  3. var btn2 = document.getElementById("btn2");
  4. var a;
  5. // 点击事件
  6. btn1.onclick = function(){
  7. // 定时器
  8. a = setInterval(function(){
  9. // scrollTo(x,y) 相对于窗口
  10. // scrollBy(x,y) 相对于当前位置
  11. // scrollBy(0,200);
  12. // 滚动高度
  13. // document.body.scrollTop
  14. // document.documentElement.scrollTop
  15. // scrollBy(0,200)
  16. var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
  17. // console.log(scrollTop)
  18. if(scrollTop < 1799) {
  19. scrollBy(0,200);
  20. } else {
  21. clearInterval(a);
  22. window.alert("到底了");
  23. }
  24. },500)
  25. // setInterval(function(){},时间)
  26. }
  27. // var btn3 = document.getElementById("btn3");
  28. // btn3.onclick = function() {
  29. // // 清除定时器的方法 clearInterval()
  30. // clearInterval(a);
  31. // }
  32. btn2.onclick = function() {
  33. var b = setInterval(function(){
  34. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  35. if(scrollTop > 0) {
  36. scrollBy(0,-200);
  37. } else {
  38. clearInterval(b);
  39. alert("回到顶部了");
  40. }
  41. },1000)
  42. }