getPos,offsetTop
2024-08-25 06:36:42
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
*{ margin:0; padding:0}
div {padding: 40px 50px;}
#div1 {background: red;position: relative;}
#div2 {background: green; position: relative;}
#div3 {background: orange; position: relative;}
</style>
<script>
window.onload = function() {
var oDiv3 = document.getElementById('div3');
var iTop = 0;//初始化
var obj = oDiv3;//默认为oDiv3
while (obj) {
iTop += obj.offsetTop;//offsetTop累加在变化
obj = obj.offsetParent;//父级也在变
//alert(obj + ' : ' + obj.id); //弹出父级
//alert( obj.id + ' : ' + obj.offsetTop );//弹出父级及offsetTop 的值
}
alert( iTop );//弹出80*{ margin:0; padding:0} 初始化了的结果 若不写初始化边距为0的情况下 在火狐谷歌360ie8以上弹出88 默认边距8px 在ie7弹出95默认边距15
}
</script>
</head>
<body id="body1">
<div id="div1">
<div id="div2">
<div id="div3"></div>
</div>
</div>
</body>
</html>
最新文章
- HTML5实现网页的全屏切换
- 我的jQuery源码读后感
- GCD 开发
- 微信支付 - V3支付问题
- IQ推理:红眼睛和蓝眼睛
- (转).net项目技术选型总结
- Gradle sourceCompatibility has no effect to subprojects(转)
- 除了使用URLSearchParams处理axios发送的数据,但是兼容性不好,其他的兼容方法
- DOM节点的创建
- Python编程从入门到实践笔记——列表简介
- UE4 打包C++项目到win32平台报错 could not find mspdbcore.dll
- 集合框架之map
- springboot热部署(二)——springboot热部署与发布
- Backpack VI
- WebService接口定义及调用
- C#复习题(概念) --C#学习笔记
- overflow 在float浮动标签里的作用
- MySQL笔记(五)MySQL 角色与SQL CHECK约束
- 阿里云上部署tomcat启动后,通过http不能访问
- dj cookie &; session组件
热门文章
- windbg的高级玩法
- 面向服务的架构SOA
- TextView展开和收回
- 【MyEcplise hibernate tools】hibernate tools的使用以及错误
- Android 实现ListView中Item被单击后背景色保持高亮
- css3 -- 背景图处理
- js运动框架tween
- sqlserver数据库 去除字段中空格,换行符,回车符(使用replace语句)
- The 2015 China Collegiate Programming Contest A. Secrete Master Plan hdu5540
- Codeforces Round #235 (Div. 2) A. Vanya and Cards