jQuery实现电梯导航栏

电梯导航栏是什么? 

电梯导航栏让用户更好的去了解到页面有几大板块以及清除自己身处哪一板块

那么我们该如何制作电梯导航栏?

下面让我们看代码

html

<div class="top"></div>
    <div class="nav"></div>
    <div class="fixed">
        <span>head</span>
        <span>banner</span>
        <span>footer</span>
    </div>
    <div class="w">
        <div class="head"></div>
        <div class="banner"></div>
        <div class="footer"></div>
    </div>

css

 * {
            margin: 0;
            padding: 0;
        }
        .top {
            width: 1200px;
            margin: 0 auto 0;
            height: 300px;
            background-color: purple;
        }
        .nav {
            width: 1200px;
            margin: 0 auto 0;
            height: 600px;
            background-color: yellow;
        }
        body {
            height: 10000px;
        }
        .w {
            width: 1200px;
            margin: 0 auto 0;
        }
        .head {
            height: 400px;
            background-color: aquamarine;
        }
        .banner {
            height: 600px;
            background-color: pink;
            
        }
        .footer {
            height: 300px;
            background-color: aqua;
        }
        .fixed {
            display: none;
            width: 50px;
            height: 150px;
            background-color: red;
            position: fixed;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
        }
        .fixed span {
            line-height: 50px;
            text-align: center;
            display: block;
        }
        .pink {
            background-color: pink;
        }

js(重点)

 var nav = $('.nav').offset().top;
        // 设置节流阀 做到点击和滑动到某一区域颜色切换独立的效果
        var flag = true;
        out();
        // 封装out()为了在页面刷新的时候调用
        // 以防一刷新fixed栏就没
        // 并且在滑动的时候也调用来判断是否隐藏与显示
        function out(){
            if ($(document).scrollTop()>=nav) {
                $('.fixed').fadeIn();
            }else {
                $('.fixed').fadeOut();
            }
        }
        $(window).scroll(function(){
            out();
            // 设置节流阀在点击的时候不执行代码
            if(flag) {
                $('.w div').each(function(i,ele){
                    // 利用each遍历每一个div当移动超过其距离顶部的距离利用排他思想给其附上颜色
                if($(document).scrollTop()>=$(ele).offset().top) {
                    $('.fixed span').eq(i).addClass('pink').siblings().removeClass('pink');
                }
            })
            }
        })
        $('.fixed span').click(function(){
            flag = false;
            var index = $(this).index();
            var current = $('.w div').eq(index).offset().top;
            // fixed栏的每一个和中心每一个板块一对一进行对应
            $('body,html').stop().animate({
                // 添加动画并且做到点那个跳转到哪层电梯
                scrollTop: current
            },function(){
                // 当点击事件结束后将节流阀打开
                flag = true;
                
            })
            $(this).addClass('pink').siblings().removeClass('pink');
            
        })

如果你觉的作者分析的不错的话请留下你的三连

感谢 ! ! !

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值