跟随垂直滚动条滚动的DIV实现

来源:互联网 发布:excel表格工资表数据 编辑:程序博客网 时间:2024/05/16 14:25

方法一:js实现,缺点:有点闪

<html>
<head>
<script>
function test()
{
    document.getElementById(
"div1").style.top=document.body.scrollTop+100;//100为top:100px
}
</script>
</head>
<body onscroll="test()">
<div id="div1" style="position:absolute;top:100px;border:solid #000 1px;width:100px;height:60px">
广告
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
</body>
</html>

方法二:CSS实现

<html>
<head>
<style>{
overflow
: hidden;
padding
: 0;
margin
: 0;
}
#div1
{
width
: 100%;
height
: 100%;
overflow
: auto;
}#loading{
position
: absolute;
top
: 100;
left
:0;
z-index
:800;
}
</style>
</head>
<body>
<div id="div1">
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
<div id="loading" style="width:100%" align="center">
loading............
</div>
</body>
</html>

 

body

 

CSS方法相比JS优点是没有闪动