top按钮返回顶部

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>

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;
    }

js

    /* 
    需求:当页面滚动大于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
    })

要实现HTML悬浮按钮返回顶部的效果,你可以按照以下步骤进行操作。 首先,在HTML中添加两个div元素,一个用于显示顶部内容,另一个用于显示返回顶部按钮。为它们设置相应的class名和内容。 然后,在CSS中设置这两个元素的样式。对于顶部内容,可以设置宽度为100%,高度为80px,使用flex布局居中对齐文字,设置背景颜色和过渡效果,并将其定位为固定位置,初始时显示在页面顶部之外。 对于返回顶部按钮,可以设置宽度为50px,高度为50px,设置背景颜色和字体样式,并将其定位为固定位置,在页面底部右下角,默认隐藏。 接下来,在JavaScript中添加逻辑代码。使用滚动事件来判断浏览器卷去的高度是否大于等于一个临界点(比如300px),如果是,则设置顶部内容显示出来,返回顶部按钮显示出来;如果不是,则设置顶部内容隐藏,返回顶部按钮隐藏。 最后,在JavaScript中添加点击事件监听,当点击返回顶部按钮时,使用动画效果将页面滚动到顶部位置。 具体的代码实现可以参考以下示例代码。请注意,示例代码中的一些具体细节可能需要根据实际情况进行调整。 HTML代码: ``` <div class="header">顶部</div> <div class="goTop">回到顶部</div> ``` CSS代码: ``` .header { width: 100%; height: 80px; display: flex; justify-content: center; align-items: center; font-size: 30px; color: #fff; background-color: rgb(86, 226, 180); transition: top 0.5s linear; overflow: hidden; position: fixed; top: -80px; left: 0; } .goTop { width: 50px; height: 50px; background-color: pink; font-size: 20px; text-align: center; line-height: 25px; color: #fff; position: fixed; bottom: 50px; right: 50px; display: none; /* 默认隐藏 */ } ``` JavaScript代码: ``` document.addEventListener('DOMContentLoaded', function() { var header = document.querySelector('.header'); var goTop = document.querySelector('.goTop'); window.addEventListener('scroll', function() { var height = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop; if (height >= 300) { header.style.top = '0px'; goTop.style.display = 'block'; } else { header.style.top = '-80px'; goTop.style.display = 'none'; } }); goTop.addEventListener('click', function() { window.scrollTo({ top: 0, behavior: 'smooth' }); }); }); ``` 通过以上的HTMLCSSJavaScript代码,你可以实现一个HTML悬浮按钮返回顶部的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值