jq 导航吸顶

注意html,body不要设置height:100%,window.addEventListener会不生效

header_topb 是你要吸顶的页面
//导航吸顶
window.addEventListener('scroll', function () {
	console.log('11');
	let t = $('body, html').scrollTop();   // 目前监听的是整个body的滚动条距离
	console.log(t);
	if (t > 0) {
		$('.header_topb').addClass('sticky')
	} else {
		$('.header_topb').removeClass('sticky')
	}
})

/* 导航吸顶 */
.sticky {
  position: fixed;
  z-index: 10000;
  width: 100%;
  top: 0px;
  border-bottom: none;
  padding: 0;
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition: all 0.5s ease-in-out;
  -ms-transition: all 0.5s ease-in-out;
  -o-transition: all 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
}

效果图

在这里插入图片描述
在这里插入图片描述

如果页面想使用滚动条样式的话,在公共样式的body设置

//vue项目加上#app ,html不需要#app
html,
body,
#app {
    width: 100%;
    /* height: 100%; */
    margin: 0px;
    padding: 0px;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-color: #535353 #fff;
    /* 滚动条轨道颜色   滚动条滑块的颜色 */
    scrollbar-width: none;
    scrollbar-width: thin;
    /* thin模式下滚动条两端的三角按钮会消失 */
    scrollbar-width: 6px;
    /* 默认大小 */
  }
  
  body::-webkit-scrollbar {
    width: 6px;
    /*滚动条宽度*/
    height: 10px;
    /*滚动条高度*/
  }
  
  body::-webkit-scrollbar-track {
    /*滚动条里面轨道*/
    -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    border-radius: 6px;
    background: #fff;
  }
  
  /*定义滑块 内阴影+圆角*/
  body::-webkit-scrollbar-thumb {
    border-radius: 6px;
    -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
    background: #535353;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值