滚动到一定高度就固定(fixed)导航栏

实战中运用到,mark下来

	//定位导航栏
	$(document).scroll(function(){
		var $top = parseInt($(window).scrollTop());
		if($top>=139){
			$(".stepBars").css({"position":"fixed","top":0,"z-index":5});
		}else{
			$(".stepBars").css({"position":"absolute","top":135,"z-index":5});
		}
	});

给导航栏的锚点功能添加动画效果

	$(".stepBars a").each(function(index){      //遍历导航栏的a标签
		$(this).bind("click",function(){
			var $href = $(this).attr("href");    //所得href即跳转到目的元素的选择器
			var $pos = $($href).offset().top;   //这里要换算的话记得用number()转换成数字
			$("html,body").animate({ scrollTop:$pos}, 40);			
		});
	});

滚动到相应的地方相应的锚点高亮

	//滚动到指定位置,对应锚点自动高亮
	$(window).scroll(function(){
		var $wst = $(window).scrollTop();
		var $cts_top = $("#cho_cts").offset().top-80;
		var $addr_top = $("#cho_serAddr").offset().top-80;
		var $time_top = $("#forSf_info").offset().top-80;
		var $sub_top = $("#xiadan_submit_box").offset().top-80;
		if($cts_top<=$wst){
			$(".stepBars a").removeClass("on").eq(0).addClass("on");
		}
		if($addr_top<=$wst){
			$(".stepBars a").removeClass("on").eq(1).addClass("on");
		}
		if($time_top<=$wst){
			$(".stepBars a").removeClass("on").eq(2).addClass("on");
		}
		if($sub_top<=$wst){
			$(".stepBars a").removeClass("on").eq(3).addClass("on");
		}
	});

逐个逐个来显得有点笨拙,有空研究下怎样遍历执行

转载于:https://my.oschina.net/daladida/blog/728142

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值