8_页面滚动.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. .div1{
  9. width: 300px;
  10. height: 100px;
  11. border:5px solid green;
  12. overflow:scroll;
  13. }
  14. .div2{
  15. width: 1000px;
  16. height: 100px;
  17. background-image: linear-gradient( to right,red,blue);
  18. }
  19. ::-webkit-scrollbar{
  20. display: none;
  21. width: 0;
  22. height: 0;
  23. }
  24. .box{
  25. width: 100px;
  26. height: 1000px;
  27. background-image: linear-gradient(to bottom,red,green);
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="div1">
  33. <div class="div2"></div>
  34. </div>
  35. <div class="box">
  36. </div>
  37. <button onclick="backtop()">返回顶部</button>
  38. <script>
  39. window.onscroll = function(){
  40. // console.log("scroll");
  41. console.log("滚动距离顶部距离",document.documentElement.scrollTop);
  42. console.log("可滚动区域高度",document.documentElement.scrollHeight);
  43. console.log("视口高度",document.documentElement.clientHeight);
  44. }
  45. function backtop(){
  46. console.log("com")
  47. // window.scrollTo(0,0)
  48. window.scrollTo({
  49. top:0,
  50. behavior:"smooth"
  51. })
  52. }
  53. </script>
  54. </body>
  55. </html>