900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 通过js实现对导航栏的固定

通过js实现对导航栏的固定

时间:2021-12-03 13:23:07

相关推荐

通过js实现对导航栏的固定

<style>

nav{

width: 1200px;

height: 100px;

background-color: bisque;

margin: 0 auto;

}

.con{

width: 1200px;

height: 1200px;

margin: 0 auto;

border: 2px solid red;

}

</style>

</head>

<body>

<nav>导航栏</nav>

<div class="con">内容区域</div>

<script>

let nav = document.querySelector("nav")

// 1.监听页面的滚动

document.onscroll = function(){

console.log(window.pageYOffset);

if(window.pageYOffset >= 110){

// 固定

nav.style.position = "fixed";

nav.style.top = 0;

}else{

// 静态定位

nav.style.position = "static";

}

}

</script>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。