http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2698|回复: 2

[Jquery] jQuery-前端楼层导航效果代码

[复制链接]
发表于 2021-6-16 17:02:48 | 显示全部楼层 |阅读模式
现在很多电商网站都使用了楼层导航的效果,效果的原理主要是用鼠标的滚动事件和元素高度的应用。下面是整理的代码,源码粘贴如下,感兴趣的可以下载源码可直接使用。
效果展示:
企业微信截图_16238397553453.png

1.css样式

[CSS] 纯文本查看 复制代码
* {
                                margin: 0;
                                padding: 0;
                        }
                        
                        #container {
                                width: 900px;
                                margin: 0 auto;
                        }
                        
                        #container>* {
                                width: 100%;
                        }
                        
                        .header {
                                height: 2000px;
                                background: red;
                        }
                        
                        .item {
                                height: 1000px;
                                font: bold 25px/30px "微软雅黑";
                        }
                        
                        .footer {
                                height: 500px;
                                background: red;
                        }
                        
                        #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;
                                position: relative;
                                font-size: 25px;
                                margin-top: 1px;
                        }
                        
                        #nav li span {
                                position: absolute;
                                display: inline-block;
                                height: 35px;
                                line-height: 35px;
                                width: 100%;
                                text-align: center;
                                left: 0;
                                top: 0;
                                display: none;
                                background: #f00;
                                color: #fff;
                        }


2.HTML 代码

[HTML] 纯文本查看 复制代码
<div id="container">
                        <div class="header">header</div>
                        <div class="main">
                                <div class="item" style="background: palegreen;">1F</div>
                                <div class="item" style="background: pink;">2F</div>
                                <div class="item" style="background: blueviolet;">3F</div>
                                <div class="item" style="background: orange;">4F</div>
                                <div class="item" style="background: greenyellow;">5F</div>
                                <div class="item" style="background: red;">6F</div>
                                <div class="item" style="background: yellow;">7F</div>
                        </div>
                        <div class="footer">footer</div>
                </div>
                <div>
                        <ul id="nav">
                                <li>1F<span style="display: inline-block;" class="active">1楼</span></li>
                                <li>2F<span>2楼</span></li>
                                <li>3F<span>3楼</span></li>
                                <li>4F<span>4楼</span></li>
                                <li>5F<span>5楼</span></li>
                                <li>6F<span>6楼</span></li>
                                <li>7F<span>7楼</span></li>

                        </ul>

                </div>


3.js 代码

[JavaScript] 纯文本查看 复制代码
$(function() {
                                var winHeight = $(window).height(), //获取浏览器可是窗口的盖度
                                        headerHeight = $('.header').height(), //获取header的高度
                                        onOff = false; //布尔值变量,通错这个变量可以防止快速连续点击的时候出现的连续滚动

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

                                        var scTop = $(window).scrollTop(); //获取滚动条的滚动距离

                                        //当楼侧开始出现时显示楼层导航条

                                        if(scTop >= headerHeight - winHeight) {
                                                $('#nav').show(400); //也可以不传参数,传参数表示运动时间
                                        } else {
                                                $('#nav').hide(400);
                                        }
                                        //滚动时切换显示楼层
                                        if(!onOff) {
                                                $('.item').each(function(index, element) {
                                                        var _top = $(this).offset().top;
                                                        //当每层楼的最上面滚动到浏览器窗口的中间时切换导航条的显示
                                                        if(scTop >= _top - winHeight / 2) {
                                                                //此处添加active类样式并不是改变显示样式,而是为了标当前所在的楼层
                                                                $('#nav>li').eq(index).addClass('active').children().show()

                                                                        .end().siblings().removeClass('active').children().hide();

                                                        }

                                                });

                                        }

                                })

                                $('#nav>li').hover(function() {

                                        $(this).children().show();

                                }, function() {

                                        //此处用到.not('.active')来过滤当前楼层,鼠标移开时仍然保持当前的显示样式

                                        $(this).not('.active').children().hide();

                                }).on('click', function() {

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

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

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

                                        $(this).addClass('active').children().show().end()

                                                .siblings().removeClass('active').children().hide();
                                        $('html,body').animate({
                                                'scrollTop': _top
                                        }, 400, function() {

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

                                        });
                                });

                        });



以上是全部代码,下载源码,请点击》》 楼层导航.zip (30.31 KB, 下载次数: 0)
企业微信截图_16238395994958.png


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2021-6-16 17:35:51 | 显示全部楼层
支持下, 粘个效果展示会更好
发表于 2021-6-17 09:13:22 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 11:04

© 2014-2021

快速回复 返回顶部 返回列表