js实现电梯导航效果的示例代码

 更新时间:2023年12月11日 14:05:56   作者:LxyingINGing  
这篇文章主要介绍了JavaScript实现电梯导航效果的相关知识,文中通过示例代码介绍的很详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

页面往下滑动到一定距离时,电梯导航显示,且与内容板块相呼应。使用了选项卡的功能,采用排他思想等实现。效果如下图所示:

代码:

封装函数 move.js:       可直接调用,无需修改

function move(obj, target, callback) {  
    clearInterval(obj.timer); // 清除之前的定时器  
    obj.timer = setInterval(function () {  
        var header = window.scrollY;  
        var distance = target - header;  
        var speed = distance / 10;  
        speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);  
        if (Math.abs(distance) <= Math.abs(speed)) { // 修改停止动画的条件  
            console.log('aaa');  
            clearInterval(obj.timer); // 清除定时器  
            window.scrollTo(0, target); // 确保滚动到目标位置  
            callback && callback();  
        } else {  
            window.scrollTo(0, header + speed);  
        }  
    }, 15);  
}

 详细代码如下:

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
 
        }
 
        .header {
            width: 1200px;
            height: 1000px;
            background-color: aqua;
            margin: 0 auto;
        }
 
        .floor1 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 0, 0, 1);
            margin: 10px auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor2 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 0, 0, 0.8);
            margin: 10px auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor3 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 0, 0, 0.6);
            margin: 10px auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor4 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 255, 0, 1);
            margin: 10px auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor5 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 0, 255, 1);
            margin: 10px auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor6 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 0, 0, 1);
            margin: 0 auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor7 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 255, 0, 1);
            margin: 10px auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor8 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 210, 0, 1);
            margin: 10px auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor9 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 255, 0, 1);
            margin: 10px auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor10 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 0, 255, 1);
            margin: 10px auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .floor11 {
            width: 1200px;
            height: 700px;
            background-color: rgba(255, 0, 0, 1);
            margin: 0 auto;
            font-size: 30px;
            text-align: center;
            line-height: 700px;
        }
 
        .footer {
            width: 1200px;
            height: 800px;
            background-color: rgba(0, 0, 0, 1);
            margin: 10px auto;
        }
 
 
        .elevator {
            width: 44px;
            height: auto;
            background: #e9e6e6;
            position: fixed;
            left: 50%;
            top: 50%;
            transform: translateX(-50%);
            /* z-index: 999; */
            margin: -266.5px 640px 0 0;
            margin-left: -650px;
            padding-bottom: 4px;
            display: none;
        }
 
        .elevator ul li {
            font-size: 12px;
            width: 28px;
            height: 29px;
            padding: 4px;
            color: #fff;
            text-align: center;
            margin: 4px 4px 0 4px;
            cursor: pointer;
            background: #655f61;
            line-height: 14px;
        }
 
        .elevator ul li:nth-child(1) {
            line-height: 29px;
            background: #ff1a66;
        }
 
        .elevator ul li:last-child {
            line-height: 29px;
            background: #acacac;
        }
 
        .elevator ul li:hover {
            background: #f69;
        }
 
        .elevator ul li.active {
            background: #f69;
        }
 
        .elevator ul li.r_active {
            background: #666;
        }
    </style>
</head>
 
<body>
    <div class="header"></div>
    <div class="floor1  f">黄金铂金</div>
    <div class="floor2  f">投资金银</div>
    <div class="floor3  f">钻石</div>
    <div class="floor4  f">银饰</div>
    <div class="floor5  f">彩色宝石</div>
    <div class="floor6  f">翡翠玉石</div>
    <div class="floor7  f">珍珠</div>
    <div class="floor8  f">流行饰品</div>
    <div class="floor9  f">天然木饰</div>
    <div class="floor10  f">奇趣收藏</div>
    <div class="floor11  f">眼镜手表</div>
    <div class="footer"></div>
 
    <div class="elevator">
        <ul>
            <li>导航</li>
            <li class="item active">黄金铂金</li>
            <li class="item">投资金银</li>
            <li class="item">钻石</li>
            <li class="item">银饰</li>
            <li class="item">彩色宝石</li>
            <li class="item">翡翠玉石</li>
            <li class="item">珍珠</li>
            <li class="item">流行饰品</li>
            <li class="item">天然木饰</li>
            <li class="item">奇趣收藏</li>
            <li class="item">眼镜手表</li>
            <li class="backtop"><a href="#" rel="external nofollow" ></a>顶部</li>
        </ul>
    </div>
    <script src="./js/move.js"></script>
    <script>
        (function () {
            //查节点对象
            var floor1 = document.querySelector('.floor1');
            var elevator = document.querySelector('.elevator');
            var items = document.querySelectorAll('.elevator li.item');
            var fs = document.querySelectorAll('.f');
            var backTop = document.querySelector('.elevator li.backtop');
            var footer=document.querySelector('.footer');
            //1.页面滚动时电梯导航的显示与隐藏效果
            load();
            function load() {
                //页面卷进去的距离
                var disY = document.documentElement.scrollTop;
                //第一个楼层与页面顶部的距离
                var dis = floor1.offsetTop - 200;
                if (disY >= dis) {
                    elevator.style.display = "block";
                } else {
                    elevator.style.display = "none";
                }
                //4.交互,相应li对应相应楼层
                for (var i = 0; i < fs.length; i++) {
                    var fs_D_top = fs[i].offsetTop-100;
                    if (disY >= fs_D_top) {
                        document.querySelector('.elevator li.active').classList.remove('active');
                        items[i].classList.add('active');
                    }
                    //到最后一个楼层不再继续往下变颜色
                    if(disY>footer.offsetTop){
                        document.querySelector('.elevator li.active').classList.add('r_active');
                    }else{
 
                        document.querySelector('.elevator li.active').classList.remove('r_active');
                    }
                }
            }
            //页面滑动
            window.onscroll = function () {
                load();
            }
            //2.选项卡
            //单击小li切换到对应页面
            for (let i = 0; i < items.length; i++) {
                items[i].onclick = function () {
                    document.querySelector('.elevator li.active').classList.remove('active');
                    this.classList.add('active');
                    //呼应内容区
                    move(window, fs[i].offsetTop)
                }
            }
            //3.返回顶部
            backTop.onclick = function () {
                move(window, 0);
            }
        })();        
    </script>
</body>
 
</html>

到此这篇关于js实现电梯导航效果的示例代码的文章就介绍到这了,更多相关js电梯导航内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • js判断文本框剩余可输入字数的方法

    js判断文本框剩余可输入字数的方法

    这篇文章主要介绍了js判断文本框剩余可输入字数的方法,可实现直观显示文本框可输入字数的功能,非常具有实用价值,需要的朋友可以参考下
    2015-02-02
  • Base64编码加密JS代码网页版

    Base64编码加密JS代码网页版

    Base64编码加密JS代码网页版,在文本框中输入任意字符串,即可将该字符串按Base64编码进行加密,也可将任意Base64编码的字符解密显示正常值
    2013-03-03
  • 前端开发不得不知的10个最佳ES6特性

    前端开发不得不知的10个最佳ES6特性

    ES6已经不再是JavaScript最新的标准,但是它已经广泛用于编程实践中。下面通过本文给大家分享前端开发不得不知的10个最佳ES6特性,感兴趣的朋友参考下吧
    2017-08-08
  • JS实现的幻灯片切换显示效果

    JS实现的幻灯片切换显示效果

    这篇文章主要介绍了JS实现的幻灯片切换显示效果,涉及javascript通过扩展实现针对页面元素的动态切换操作相关技巧,需要的朋友可以参考下
    2016-09-09
  • 浅谈javascript的分号的使用

    浅谈javascript的分号的使用

    不加分号其实体现的是对javascript的深入理解。最后一个分号是因为无法判断后引入的js文件内容会不会对本文档产生影响;return多数是要加分号的,
    2015-05-05
  • 前端开发中常见的数据结构优化问题与解决

    前端开发中常见的数据结构优化问题与解决

    在实际前端开发中,后端返回的数据结构往往不能直接满足前端展示或业务逻辑的需求,需要进行各种优化处理,下面我们来看看常见的几个优化问题及解决方案吧
    2025-04-04
  • 详解使用JS如何制作简单的ASCII图与单极图

    详解使用JS如何制作简单的ASCII图与单极图

    这篇文章主要给大家介绍了使用JS如何制作简单的ASCII图与单极图的相关资料,文中介绍的非常详细,并在文末给出了详细的示例代码,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
    2017-03-03
  • three.js中文文档学习之通过模块导入

    three.js中文文档学习之通过模块导入

    这篇文章主要给大家介绍了关于three.js中文文档学习之通过模块导入的相关资料,文中通过示例代码介绍的非常详细,对大家学习或使用three.js具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2017-11-11
  • JS实现简单拖拽效果

    JS实现简单拖拽效果

    这篇文章主要为大家详细介绍了JS实现拖拽效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • javascript中slice(),splice(),split(),substring(),substr()使用方法

    javascript中slice(),splice(),split(),substring(),substr()使用方法

    这篇文章主要介绍了javascript中slice(),splice(),split(),substring(),substr()使用方法,需要的朋友可以参考下
    2015-03-03

最新评论