滚动条 固定导航栏

onscroll方法

onscroll 事件在元素滚动条在滚动时触发。

css代码

.co{
   position: fixed;
    top: 0px;
}
.top_menu{
导航栏的样式
}

 

js代码

 

window.onload = function () {
 onscroll = function () {
        var t = document.documentElement.scrollTop || document.body.scrollTop;获取当前页面的滚动条纵坐标位置
     if ( t >= 50 ) {
        console.log( t )
              document.getElementById(
"top_menu" ).className = "top_menu co";
        }
else {
              document.getElementById(
"top_menu" ).className = "top_menu";
        }
      }
    }

当滚动条运行大于 t 设定的值后  

执行document.getElementById( "top_menu" ).className = "top_menu co";   

给原有的导航栏在加上一个已经封装好的class  

实现滚动条滚动时 导航栏固定  

当 t 小于设定值时 

执行document.getElementById( "top_menu" ).className = "top_menu";

恢复原来样式

 

转载于:https://www.cnblogs.com/gaojian910/p/11021872.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值