关键词scrollTop
首先,导航固定在距离页面顶端一定距离,随着下拉,导航会悬浮到最顶端
<style>
#daohang
{
width:100%;
height:60px;
background-color:#000;
position:relative;
top:0px;
left:0px;
opacity:0.6;}
</style>
<script>
var a = document.getElementById("pp");
window.onscroll=function(){
var aa=document.documentElement.scrollTop||document.body.scrollTop;
if(aa>110)
{
document.getElementById("daohang").style.position="fixed";
}
else
{
document.getElementById("daohang").style.position="relative";
}
}
</script>
</head>
<body>
<p style="font-size:36px;">你好</p>
<div id="daohang"></div>
<p id="pp">0px</p>
效果图如下
随着滚动条向下拉动,导航悬浮在最顶不再消失