jquery实现侧边导航栏

时间: 2023-09-09 12:09:52 浏览: 57
HTML代码: ``` <div class="sidebar"> <ul> <li><a href="#section1">Section 1</a></li> <li><a href="#section2">Section 2</a></li> <li><a href="#section3">Section 3</a></li> <li><a href="#section4">Section 4</a></li> </ul> </div> <div id="section1" class="section">Section 1</div> <div id="section2" class="section">Section 2</div> <div id="section3" class="section">Section 3</div> <div id="section4" class="section">Section 4</div> ``` CSS代码: ``` .sidebar { position: fixed; top: 50%; left: 0; transform: translateY(-50%); } .sidebar ul { list-style: none; padding: 0; margin: 0; } .sidebar li { margin-bottom: 10px; } .sidebar a { display: block; padding: 5px; border-radius: 5px; text-decoration: none; color: #fff; background-color: #333; transition: background-color 0.3s ease; } .sidebar a:hover { background-color: #555; } .section { height: 500px; margin-bottom: 50px; } ``` jQuery代码: ``` $(document).ready(function() { // 点击侧边导航栏,滚动到指定区域 $('.sidebar a').click(function(e) { e.preventDefault(); var target = $(this).attr('href'); $('html, body').animate({ scrollTop: $(target).offset().top }, 500); }); // 监听滚动事件,高亮显示对应的导航栏 $(window).scroll(function() { var sections = $('.section'); var positions = []; sections.each(function() { positions.push($(this).offset().top - 200); }); var scrollTop = $(window).scrollTop(); var currentIndex = 0; for (var i = 0; i < positions.length; i++) { if (scrollTop >= positions[i]) { currentIndex = i; } } $('.sidebar a').removeClass('active'); $('.sidebar li').eq(currentIndex).find('a').addClass('active'); }); }); ``` 解释: 1. 点击侧边导航栏,通过`animate`函数实现页面滚动到指定区域; 2. 监听滚动事件,获取每个区域的位置信息,根据滚动位置计算出当前应该高亮显示的导航栏,并在对应的导航栏上加上`active`类名; 3. `active`类名对应的样式可以改变导航栏的背景色、字体颜色等,以突出显示当前所在的位置。

相关推荐

最新推荐

recommend-type

jQuery实现的中英文切换功能示例

主要介绍了jQuery实现的中英文切换功能,结合实例形式分析了jQuery结合插件translate.js实现中英文翻译的相关操作技巧,需要的朋友可以参考下
recommend-type

基于jquery实现点击左右按钮图片横向滚动

点击左右按钮图片横向滚动jquery,一次滚动四个,图片滚动完成,自动回到第一个版面: 效果图如下: 代码如下: &lt;!DOCTYPE html&gt; &lt;html lange=”en”&gt; &lt;head&gt; &lt;title&gt;点击左右按钮图片横向滚动&...
recommend-type

jQuery实现图像旋转动画效果

jquery动画旋转效果在项目中经常遇到这样的需求,下面小编给大家分享具体实现代码,感兴趣的朋友一起学习吧
recommend-type

jQuery实现动态给table赋值的方法示例

主要介绍了jQuery实现动态给table赋值的方法,结合具体实例形式分析了jQuery动态操作table表格节点的相关技巧,需要的朋友可以参考下
recommend-type

jquery实现input输入框实时输入触发事件代码

代码如下: &lt;input id=”productName” name=”productName” class=”wid10″ type=”text” value=”” /&gt; 代码如下: //绑定商品名称联想 $(‘#productName’).bind(‘input propertychange’, ... 实现效果:
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。