jquery实现电梯导航

效果图如下:

在这里插入图片描述

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>电梯导航</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #container {
            width: 900px;
            margin: 0 auto;
        }

        #container>* {
            width: 100%;
        }

        .header {
            height: 2000px;
            background: rgb(158, 250, 238);
        }

        .floor {
            height: 1000px;
        }

        .footer {
            height: 500px;
            background: rgb(255, 187, 0);
        }

        #nav {
            width: 100px;
            position: fixed;
            left: 20px;
            bottom: 300px;
            list-style: none;
            display: none;
            cursor: pointer;
        }

        #nav li {
            height: 35px;
            line-height: 35px;
            border-bottom: 1px dashed #ccc;
            text-align: center;
            font-size: 25px;
            margin-top: 1px;
        }

        .curr {
            background: rgb(84, 250, 228);
            color: #fff;
        }
    </style>



</head>

<body>

    <div id="container">

        <div class="header">header</div>

        <div class="main">

            <div class="floor" style="background: rgb(251, 152, 206);">1F</div>

            <div class="floor" style="background: yellow;">2F</div>

            <div class="floor" style="background: rgb(164, 78, 245);">3F</div>

            <div class="floor" style="background: orange;">4F</div>

            <div class="floor" style="background: greenyellow;">5F</div>

            <div class="floor" style="background: bisque;">6F</div>

            <div class="floor" style="background: pink;">7F</div>

        </div>

        <div class="footer">footer</div>

    </div>

    <div>

        <ul id="nav">

            <li class="curr">1F</li>

            <li>2F</li>

            <li>3F</li>

            <li>4F</li>

            <li>5F</li>

            <li>6F</li>

            <li>7F</li>

            <li class='last'>返回顶部</li>

        </ul>

    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>

    <script>

        $(function () {
            //返回顶部的点击事件
            $('.last').click(function () {
                $('body,html').animate({
                    scrollTop: 0,
                })
            })

            var winHeight = $(window).height() //获取浏览器可视窗口的高度
            console.log(winHeight)
            var headerHeight = $('.header').height() //获取header的高度
            console.log(headerHeight)
            onOff = false;    //布尔值变量,通错这个变量可以防止快速连续点击的时候出现的连续滚动

            $(window).on('scroll', function () {

                var scTop = $(window).scrollTop();  //获取滚动条的滚动距离
                console.log(scTop)
                //当楼侧开始出现时显示楼层导航条

                if (scTop >= headerHeight - winHeight) {

                    $('#nav').show(400);   //也可以不传参数,传参数表示运动时间

                } else {

                    $('#nav').hide(400);

                }


                //滚动时切换显示楼层

                if (!onOff) {

                    $('.floor').each(function (index, element) {

                        var _top = $(this).offset().top;

                        //当每层楼的最上面滚动到浏览器窗口的中间时切换导航条的显示

                        if (scTop >= _top - winHeight / 2) {

                            //此处添加curr类样式并不是改变显示样式,而是为了标当前所在的楼层

                            $('#nav>li').not('.last').eq(index).addClass('curr').siblings().removeClass('curr')
                        }

                    });

                }

            })

            var list = $('#nav li').not('.last');
            list.on('click', function () {

                onOff = true;  //将开关变量onOff置为true

                var index = $(this).index(),  //获取当前电机的li的索引

                    _top = $('.floor').eq(index).offset().top;//获取相对于的楼层到文档顶部的距离

                $(this).addClass('curr').siblings().removeClass('curr')

                $('html,body').animate({ 'scrollTop': _top }, 400, function () {

                    onOff = false; //在运动执行完毕的毁掉函数中将onOff的值重置为false

                });
            });

        });

    </script>

</body>

</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

吃葡萄不吐葡萄皮嘻嘻

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

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

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

打赏作者

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

抵扣说明:

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

余额充值