jq实现导航条根据滚动条高度聚焦,点击导航锚点到对应内容,垂直滚动聚焦导航,导航自动聚焦

先上代码,解释留到最后

html

<!--start 文章内容-->
	<div class="artical-contanner" style="width: 500px; margin: 0 auto;padding: 20px;">
		<!--模块1-->
		<div class="box article1">
			<div class="title">CSS</div>
			<div class="art" style="height: 500px;background-color: #0000FF;">
				此处很多内容..........
			</div>
		</div>
		<!--模块2-->
		<div class="box article1">
			<div class="title">javascript</div>
			<div class="art" style="height: 800px; background-color: #0064CF;">
				此处很多内容..........
			</div>
		</div>
		<!--模块3-->
		<div class="box article1">
			<div class="title">html</div>
			<div class="art" style="height: 1000px; background-color: #ccc;">
				此处很多内容..........
			</div>
		</div>
		<!--模块4-->
		<div class="box article1">
			<div class="title">jQuery</div>
			<div class="art" style="height: 1000px; background-color: #c9b8d7;">
				此处很多内容..........
			</div>
		</div>
		<!--模块5-->
		<div class="box article1">
			<div class="title">nodejs</div>
			<div class="art" style="height: 1000px; background-color: #c6e2bc;">
				此处很多内容..........
			</div>
		</div>
		<!--模块6-->
		<div class="box article1">
			<div class="title">vue</div>
			<div class="art" style="height: 1000px; background-color: #A2B1C1;">
				此处很多内容..........
			</div>
		</div>
		
		...
	</div>
	<!--end 文章内容-->
	
	<!--start 右侧导航-->
		<ul class="right-nav">
			<li class="item active">CSS</li>
			<li class="item">javascript</li>
			<li class="item">html</li>
			<li class="item">jQuery</li>
			<li class="item">nodejs</li>
			<li class="item">Vue</li>
		</ul>
	<!--end 右侧导航-->

 css

/*start 文章内容*/
   		.title{
   			font-size: 26px;
   			font-weight: bold;
   			border-bottom: 1px solid #ccc;
   			margin-bottom: 20px;
   			padding-bottom: 20px;
   		}
   		.box{
   			padding: 20px;
   			border: 1px solid #ccc;
   			border-radius: 2px;
   			margin-bottom: 20px;
   		}
   		.art{
   			padding: 20px;
   		}
   		body{
   			transition: all ease 1s;
   		}
   		/*end 文章内容*/
   		/*start 右侧导航*/
   		ul,li{
   			list-style: none;
   		}
   		.right-nav{
   			position: fixed;
   			width: 100px;
   			top: 50%;
   			right: 10px;
   			border: 1px solid #ccc;
   			border-radius: 6px;
   			padding: 2px;
   			transform: translate(0,-50%);
   			background-color: rgba(225,225,216,.8);
   		}
   		.right-nav .item{
   			height: 50px;
   			line-height: 50px;
   			text-align: center;
   			font-size: 16px;
   			border-bottom: 1px solid #ccc;
   			border-radius: 2px;
   			-webkit-user-select: none;
   		}
   		.right-nav .item:hover{
   			cursor: pointer;
   			background-color: #ccc;
   		}
   		.right-nav .item:last-child{
   			border-bottom: none;
   		}
   		.right-nav .item.active{
   			background-color: #6b6b6b;
   			color: #fff;
   		}
   		/*end 右侧导航*/

jq、javascript

$(window).scroll(function(e){
			setNavActive()
		})
		
		var titleArr = $('.title');
		//聚焦
		function setNavActive(){
			if(isClick) return ''; 
			var stop = $(document).scrollTop();
			for(var i = titleArr.length - 1; i >= 0; i--){
				if($(titleArr[i]).offset().top - $(window).height()/2 <= stop){
					$('.right-nav .item').eq(i).addClass('active').siblings().removeClass('active');
					break;
				}
			}
		}
		
		var isClick = false,
		timer = 0;
		//锚点
		$('.right-nav .item').click(function(){
			isClick = true;
			clearTimeout(timer);
			var index = $(this).index();
			$(this).addClass('active').siblings().removeClass('active');
			$("body, html").stop().animate({
                scrollTop: $('.title').eq(index).offset().top - 40
            });
            timer = setTimeout(function(){
            	isClick = false;
            },1000)
		});

原理

这里的功能细分的话有两部分,一部分是随滚动条的移动右侧导航自动聚焦。二是点击右侧导航条滚动条滚动到对应内容。

首先要获取所有对应内容距离页面顶部的距离,通过遍历(注意是逆序遍历),通过逆序遍历每次和滚动条顶部距离对比,如果小于滚动条距离的此时给右侧导航对应选项聚焦、移除其他聚焦同时跳出遍历即可。

至于点击导航条让滚动条滚动到对应内容则更简单,如果把对应内容和导航条选项都看做一个数组的话,那么两个数组的元素个数是一一对应的。只要获取点击导航元素的下标,再让滚动条滚动到对应内容的页面高度即可。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
UniApp 是一个基于 Vue.js 的跨平台应用开发框架,可以同时构建 iOS、Android 和 Web 应用。关于导航,UniApp 提供了一个内置的组件 `uni-page-scroll`,用于实现页面内的导航。 使用 `uni-page-scroll` 组件,你需要在页面中设置多个点,并在导航菜单中设置对应点链接。具体的步骤如下: 1. 在页面中设置点:在需要跳转的位置标签上添加 `id` 属性,作为点的标识。例如: ```html <!-- 页面内容 --> <div id="section1">Section 1</div> <div id="section2">Section 2</div> <div id="section3">Section 3</div> ``` 2. 在导航菜单中设置点链接:使用 `<a>` 标签,并设置 `href` 属性为对应点标识。例如: ```html <!-- 导航菜单 --> <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> </ul> ``` 3. 在页面中使用 `uni-page-scroll` 组件:将 `uni-page-scroll` 组件放置在需要实现导航的位置,并设置 `scroll-into-view` 属性为当前显示的点标识。例如: ```html <!-- 页面 --> <template> <view> <uni-page-scroll scroll-into-view="{{currentView}}"> <!-- 页面内容 --> <div id="section1">Section 1</div> <div id="section2">Section 2</div> <div id="section3">Section 3</div> </uni-page-scroll> </view> </template> <script> export default { data() { return { currentView: 'section1' // 默认显示第一个点 } } } </script> ``` 通过以上步骤,你就可以实现 UniApp 中的导航了。当点导航菜单中的链接时,页面会滚动对应点位置。希望对你有帮助!如果有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值