滚动条固定位置
2014-08-17 21:56
176 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
*{margin:0; padding:0;}
.fixed{position:fixed; top:0; left:0; width:500px;height:100px; background:#f60; margin-left:100px;}
</style>
</head>
<body>
<div style="height:300px;"></div>
<div id="test" style="height:100px; background:#f60; margin-left:100px;"></div>
<script>
var test=document.getElementById("test");
var X= test.getBoundingClientRect().top;
window.onscroll=function(){
if(document.documentElement.scrollTop>=X||document.body.scrollTop>=X){
test.className="fixed";
}else{
test.className="";
}
};
/*
var X= this.getBoundingClientRect().left;
var Y =this.getBoundingClientRect().top;
再加上滚动距离,就可以得到绝对位置
var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft;
var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop;
*/
</script>
<div style="height:1000px;"></div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
*{margin:0; padding:0;}
.fixed{position:fixed; top:0; left:0; width:500px;height:100px; background:#f60; margin-left:100px;}
</style>
</head>
<body>
<div style="height:300px;"></div>
<div id="test" style="height:100px; background:#f60; margin-left:100px;"></div>
<script>
var test=document.getElementById("test");
var X= test.getBoundingClientRect().top;
window.onscroll=function(){
if(document.documentElement.scrollTop>=X||document.body.scrollTop>=X){
test.className="fixed";
}else{
test.className="";
}
};
/*
var X= this.getBoundingClientRect().left;
var Y =this.getBoundingClientRect().top;
再加上滚动距离,就可以得到绝对位置
var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft;
var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop;
*/
</script>
<div style="height:1000px;"></div>
</body>
</html>
相关文章推荐
- div+css中如何将层随着滚动条的移动的初始位置固定在导航栏左下方
- 如何让DIV固定在页面的某个位置而不随着滚动条随意滚动
- NO.28 滚动条滚动层固定位置不变
- [转]如何让DIV固定在页面的某个位置而不随着滚动条随意滚动
- 监听滚动条实现到固定位置时触发事件
- 滚动条上下左右滚动,div始终保持在窗口的固定位置。
- js实现html页面滚动条向下拉到一定的程度时,某个div就固定在顶部,向上拉时返回原位置。
- 网页右下角固定图片(不随滚动条滑动,始终在固定位置)
- 如何让DIV固定在页面的某个位置而不随着滚动条随意滚动 (转)
- 如何让DIV固定在页面的某个位置而不随着滚动条随意滚动 (转)
- [转]如何让DIV固定在页面的某个位置而不随着滚动条随意滚动
- 如何让DIV固定在页面的某个位置而不随着滚动条随意滚动
- div固定位置,不随滚动条滚动
- 固定div位置,不随滚动条上下动
- 如何将某个层固定在网页的底部,不会随网页右边滚动条的位置而改变
- div固定位置,不随滚动条滚动
- Jquery定位插件,固定元素在页面某个位置,不随滚动条滚动
- 如何让DIV固定在页面的某个位置而不随着滚动条随意滚动
- CSS控制层让某个位置固定不会跟随滚动条移动
- 如何让DIV固定在页面的某个位置而不随着滚动条随意滚动