h5中控制元素滚动到指定位置。

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="utf-8">
    5. <style>
    6. #myDIV {
    7. height: 250px;
    8. width: 250px;
    9. overflow: auto;
    10. background: green;
    11. }
    12. #content {
    13. margin:500px;
    14. height: 800px;
    15. width: 2000px;
    16. background-color: coral;
    17. position: relative;
    18. }
    19. </style>
    20. </head>
    21. <body>
    22. <p>单击按钮以滚动到元素的顶部或底部</p>
    23. <button onclick="toTop()">滚动到元素的顶部</button>
    24. <button onclick="toBottom()">滚动到元素的底部</button>
    25. <button onclick="toTopTrans()">到顶部加动画</button>
    26. <div id="myDIV">
    27. <div id="content">
    28. <div style="position:absolute;top:0;">顶部</div>
    29. <div style="position:absolute;bottom:0">顶部</div>
    30. </div>
    31. </div>
    32. <script>
    33. var elmnt = document.getElementById("content");
    34. function toTop() {
    35. elmnt.scrollIntoView(true);
    36. }
    37. function toBottom() {
    38. elmnt.scrollIntoView(false);
    39. }
    40. function toTopTrans(){
    41. elmnt.scrollIntoView({
    42. behavior:'smooth'
    43. })
    44. }
    45. </script>
    46. </body>
    47. </html>