js固定在顶部
2015-10-19 17:18
691 查看
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1" />
<title></title>
<style>
*{margin: 0px;padding: 0px;}
html{overflow: auto;position: relative;}
.div1{width: 100%;height: 3rem;background: #8A2BE2;}
.div2{width: 100%;height: 3rem;background: #D2691E;}
.div3{width: 100%;height: 4rem;background: #E5EECC;text-align: center;line-height: 4rem;}
.div4{width: 100%;height: 9rem;background: #FF0000;}
.div5{width: 100%;height: 9rem;background: #FFA500;}
</style>
</head>
<body>
<script>
var iWidth=document.documentElement.getBoundingClientRect().width;
iWidth=iWidth>540?540:iWidth;
document.getElementsByTagName("html")[0].style.fontSize=iWidth/10+"px";
</script>
<div class="wrap">
<div class="div1"></div>
<div class="div2"></div>
<div class="div3" id="fixed">固定</div>
<div class="div4"></div>
<div class="div5"></div>
</div>
</body>
<script src="js/jquery-1.11.3.js"></script>
<script>
window.onload=function(){
var fixedTop = document.getElementById('fixed').offsetTop;
document.ontouchmove = function() {
move();
}
window.onscroll=function(){
move();
}
function move(){
var t = document.documentElement.scrollTop || document.body.scrollTop;
if (t >= fixedTop) {
document.getElementById('fixed').style.position = "fixed";
document.getElementById('fixed').style.top=0;
} else {
document.getElementById('fixed').style.position = "static";
};
console.log(document.getElementById('fixed').offsetTop)
}
}
</script>
</html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1" />
<title></title>
<style>
*{margin: 0px;padding: 0px;}
html{overflow: auto;position: relative;}
.div1{width: 100%;height: 3rem;background: #8A2BE2;}
.div2{width: 100%;height: 3rem;background: #D2691E;}
.div3{width: 100%;height: 4rem;background: #E5EECC;text-align: center;line-height: 4rem;}
.div4{width: 100%;height: 9rem;background: #FF0000;}
.div5{width: 100%;height: 9rem;background: #FFA500;}
</style>
</head>
<body>
<script>
var iWidth=document.documentElement.getBoundingClientRect().width;
iWidth=iWidth>540?540:iWidth;
document.getElementsByTagName("html")[0].style.fontSize=iWidth/10+"px";
</script>
<div class="wrap">
<div class="div1"></div>
<div class="div2"></div>
<div class="div3" id="fixed">固定</div>
<div class="div4"></div>
<div class="div5"></div>
</div>
</body>
<script src="js/jquery-1.11.3.js"></script>
<script>
window.onload=function(){
var fixedTop = document.getElementById('fixed').offsetTop;
document.ontouchmove = function() {
move();
}
window.onscroll=function(){
move();
}
function move(){
var t = document.documentElement.scrollTop || document.body.scrollTop;
if (t >= fixedTop) {
document.getElementById('fixed').style.position = "fixed";
document.getElementById('fixed').style.top=0;
} else {
document.getElementById('fixed').style.position = "static";
};
console.log(document.getElementById('fixed').offsetTop)
}
}
</script>
</html>
相关文章推荐
- js原生弹出窗
- 使用JS的FormData对象
- CommonJS Modules/1.0 规范
- js中操作cookie
- javascript 自定义事件
- javascript的一点误解
- javascript:设置URL参数的方法,适合多条件查询
- js验证身份证号有效性并提示对应信息
- js基础知识
- 写了10年Javascript未必全了解的连续赋值运算
- js变量以及其作用域详解
- 两种JS方法实现斐波那契数列
- 关于json解析
- 项目____JSP中文乱码问题总结
- Javascript去除回车换行符号和PHP去除回车换行符的方法
- asp.net javascript客户端调用服务器端方法
- jsp/JSTL/javascript 变量访问
- MVC应用程序JsonResult()的练习
- JavaScript == 与===
- 用Jsoup爬取全国省到村名字