改良版 导航栏自动跟随

css部分:

           .container {
                /*最外层div定宽*/
                position: relative;
                width: 15rem;
                height: 1rem;
                margin-top: 20rem;
            }
            .con {
                /*第二层div,设置溢出为滚动条*/
                overflow-x: scroll;
                overflow-y: hidden;
                height: 1rem;
                font-size: 0.4rem;
                width: 15rem;
                position: absolute;
                top: 0;
            }
            .nav {
                /*导航条div 设置弹性盒子,使得子集为一行显示,设置子集float也可以       宽度为js动态设置*/
                display: flex;
            }
            .nav li {
                /*子集样式*/
                width: 4rem;
                flex: 0 0 4rem;
                border: 1px solid #000000;
            }
            .active {
                /*选中后的样式*/
                background-color: bisque;
            }

            /*以下是监听元素的样式*/
            .list {
                margin-bottom: 30rem;
            }
            .item {
                height: 6rem;
                width: 15rem;
                border: 1px solid;
            }                    

html结构部分:

    <!--导航栏部分-->
        <div class="container">
            <div class="con">
                <ul class="nav">
                    <li class="active"> 1111111</li>
                    <li>222222</li>
                    <li>333333</li>
                    <li>44444</li>
                    <li>55555555</li>
                    <li>66666666</li>
                    <li>7777</li>
                </ul>
            </div>
        </div>

        <!--监听元素部分-->
        <div class="list">
            <div class="item">
                111111111111111111
            </div>
            <div class="item">
                222222222222222222
            </div>
            <div class="item">
                3333333333333333333
            </div>
            <div class="item">
                444444444444444444
            </div>
            <div class="item">
                5555555555555555555
            </div>
            <div class="item">
                6666666666666666666
            </div>
            <div class="item">
                77777777777777777777
            </div>
        </div>

js部分:

$(function() {
                scrollgen({ //调用  全部可选参数        导航栏默认为    .nav
                    navChild: ‘.nav li‘, //导航栏子集 li
                    navFather: ‘.con‘, //导航栏父级
                    nav: ‘.nav‘, //导航栏ul
                    active: ‘.active‘, //被选中后的样式
                    control: ‘.item‘, //监听的元素
                    //isFollow: true //默认为false 为全部显示  false为tab切换 //true  为全部显示, 监视滚动条跟随
                })

                function scrollgen(obj) {

                    //默认参数
                    var objDefault = {
                        navChild: $(‘.nav‘).children(), //默认获取子节点
                        navFather: $(‘.nav‘).parent(), //默认获取父级
                        nav: $(‘.nav‘),
                        active: $(‘.active‘),
                        control: $(‘.nav‘).parent().parent().next().children(),
                    };
                    if(!obj) {
                        obj = {}
                    }
                    $.extend(obj, objDefault);

                    var $navChild = $(obj.navChild),
                        $navFather = $(obj.navFather),
                        $nav = $(obj.nav),
                        $active = $(obj.active),
                        active = $active[0].className,
                        $control = $(obj.control),
                        isFollow = obj.isFollow || false; //默认为false

                    $navChild.eq(0).addClass(active).siblings().removeClass(active);//初始值为第一个li选中
                    $navFather.animate({
                        scrollLeft: 0
                    }, 200)
                    var nh = $nav.offset().top, //获取导航栏在文档中的高度
                        sh = $(document).scrollTop(); //获取滚轮高度
                    var start_nH = $nav.offset().top
                    index = $navChild.index() + 1
                    //动态设置nav 宽度
                    $nav.width(function() {
                        return $navChild.width() * index + 10 + ‘px‘;
                    })
                    var Harr = [], //存储item高度容器
                        Warr = []; //存储nav li offsetleft容器
                    $control.each(function(index, el) {
                        Harr.push(el.offsetTop);
                    })
                    $navChild.each((index, el) => {
                        Warr.push(el.offsetLeft);
                    })

                    if(isFollow == false) {
                        $control.eq(0).css({
                            ‘display‘: ‘block‘
                        }).siblings().css({
                            ‘display‘: ‘none‘
                        });
                        $navChild.click(function() {
                            var i = $(this).index()
                            $control.eq(i).css({
                                ‘display‘: ‘block‘
                            }).siblings().css({
                                ‘display‘: ‘none‘
                            });
                            $(‘html,body‘).animate({
                                scrollTop: $control.eq(i).offset().top - $nav.height() - 10
                            }, 400);
                            $navChild.eq(i).addClass(active).siblings().removeClass(active);
                            var item = $("." + active);
                            var itemOffset = item.offset();
                            //元素离    scrollLeft等于0  时的距离
                            var itemOffsetLeft = itemOffset.left + $navFather.scrollLeft();
                            //scrollLeft等于0,居中时的offsetLeft
                            var centerLeft = ($navFather.width() - item.width()) / 2;
                            $navFather.stop().animate({
                                scrollLeft: itemOffsetLeft - centerLeft
                            }, 200)
                        })
                    }
                    $(window).scroll(function() {
                        sh = $(document).scrollTop();
                        if($(window).scrollTop() >= nh) {
                            $navFather.css({
                                ‘position‘: ‘fixed‘,
                                "top": ‘0‘
                            });
                            Harr.forEach((n, i) => {
                                if($(window).scrollTop() >= n - $nav.height() - 15) {
                                    if(isFollow) {
                                        $navChild.eq(i).addClass(active).siblings().removeClass(active);
                                    }
                                }
                            });

                            //以下是导航跟随逻辑,
                            //思路:计算出scrollLeft =0 时   元素离文档最左侧的距离   -   滚动后元素离  scrollLeft =0 时 元素离文档最左侧的距离  =滚动距离
                            var item = $("." + active);
                            var itemOffset = item.offset();
                            //元素离    scrollLeft等于0  时的距离
                            var itemOffsetLeft = itemOffset.left + $navFather.scrollLeft();
                            //scrollLeft等于0,居中时的offsetLeft
                            var centerLeft = ($navFather.width() - item.width()) / 2;
                            $navFather.stop().animate({
                                scrollLeft: itemOffsetLeft - centerLeft
                            })
                        } else {
                            $navFather.css({
                                ‘position‘: ‘absolute‘,
                                "z-index": ‘7‘
                            })
                        }
                    });

                    $navChild.click(function() {
                        var i = $(this).index()
                        var offsetH = $control.eq(i).offset().top;
                        $(‘html,body‘).animate({
                            scrollTop: $control.eq(i).offset().top - $nav.height() - 10
                        }, 500)
                    })
                }
            })

原文地址:https://www.cnblogs.com/wxyblog/p/12050175.html

时间: 2024-08-26 19:20:06

改良版 导航栏自动跟随的相关文章

双击导航栏自动滑动ListView到顶部

有些app都实现了双击导航栏让页面的list自动滑动到顶部的feature. 先实现一个继承于OnTouchListener的监听多次点击事件的监听器,通过callback把连续点击的次数返回给客户代码,代码见gist:MultiTouchListener.java. https://gist.github.com/Viyu/d06eda19f9bcf7223f6b 然后给导航栏添加下面这个OnTouchListener的实现: OnMultiTouchListener mOnMultiTouc

ecshop导航栏自动显示三级或多级子栏目,多级频道分类,并实现css高亮显示

ecshop导航要达到的目标: 一,比如上图,当我访问三级分类,响应式布局,这个栏目时,最顶级的元件这个分类,要高亮显示 二,如果导航上面有商品或文章频道, 并且他们有子栏目,则全自动显示所有的子栏目. 三,如果这个导航有子分类,则统一显示下拉三角标志. 代码如下 一,在includes/lib_main.php 文件中,修改掉或另外重命名并重定义一个这个get_navigator()函数,修改后的内容如下,另外get_categories_tree()这个函数为系统自带的在lib_goods.

自制一个UIView代替ViewController的导航栏视图跟随scrollview滑动而改变大小并且图片移动交错效果,列表的Header View中的图片产生视差滚动效果

#import <UIKit/UIKit.h> @interface ELHeaderView : UIView@property (nonatomic, weak) UIViewController *viewController;@property (nonatomic, weak) UIScrollView *scrollView; - (id)initWithFrame:(CGRect)frame backGroudImageURL:(NSString *)backImageURL h

js监听网页页面滑动滚动事件,实现导航栏自动显示或隐藏

/** * 页面滑动滚动事件 * @param e *///0为隐藏,1为显示var s = 1; function scrollFunc(e) { // e存在就用e不存在就用windon.event e = e || window.event;// 先判断是什么浏览器 if (e.wheelDelta) { // 浏览器IE,谷歌 if (e.wheelDelta > 0) {//当滑轮向上滚动时// console.log("滑轮向上滚动"); if (s == 0) {

改良版sidebar 通讯录导航栏A-Z

我在网上搜了个sidebar的源码, 但是在xml里面开启大屏模式后,导航栏字体变得很小 <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true"

【AmazeUI】手机版页面的顶部导航条Header与侧边导航栏offCanvas

顶部导航条如果你细心留意下现在的页面,实在是太常见了.这个组件在手机端的页面中,同样可以借助AmazeUI这个前端框架实现.与此同时,可以在导航栏的最右方加一个触发侧边导航栏offCanvas.不要再使用BootStrap那种,一旦点击就大幅度下拉的导航了,反正我个人觉得很蛋疼的.一个导航活活地占据了页面大量位置.这个侧边导航栏offCanvas曾经是php方面的WorkPress的优秀设计来的. 使用AmazeUI做出来的效果如下: 首先是顶部导航栏的代码: <!--这里的顶部导航栏与Boot

jQuery粘性跟随滚动条滚动的导航栏源代码下载

jQuery粘性跟随滚动条滚动的导航栏源代码下载 作者:网页模板 大小:0.005MB 点击次数:3494 发布时间:2014-03-07 12:55 分享到:0 特效介绍 jQuery粘性跟随滚动条滚动的导航栏源代码下载,不兼容IE6.点击导航栏不同的按钮,滑动到不同的内容.滚动条滚动,导航栏会自始至终保持在浏览器可是区域的最顶端. 使用方法 第一步.在head区域引入下面的css样式: 01 <link href="http://fonts.googleapis.com/css?fam

[HTML/CSS]导航栏的下划线跟随效果

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <meta http-equiv="X-UA-Compatib

Markdown使用TOC自动生成导航栏

经常使用markdown 的玩家一定很想要一个自动生成的导航栏吧,自己写的基本思路就是 轮询监听滚动条的位置,通过抛锚和跳锚实现,这里介绍一下今天的主角,markdown-toc插件: https://github.com/jonschlinkert/markdown-toc # 0x00 安装 TOC = Table of content , 将内容制作成导航 这个插件是基于 nodejs 的,因此需要安装 node 和 npm ,这里同样采用nvm的形式安装 curl -o- https:/