网页回到顶部案列同时

回到顶部:

      确认需求:

  1. 滚动条滚动超过临界点,顶部通栏显示,否则隐藏
  2. 滚动条超过临界点时,回到顶部按钮显示,否则隐藏
  3. 点击回到顶部按钮,滚动条滚动回到顶部

源码如下:

<!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;
            padding: 0;
        }
        body{
            height: 3000px;
        }
        .header{
            width: 100%;
            height: 80px;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 30px;
            color: white;
            background-color: skyblue;
            position: fixed;
            top: -80px;
            left: 0;
            transition: top .5s linear;
        }
        .goTop{
            width: 50px;
            height: 50px;
            background-color: pink;
            font-size: 20px;
            text-align: center;
            line-height: 25px;
            color: white;
            position: fixed;
            bottom: 50px;
            right: 50px;
         
        }
    </style>
</head>
<body>
    <div class="header">顶部通栏</div>
    <div class="goTop">回到顶部</div>
</body>
<script>
var header = document.querySelector(".header");
var goTop = document.querySelector(".goTop");
window.onscroll = function () { 
    var height = 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.onclick =function(){
    window.scrollTo({
        top:0,
        behavior:'smooth'
    })
    }
</script>
</html>

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

            padding: 0;

        }

        body{

            height: 3000px;

        }

        .header{

            width: 100%;

            height: 80px;

            display: flex;

            justify-content: center;

            align-items: center;

            font-size: 30px;

            color: white;

            background-color: skyblue;

            position: fixed;

            top: -80px;

            left: 0;

            transition: top .5s linear;

        }

        .goTop{

            width: 50px;

            height: 50px;

            background-color: pink;

            font-size: 20px;

            text-align: center;

            line-height: 25px;

            color: white;

            position: fixed;

            bottom: 50px;

            right: 50px;

         

        }

    </style>

</head>

<body>

    <div class="header">顶部通栏</div>

    <div class="goTop">回到顶部</div>

</body>

<script>

var header = document.querySelector(".header");

var goTop = document.querySelector(".goTop");

window.onscroll = function () {

    var height = 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.onclick =function(){

    window.scrollTo({

        top:0,

        behavior:'smooth'

    })

    }

</script>

</html>

解析:

        当滚动条超过一定距离时,顶部导航栏才会显示和回到顶部按钮才会显示。默认顶部导航栏和顶部按钮是隐藏的。挺简单的案例,要是哪里不明白的私信我。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麋鹿会飞但不飘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值