js回到底部和顶部

回到顶部:document.documentElement.scrollTop = 0;
回到底部:window.scrollTo( 0, document.documentElement.scrollHeight - document.documentElement.clientHeight );

下面一个简单的案例演示一下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
      }
      div {
        width: 300px;
        height: 1000px;
        background: linear-gradient(skyblue, red);
        display: flex;
        flex-direction: column;
        justify-content: space-between;
      }
    </style>
  </head>
  <body>
    <div>
      <button onclick="clickToBottom()">回到底部</button>
      <button onclick="clickToTop()">回到顶部</button>
    </div>

    <script>
      function clickToBottom() {
        window.scrollTo(
          0,
          document.documentElement.scrollHeight -
            document.documentElement.clientHeight
        );
      }

      function clickToTop() {
        document.documentElement.scrollTop = 0;
      }
    </script>
  </body>
</html>

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <title>一键回到顶部</title> <style> #back-to-top { display: none; position: fixed; bottom: 20px; right: 20px; cursor: pointer; width: 50px; height: 50px; background-color: #555; color: #fff; font-size: 30px; text-align: center; line-height: 50px; border-radius: 50%; transition: background-color 0.3s ease-in-out; } #back-to-top:hover { background-color: #666; } </style> </head> <body> <div id="content"> <!-- 此处为页面内容 --> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque vel sagittis lectus, quis tristique sapien. Nunc pretium arcu ut enim laoreet, eu tristique diam bibendum. Proin in pretium augue. Sed ante augue, mollis at pharetra in, dictum vel elit. Integer vel massa faucibus, bibendum ante sed, ultricies odio. Sed auctor, quam ac malesuada facilisis, sem odio varius nulla, nec molestie nisl leo vel elit. Donec blandit, quam a tincidunt facilisis, tellus ipsum rhoncus mi, id fringilla velit odio eget enim.</p> </div> <div id="back-to-top" title="回到顶部">^</div> <script> var backToTop = document.getElementById("back-to-top"); window.onscroll = function() { if (document.documentElement.scrollTop > 100) { backToTop.style.display = "block"; } else { backToTop.style.display = "none"; } } backToTop.onclick = function() { document.documentElement.scrollTop = 0; } </script> </body> </html> ``` 这段代码中,我们首先使用 CSS 样式定义了一个“回到顶部”按钮,然后在页面底部添加一个 `<div>` 元素作为按钮,同时使用 JavaScript 监听 `window` 对象的 `onscroll` 事件来判断是否应该显示按钮。当用户单击按钮时,我们再使用 JavaScript 将滚动条位置设置为 0,即可实现一键回到顶部的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值