描述:利用js实现类似于css fixed定位的效果,即滚动条拖动盒子的位置相对于浏览器窗口位置不改变
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style type="text/css">
body{
height:1000px;
}
div{
background-color: red;
width:100px;
height: 100px;
position: absolute;
bottom: 0;
right: 0;
}
</style>
<script type="text/javascript">
window.onscroll=function(){
var div = document.getElementById('di');
var scrollHeight =document.documentElement.scrollTop || document.body.scrollTop;
var divHeight = div.offsetHeight;
var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
var top = scrollHeight+clientHeight -divHeight+'px';
div.style.top = top;
}
</script>
</head>
<body>
<div id="di"></div>
</body>
</html>
image.png
网友评论