jquery之锚点定位和jquery模拟锚点定位效果,页面滚动到相对应的位置时,所在的导航文字高亮显示

2023-11-19

1.常见的锚点定位效果:

<a name="print"></a>
<div id="print">
<!-- HTTP请求没有 # -->

<a href="#print"></a>
<div id="print">
<!-- HTTP请求有 # -->

这种方法,如果网页中有顶部固定导航栏,那么常见的锚点定位就会被遮住了一部分,很难用样式弥补。于是可以考虑第二种方法 js模拟锚点跳转效果

2.js模拟锚点跳转效果 见上一篇详细介绍

这是锚点上一篇:jquery模拟锚点定位效果:锚点定位——如何设置锚点居页面顶部距离,锚点定位并距离顶部一定偏移

但是,这样模拟url是没有#(hash)值的 如果导航有其他分页,就不容易跳回了,比如:
在这里插入图片描述
当前页是楼盘信息详情页,如果我想点楼盘相册,想要的效果是跳转到首页,再锚点定位到楼盘相册的位置;
解决方案:
html:
给导航a标签 添加一个#锚点(与模拟锚点点击事件的class同名),后面再用js获取url的hash值,面加载后判断url 是否带有hash值,如有,侧js执行点击相应的hash 事件(附代码如下)

<div class="listnav cenbox">
    <div class="cenbox clearfix">
        <ul class="xiangqingul">
            <li class="on">
                <a href="#huxingmao" class="huxingmao">楼盘户型</a>
            </li>
            <li>
                <a href="#xiangcemao" class="xiangcemao">楼盘相册</a>
            </li>
            <li>
                <a href="#xinximao" class="xinximao">楼盘信息</a>
            </li>
            <li>
                <a href="#mapmao" class="mapmao">位置配套</a>
            </li>
            <li>
                <a href="#fangdaimao" class="fangdaimao">贷款计算</a>
            </li>
            <li>
                <a href="#zixunmao" class="zixunmao">楼盘资讯</a>
            </li>
            <li>
                <a href="#tuijianmao" class="tuijianmao">好房推荐</a>
            </li>
        </ul>
        <p class="tel">咨询电话: 400-815-8655</p>
    </div>
</div>

css:


/* listnav */

.listnav {
    position: relative;
    width:1200px;
    margin:0 auto;
    background: #f4e827;
}

.listnav ul {
    float: left;
    position: relative;
    width: 900px;
    display: flex;
    flex-direction: row;
}

.listnav ul.xiangqingul {
    width: 930px;
}

.listnav ul li {
    position: relative;
    padding: 0 25px;
    font-size: 20px;
    line-height: 50px;
    color: #000;
    font-weight: 500px;
}

.listnav ul li a {
    color: #000;
}

.listnav ul li.on {
    color: #3490d3
}

.listnav ul li.on a, .listnav ul li:hover a {
    color: #3490d3
}

.listnav .searchcon {
    float: right;
    position: relative;
    width: 285px;
    height: 30px;
    margin-top: 10px;
    background: #fff;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    border-radius: 2px;
}

.listnav .tel {
    float: right;
    margin-right: 20px;
    font-size: 20px;
    line-height: 50px;
    color: #000;
    font-weight: 500px;
}

.listnav .searchbox {
    position: relative;
    width: 240px;
    height: 30px;
    padding-left: 10px;
    font-size: 14px;
    line-height: 30px;
    color: #666;
}

.listnav .searchbox .searchinp {
    display: block;
    position: relative;
    width: 100%;
    height: 100%;
    background: none;
    border: 0;
    outline: none;
}

.listnav .searchcon .searchico {
    width: 15px;
    height: 15px;
    margin: 7px 10px 0 0;
}

.listnav .searchcon .searchicoinp {
    position: absolute;
    top: 0;
    right: 0;
    width: 30px;
    height: 30px;
    opacity: 0;
    z-index: 2;
    cursor: pointer;
}

js:

// 锚点定位
            function topMao(target) {
                $('html, body').animate({
                    scrollTop: $(target).offset().top - $('.topfubox').height()
                }, 500); //130为锚点到距顶部的距离,500为执行时间
                return false;
            }

            $('.huxingmao').click(function () {
                topMao('#huxing');
            })
            $('.xiangcemao').click(function () {
                topMao('#xiangce');
            })
            $('.xinximao').click(function () {
                topMao('#xinxi');
            })
            $('.mapmao').click(function () {
                topMao('#map');
            })
            $('.fangdaimao').click(function () {
                topMao('#fangdai');
            })
            $('.zixunmao').click(function () {
                topMao('#zixun');
            })
            $('.tuijianmao').click(function () {
                topMao('#tuijian');
            })

			//用js获取url的hash值,页面加载后判断url是否带有hash值,如有,侧js执行点击相应的hash事件
			var hash = window.location.hash;
            if (hash != '') {
                var hashinp = hash.substr(1, hash.length);
                console.log(hashinp);
                $('.' + hashinp).click();
            }

另:
页面滚动到相对应的位置时,所在的导航文字高亮显示;
html:

<div class="topfubox clearfix">
        <div class="topfucon cenbox clearfix">
            <div class="lefbox">
                <div class="ltop">
                    <p class="lptit">水岸花语(A栋)</p>
                    <p class="lptxt">3室2厅1卫</p>
                    <p class="lptxt">98.88㎡</p>
                    <p class="lptxt red">155万</p>
                </div>
                <div class="lbom bltan" type="6">
                    <p>发送楼盘最新资料和价格到手机 </p>
                    <span class="sendinfo">立即发送</span>
                </div>
            </div>
            <div class="rigbox">
                <div class="tximg">
                    <img src="img/guwenpic.jpg" alt="">
                </div>
                <div class="namebox">
                    <p class="name">刘朝</p>
                    <p class="yongjin">佣金≤2.0%
                        <span>|</span> 美堂房产
                    </p>
                </div>
                <div class="daikan">
                    <span>带看</span>
                    <!-- 带看 用线上咨询插件 -->
                    <span>电话咨询</span>
                    <!-- 以后做小程序 扫码拨号 -->
                    <a class="linkover" href="#" target="_blank"></a>
                </div>
            </div>
        </div>
        <div class="listnav cenbox">
            <div class="cenbox clearfix">
                <ul class="xiangqingul">
                    <li class="on">
                        <a href="#huxingmao" class="huxingmao">楼盘户型</a>
                    </li>
                    <li>
                        <a href="#xiangcemao" class="xiangcemao">楼盘相册</a>
                    </li>
                    <li>
                        <a href="#xinximao" class="xinximao">楼盘信息</a>
                    </li>
                    <li>
                        <a href="#mapmao" class="mapmao">位置配套</a>
                    </li>
                    <li>
                        <a href="#fangdaimao" class="fangdaimao">贷款计算</a>
                    </li>
                    <li>
                        <a href="#zixunmao" class="zixunmao">楼盘资讯</a>
                    </li>
                    <li>
                        <a href="#tuijianmao" class="tuijianmao">好房推荐</a>
                    </li>
                </ul>
                <p class="tel">咨询电话: 400-815-8655</p>
            </div>
            <!-- <div class="searchcon">        
                      <div class="searchbox">
                        <input type="text" placeholder="请试试输入楼盘名或小区" class="searchinp">
                      </div>
                      <img src="img/searchico.png" alt="" class="searchico">
                    </div> -->
        </div>
    </div>

css:


.topfucon {
    display: none;
    height: 100px;
}

.topfubox .lefbox {
    position: relative;
    float: left;
    width: 530px;
}

.topfubox .lefbox .ltop {
    position: relative;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-top: 10px;
}

.topfubox .lefbox .ltop .lptit {
    position: relative;
    font-size: 24px;
    color: #000;
    line-height: 50px;
    margin-right: 20px;
    max-width: 220px;
    height: 50px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.topfubox .lefbox .ltop .lptxt {
    position: relative;
    font-size: 20px;
    color: #666;
    line-height: 50px;
}

.topfubox .lefbox .ltop .red {
    color: #ff3a3a;
}

.topfubox .lefbox .lbom {
    position: relative;
    display: flex;
    flex-direction: row;
    font-size: 16px;
    color: #333;
    line-height: 40px;
}

.topfubox .lefbox .lbom p {
    display: block;
    padding-left: 24px;
    background: url(../img/newhouseico.png) no-repeat 0 -198px;
}

.topfubox .lefbox .lbom span {
    color: #fff;
    padding: 0 15px;
    height: 26px;
    line-height: 26px;
    border-radius: 20px;
    background: #ff3a3a;
    margin-left: 10px;
    margin-top: 8px;
    cursor: pointer;
}

.topfubox .rigbox {
    float: right;
    position: relative;
    width: 450px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    margin-top: 10px;
}

.topfubox .rigbox .tximg {
    position: relative;
    width: 80px;
    height: 80px;
    overflow: hidden;
    border-radius: 100%;
}

.topfubox .rigbox .tximg img {
    width: 100%;
    height: auto;
    min-height: 100%;
}

.topfubox .rigbox .namebox {
    position: relative;
    margin-left: 10px;
}

.topfubox .rigbox .namebox .name {
    font-size: 18px;
    color: #666;
    line-height: 40px;
}

.topfubox .rigbox .namebox .yongjin {
    font-size: 14px;
    color: #666;
    line-height: 30px;
}

.topfubox .rigbox .namebox .yongjin span {
    color: #999;
}

.topfubox .rigbox .daikan {
    display: flex;
    flex-direction: row;
    font-size: 16px;
    color: #fff;
    line-height: 34px;
    margin-left: 20px;
    margin-top: 28px;
}

.topfubox .rigbox .daikan span {
    display: block;
    height: 34px;
    margin-left: 10px;
    background: #ff3a3a;
    padding: 0 15px;
    border-radius: 20px;
    background: #ff3a3a;
    margin-left: 10px;
    cursor: pointer;
}

js:

var topBoxTop = $('.topfubox').offset().top + 50;
        var tab_l = $('.listnav li').length;
        var tabctnOffsetTopArr = [];
        var dingweiArr = ['huxing', 'xiangce', 'xinxi', 'map', 'fangdai', 'zixun', 'tuijian'];
        $(window).scroll(function () {
            setElPosition()
        })

        function setElPosition() {
            var top = $(document).scrollTop();
            if (top > topBoxTop) {
                console.log(1);
                $('.topfubox').addClass('fixed');
                $('.topfubox .listnav').removeClass('cenbox');
                $('.topfubox .topfucon,.zhanwei').show();
            } else {
                console.log(2);
                $('.topfubox').removeClass('fixed');
                $('.topfubox .listnav').addClass('cenbox');
                $('.topfubox .topfucon,.zhanwei').hide();
            }

            var setNavBarStyle = function (el, top, arr) {
                // if (clickSetStyle) {
                for (var i = 0; i < arr.length; i++) {
                    if (top >= arr[i]) {
                        el.eq(i).addClass('on').siblings('li').removeClass('on')
                    }
                }
                // }
            }
            setNavBarStyle($('.listnav .xiangqingul li'), top, tabctnOffsetTopArr)
        }


			$(document).ready(function () {
				setElPosition();
	            for (var i = 0; i < tab_l; i++) {
	                if (dingweiArr.length > 0) {
	                    var offsettop = $('#' + dingweiArr[i]).offset().top - 200;
	                    tabctnOffsetTopArr.push(offsettop)
	                }
	            }
	            console.log(tabctnOffsetTopArr);
	        })

思路:把页面锚点对应的板块id写入数组,页面加载 获取id距离页面顶部的高度,页面滚动事件执行,滚动到相应数组的高度值,则高亮显示相应的导航文字。

缺点:页面中导航板块不可缺少,如某页隐藏某一板块,则。。。问题待研究。。。

借鉴文章:https://blog.csdn.net/weixin_42839080/article/details/82825295

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jquery之锚点定位和jquery模拟锚点定位效果,页面滚动到相对应的位置时,所在的导航文字高亮显示 的相关文章

  • 如何使用 jquery 阻止或限制输入字段中的特殊字符?

    如何使用 jquery 阻止在输入字段中输入特殊字符 一个使用正则表达式的简单示例 您可以更改它以允许 禁止您喜欢的任何内容 input on keypress function event var regex new RegExp a z
  • jQuery 验证 - 只显示一条错误消息

    使用 jQuery 验证插件时 有没有办法只显示抛出的最新错误消息 我认为你应该调查一下groups and errorPlacement的选项validate方法 这应该适合你 jQuery 验证 validate 选项 http doc
  • 如何在返回的 AJAX 调用上使用 django 模板标签?

    我有一个简单的 AJAX 脚本 它在名为的搜索字段中获取输入的字符串AJAXBox并调用一个视图函数 该函数使用过滤器查询数据库并返回与输入参数匹配的所有 User 对象的查询集 当我使用 django 模板标签迭代查询集时 它不起作用 我
  • 验证动态添加的输入字段

    我用过this http docs jquery com Plugins Validation以下表单的 jquery 验证插件
  • Jquery .scrollTop() 不工作

    标题解释了我的问题 这是我的代码 my div ul scrollTop my div ul 0 scrollHeight my div 使用 AJAX 填充 因此 在我的 ajax 请求中 我有一个执行上述代码的成功回调 ajax url
  • 交换两个文本框的值

    我有两个文本框值 var pickup txt pickup var destination txt destination 我想交换这两个值 如下所示 pickup val destination val destination val
  • 通过 Ajax 加载内容

    好的 我对 ajax 和从外部加载内容还很陌生 如果您能深入了解我的问题 我将不胜感激 我目前有一个空的隐藏 div 单击链接后应在其中加载 ajax 内容 div div 我目前有一个链接列表 它们都具有相同的类 我希望在单击时 空白 d
  • jQuery - 将 href="" 的内容替换为 '#'

    我需要一种替换其中包含的文本的方法 a href a
  • 使用 jQuery 的 ASP.NET 中的模态表单

    我对 ASP NET 开发还比较陌生 到目前为止 我已经设法让事情变得简单 但我现在的需求有点复杂 到目前为止还没有得到太多乐趣 本质上我希望在单击按钮添加新用户时弹出一个模式表单 所以我发现这个在 jQuery 网站上 http jque
  • 如何使用 JavaScript 隐藏滚动条

    怎样才能隐藏滚动条呢 我想这样做是因为滚动条不太好 overflow hidden没有用 因为我的 div 元素还有许多其他元素 所以设置overflow不能解决我的问题 你可以用这个隐藏滚动条 document body style ov
  • 如何将一个div的内容克隆到另一个div

    我想使用jquery克隆将选定div的内容复制到另一个div 但我不想将它附加到任何地方 我的意思是 当我们用 jquery 克隆一个 div 时 如果我错了 请纠正我 我们必须设置它的位置 它将动态创建一个显示的新分区 但我想获取所选di
  • Chrome 中的 jQuery 动画问题

    我使用 jQuery 1 3 2 和 jQuery 颜色插件为 jQuery 中的 a 元素设置动画 我同时对 颜色 和 背景颜色 属性进行动画处理 在 IE8 和 FF 中它运行得很好 Chrome 对鼠标悬停颜色进行动画处理 然后停止
  • 如何在 jQuery 中获取选择框选项值

    如果我有这样的代码 如何获取 jQuery 中选项选择框的值
  • socket.io - ReferenceError:套接字未定义

    我正在尝试编写一个简单的应用程序 它将我在文本区域中输入的每个字符镜像到 div 使用socket io 但我不断收到以下客户端错误 ReferenceError 套接字未定义 这是我的服务器代码 var express require e
  • 关闭 bootstrap-select / select2 的自动对焦

    http www bootply com YAQrE52K6X http www bootply com YAQrE52K6X dataCombo selectpicker multiple true div class container
  • 在 jQuery 中填充文本框时执行 JavaScript 函数?

    当文本框填充文本时 如何在 JavaScript 中执行函数 文本框对用户隐藏 它将由 USB 磁卡刷卡器填充 伪代码
  • 重定向后 HTML5 CORS 请求在 safari 中失败

    我正在使用 jQuery 制作 CORS 请求来完成 SSO 类型系统 用户登录 WordPress 同时使用钩子也登录 Moodle 我遇到的问题是 在 Safari 中 仅限 safari 7 当初始 POST 请求设置为 mudles
  • 克隆 jQuery 样式?

    我有一个 span called spn1 它有一些来自内联 CSS 文件的样式 我还有一个 span called spn2 我怎样才能克隆spn1 s complete风格融入spn2 I want spn2看起来完全 风格 像spn1
  • 如何获取元素相对于当前屏幕位置的偏移量?

    我正在尝试用纯 Javascript 重构所有 jQuery 除了非常具体的值之外 一切都正常工作 根据此代码的浏览器供应商 我得到了不同的值 对于 jQuery 我会使用 var topSelected figure offset top
  • 尝试将 jquery 加载到 tampermonkey 脚本中

    我正在编写一个脚本 在加载登录页面时登录到我的大学网络 代码如下 UserScript name My Fancy New Userscript namespace http use i E your homepage version 0

随机推荐

  • c++3之static、const、friend关键字

    1 1static 修饰局部变量 延长生命周期 由栈区 gt 静态区 修饰全局变量或函数 限制作用域 只能用于本文件中使用 static 成员变量 1 static成员变量不占class的空间 修饰成员变量 需要在外部单独定义 要加来源 A
  • Think-on-Graph: Deep and Responsible Reasoning of Large Language Model with Knowledge Graph

    本文是LLM系列文章 针对 Think on Graph Deep and Responsible Reasoning of Large Language Model with Knowledge Graph 的翻译 对图的思考 基于知识图
  • TensorFlow时间序列tfts-seq2seq

    关注我的公众号YueTan进行交流探讨 欢迎关注时间序列仓库 https github com LongxingTan Time series prediction 时间序列1 概述 时间序列2 transformers 时间序列3 seq
  • Windows11 安卓子系统安装(附apk安装步骤)

    Windows11 安卓子系统安装 附apk安装步骤 系列 Android 前言 Win11安卓子系统 Windows Subsystem for Android 是一个组件 以帮助通过亚马逊商店在其上运行Android 应用程序 在最新的
  • Golang适合高并发场景的原因分析

    典型的两个现实案例 我们先看两个用Go做消息推送的案例实际处理能力 360消息推送的数据 16台机器 标配 24个硬件线程 64GB内存 Linux Kernel 2 6 32 x86 64 单机80万并发连接 load 0 2 0 4 C
  • VBA-选择文件对话框

    打开选择路径对话框 strTitle 对话框标题名 strTypesDec 选择文件类型名 多文件名时用 连接 Images All files strExten 选择文件类型 一个文件名有多个读取类型时用 连接 多个文件名用 连接 gif
  • c++ extern的用处(转载)

    转自chao yu cnblog com 1 基本解释 extern可以置于变量或者函数前 以标示变量或者函数的定义在别的文件中 提示编译器遇到此变量和函数时在其他模块中寻找其定义 此外extern也可用来进行链接指定 也就是说extern
  • ext4 mballoc之buddy算法

    buddy bitmap 根据 Ext4文件系统介绍 理论篇 nginux的博客 CSDN博客 我们知道磁盘上有1block 大小 默认4K data block bitmap 每bit位代表一个block的使用情况 1代表占用 0代表空闲
  • 机器学习主题模型之LDA参数求解——变分推断+EM近似

    由上一篇可知LDA主要有两个任务 对现有文集确定LDA模型参数 的值 或对一篇新文档 根据模型确定隐变量的分布p z w 由于无法直接求出这个后验分布 因此可以考虑使用Laplace近似 变分近似 MCMC Gibbs采样法等算法求解 1
  • 如何解决Visual Studio 2017 运行后控制台窗口一闪就消失了

    安装使用Visual Studio 2017 后 用Ctrl F5运行程序 结果控制台窗口一闪就没了 也没有出现 press any key to continue 或者 请按任意键继续 出现这种原因 主要是建立工程时选用的是 空工程 Em
  • Spring Boot日志框架Log4j 2详解(干货)

    程序开发过程中 Log日志是发现问题和分析问题的利器 可以通过打印的Log可以快速的定位bug出现的原因 好的Log信息还会提供问题的解决方法 Spring Boot 默认使用的是logback日志框架 在性能方面Log4j2更优 Spri
  • FreeRTOS之系统配置

    1 FreeRTOS的系统配置文件为FreeRTOSConfig h 在此配置文件中可以完成FreeRTOS的裁剪和配置 在官方的demo中 每个工程都有一个该文件 2 先说一下 INCLUDE 开始的宏 使用 INCLUDE 开头的宏用来
  • Hbase数据库的安装

    Linux tar 命令 菜鸟教程
  • 编码GBK不可映射字符的三种解决方法

    当我们需要输出中文时 在DOS等运行和调试代码时会出现 study01 java 3 错误 编码GBK的不可映射字符 System out println 浠婂 鐨勬棩鏈熸槸锛 day 锛 或者中文编码错误 原因 Windows CMD和P
  • IP地址与int整数的转换

    将 IP 地址转换成 10 进制整数 例如 IP 地址为10 0 3 193 把每段拆分成一个二进制形式组合起来为00001010 00000000 00000011 11000001 然后把这个二进制数转变成十进制整数就是16777312
  • CTFShow-WEB入门篇--信息搜集详细Wp

    CTFShow WEB入门篇详细Wp 信息收集 web1 web2 web3 web4 web5 web6 web7 web8 web9 web10 web11 web12 web13 web14 web15 web16 web17 web
  • 微服务项目之项目简介

    目录 项目模式 技术栈 项目架构图 模块 主模块 项目模式 电商模式 市面上有5种常见的电商模式 B2B B2C C2B C2C O2O 1 B2B模式 B2B Business to Business 是指 商家与商家建立的商业关系 如
  • 软件设计师---程序设计语言

    程序设计语言 简介 编译程序和解释程序 真题 程序设计语言基本成分 真题 传值调用和传地址调用 代码实现 真题 编译 解释程序翻译阶段 真题 符号表 真题 编译过程概述 词法分析 语法分析 语义分析 目标代码生成 小结 真题 动态语义错误
  • Axure RP PC电商平台Web端交互原型模板

    Axure RP PC电商平台Web端交互原型模板 原型图内容齐全 包含了用户中心 会员中心 优惠券 积分 互动社区 运营推广 内容推荐 商品展示 订单流程 订单管理 售后及服务等完整的电商体系功能架构和业务流程 在设计尺寸方面 本套模板按
  • jquery之锚点定位和jquery模拟锚点定位效果,页面滚动到相对应的位置时,所在的导航文字高亮显示

    1 常见的锚点定位效果 a a div 或 a href print a div 这种方法 如果网页中有顶部固定导航栏 那么常见的锚点定位就会被遮住了一部分 很难用样式弥补 于是可以考虑第二种方法 js模拟锚点跳转效果 2 js模拟锚点跳转