1.认识BOM.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. </head>
  8. <body>
  9. <!-- <input type="button" > -->
  10. <!-- 按钮标签 -->
  11. <button id="down">往下走</button>
  12. <h2>哈哈哈</h2>
  13. <h2>哈哈哈</h2>
  14. <h2>哈哈哈</h2>
  15. <h2>哈哈哈</h2>
  16. <h2>哈哈哈</h2>
  17. <h2>哈哈哈</h2>
  18. <h2>哈哈哈</h2>
  19. <h2>哈哈哈</h2>
  20. <h2>哈哈哈</h2>
  21. <h2>哈哈哈</h2>
  22. <h2>哈哈哈</h2>
  23. <h2>哈哈哈</h2>
  24. <h2>哈哈哈</h2>
  25. <h2>哈哈哈</h2>
  26. <h2>哈哈哈</h2>
  27. <h2>哈哈哈</h2>
  28. <h2>哈哈哈</h2>
  29. <h2>哈哈哈</h2>
  30. <h2>哈哈哈</h2>
  31. <h2>哈哈哈</h2>
  32. <h2>哈哈哈</h2>
  33. <h2>哈哈哈</h2>
  34. <h2>哈哈哈</h2>
  35. <h2>哈哈哈</h2>
  36. <h2>哈哈哈</h2>
  37. <h2>哈哈哈</h2>
  38. <h2>哈哈哈</h2>
  39. <h2>哈哈哈</h2>
  40. <h2>哈哈哈</h2>
  41. <h2>哈哈哈</h2>
  42. <h2>哈哈哈</h2>
  43. <h2>哈哈哈</h2>
  44. <h2>哈哈哈</h2>
  45. <h2>哈哈哈</h2>
  46. <h2>哈哈哈</h2>
  47. <h2>哈哈哈</h2>
  48. <h2>哈哈哈</h2>
  49. <h2>哈哈哈</h2>
  50. <h2>哈哈哈</h2>
  51. <h2>哈哈哈</h2>
  52. <h2>哈哈哈</h2>
  53. <h2>哈哈哈</h2>
  54. <h2>哈哈哈</h2>
  55. <h2>哈哈哈</h2>
  56. <h2>哈哈哈</h2>
  57. <h2>哈哈哈</h2>
  58. <h2>哈哈哈</h2>
  59. <h2>哈哈哈</h2>
  60. <h2>哈哈哈</h2>
  61. <h2>哈哈哈</h2>
  62. <button id="up">往上走</button>
  63. <script>
  64. /**
  65. * js分为三个阶段
  66. * 1.BOM:Bowers Object Model 浏览器对象模型
  67. * 2.DOM:Document Object Model 文档对象模型
  68. * 3.ES:es6 es8 ...
  69. */
  70. // 1.获取id命名的标签 document.getElementById("id名称")
  71. var btn1 = document.getElementById("down");
  72. var btn2 = document.getElementById("up");
  73. // 2.点击事件
  74. // xxx.onclick = function() {}
  75. btn1.onclick = function() {
  76. //3.获取屏幕可滚动距离
  77. // console.log(document.documentElement.scrollTop)
  78. // console.log(document.body.scrollTop)
  79. // 4.定时器:在一定的时间内执行一次 循环执行
  80. // setInterval(function(){ console.log("每两秒指向")},2000)
  81. // 清除定时器:clearInterval();
  82. // 5.延时器:延迟一定的时间后执行 执行一次
  83. // setTimeout(function(){console.log("111")},2000)
  84. // 清除延时器:clearTimeout()
  85. // scrollTo(0,200); 相对于窗口滚动
  86. // scrollBy(0,300); 相对于自身滚动
  87. var timer = setInterval(function(){
  88. var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
  89. // scrollBy(0,200)
  90. // console.log(scrollTop);
  91. if(scrollTop <= 2400) {
  92. scrollBy(0,200)
  93. } else {
  94. alert("到底了");
  95. clearInterval(timer);
  96. }
  97. },100)
  98. }
  99. btn2.onclick = function() {
  100. var timer = setInterval(function(){
  101. var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  102. if(scrollTop > 0) {
  103. scrollBy(0,-200)
  104. } else {
  105. alert("到顶了");
  106. clearInterval(timer);
  107. }
  108. },300)
  109. }
  110. </script>
  111. </body>
  112. </html>