html一键返回顶部功能

1.HTML部分

代码如下(示例):

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>回到顶部</title>
</head>
<body>
  <!-- 回到顶部按钮 -->
  <div class="backTop" id="backTop">Top</div>
</body>
</html>

2.CSS

代码如下(示例):

/* 设置body高度为 3000px,页面就有滚动条显示啦 */
    body {
      height: 3000px;
    }
 
    /* 回到顶部 */
    .backTop {
      /* 固定定位 */
      position: fixed;
      bottom: 250px;
      right: 0;
      margin-right: 20px;
      z-index: 999;
      /* 回到顶部样式 */
      width: 35px;
      height: 35px;
      background-color: deepskyblue;
      /* 字体在元素中水平垂直居中 */
      line-height: 35px;
      text-align: center;
      font-size: 14px;
      color: white;
      /* 设置圆角 */
      border-radius: 50%;
      /* 设置过渡效果 */
      transition: all linear 0.5s;
      cursor: pointer;
      /* 隐藏 透明度为0 */
      opacity: 0;
    }

3.JavaScrip

代码如下(示例):

    /* 
    需求:当页面滚动大于300像素,就显示回到顶部按钮
    */
    // 获取回到顶部元素
    const backTop = document.querySelector('#backTop')
    // L2 事件监听
    // 给页面添加 滚动事件
    window.addEventListener('scroll', function () {
      // 检测页面滚动的头部距离属性
      const n = document.documentElement.scrollTop
       // 三元运算符 简写判断
      backTop.style.opacity = n >= 300 ? 1 : 0
    })
    // 需求:点击返回按钮,页面会返回顶部
    // 给回到顶部元素添加 点击事件
    backTop.addEventListener('click',function () {
      document.documentElement.scrollTop = 0
    })

4.整体代码

<!DOCTYPE html>
<html lang="zh-CN">
 
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>回到顶部</title>
  <style>
    /* 设置body高度为 3000px,页面就有滚动条显示啦 */
    body {
      height: 3000px;
    }
 
    /* 回到顶部 */
    .backTop {
      /* 固定定位 */
      position: fixed;
      bottom: 250px;
      right: 0;
      margin-right: 20px;
      z-index: 999;
      /* 回到顶部样式 */
      width: 35px;
      height: 35px;
      background-color: deepskyblue;
      /* 字体在元素中水平垂直居中 */
      line-height: 35px;
      text-align: center;
      font-size: 14px;
      color: white;
      /* 设置圆角 */
      border-radius: 50%;
      /* 设置过渡效果 */
      transition: all linear 0.5s;
      cursor: pointer;
      /* 隐藏 透明度为0 */
      opacity: 0;
    }
  </style>
</head>
 
<body>
  <div class="backTop" id="backTop">Top</div>
  <script>
    /* 
    需求:当页面滚动大于300像素,就显示回到顶部按钮
    */
    // 获取回到顶部元素
    const backTop = document.querySelector('#backTop')
    // L2 事件监听
    // 给页面添加 滚动事件
    window.addEventListener('scroll', function () {
      // 检测页面滚动的头部距离属性
      const n = document.documentElement.scrollTop
      backTop.style.opacity = n >= 300 ? 1 : 0
    })
    // 需求:点击返回按钮,页面会返回顶部
    // 给回到顶部元素添加 点击事件
    backTop.addEventListener('click',function () {
      document.documentElement.scrollTop = 0
    })
  </script>
</body>
 
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值