jQuery鼠标滚动/点击定位

21 篇文章 2 订阅

 演示效果

HTML,jQuery点击滚动定位

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			/* 公共样式 */
			body,input,h1,h2,h3,h4,h5,h6,b,c,e,i,g,p,div,dl,dt,dd,select,textarea,font,right,left{ margin:0; padding:0;}
			body,html{height:100%;position:relative;width: 100%;background-color: #FFFFFF;}
			ul, ol, li, dl, dd {margin: 0;padding: 0; list-style:none;}
			
			/* banner */
			.banner{width: 100%;box-sizing: border-box;overflow: hidden;height: 500px;overflow: hidden;}
			.banner img{display: block;width: 100%;height: 100%;object-fit: cover;}
			
			/* 导航 */
			.protab{width: 100%;box-sizing: border-box;display: flex;display: -webkit-flex;-webkit-justify-content: center;justify-content: center;background-color: #e9470a;}
			.protab ul{width: 1200px;display: flex;display: -webkit-flex;-webkit-justify-content: space-between;justify-content: space-between;}
			.protab ul li{width: 25%;position: relative;transition: all .3s;}
			.protab ul li:hover{background-color: rgba(0,0,0,0.2);}
			.protab ul li a{height: 70px;display: block;width: 100%;text-align: center;line-height: 70px;font-size: 15px;color: #FFFFFF;}
			.protab ul li::after{content: '';display: block;width: 0%;height: 3px;position: absolute;bottom: 0;left: 0;background-color: #FFFFFF;transition: all .3s;}
			.protab ul li.current::after{width: 100%;}
			.fixedSubNav{position:fixed;top:0;left:0;z-index:95}
			
			/* 内容css */
			.contents{width: 100%;height: 100%;}
			.contents .productcont{width: 100%;height: 100%;}
			.contents .productcont:nth-of-type(1){background-color: aliceblue;}
			.contents .productcont:nth-of-type(2){background-color: antiquewhite;}
			.contents .productcont:nth-of-type(3){background-color: aqua;}
			.contents .productcont:nth-of-type(4){background-color: aquamarine;}
			
			
		</style>
	</head>
	<body>
		<!-- banner -->
		<div class="banner">
			<img src="img/banner_01.jpg" />
		</div>
		
		<!-- 导航 -->
		<div class="protab">
			<ul>
				<li class="current"><a href="#product01">产品</a></li>
				<li><a href="#product02">服务与咨询</a></li>
				<li><a href="#product03">解决方案</a></li>
				<li><a href="#product04">典型产品</a></li>
			</ul>
		</div>
		
		<!-- 主体内容 -->
		<div class="contents">
			<div class="productcont" id="product01"></div>
			<div class="productcont" id="product02"></div>
			<div class="productcont" id="product03"></div>
			<div class="productcont" id="product04"></div>
		</div>
		
		<script type="text/javascript" src="js/jquery-2.1.0.js"></script>
		<script>
		// 滚动导航定位
		function scrollNav(){
				var  sections = $(".productcont"); 
				var  s_num = sections.length;
				var baheight=$(".banner").height();//获取banner高度
				sections.each(function(index,el){
					var _this = $(this);
					if( _this.offset().top <  $(document).scrollTop() + baheight ){
						$(".protab ul li").removeClass("current").eq(index).addClass("current");
					}
				});
				var targetTop = $(this).scrollTop();
				if (targetTop >= 300) {
					$(".protab").addClass("fixedSubNav");
				} else {
					$(".protab").removeClass("fixedSubNav");
				}
				
			}
		// 导航点击定位
		$(".protab ul li").on("click",function(e){
				var _this = $(this);
				var a = _this.children("a");
				var _href = a.attr("href");
				e.preventDefault() ;
				$("body,html").animate({
					'scrollTop': $(_href).offset().top
				});
		});
		// 给窗口添加滚动事件
		$(window).on("scroll.nav",function(e){  
				scrollNav();  // 滚动导航
		});
		$(window).trigger("scroll.nav");
	</script>
		
		
	</body>
</html>

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值