1.

offsetParent  获取的最近的定位的父元素
  offsetLeft/offsetTop  是相对于offsetParent的距离

offsetHeight/offsetWidth 获取盒子的大小   border + height + padding

<style>
#box {
width: 200px;
height: 200px;
background-color: #ff0000; }
#child {
width: 100px;
height: 100px;
margin-left: 10px;
background-color: #00ff00;
}
</style>
</head>
<body>
<!--offsetParent--> <div id="box"> <div id="child"> </div> </div> <script>
var child = document.getElementById("child");
//border + padding + width
//offsetLeft获取的是相对于offsetParent的距离
console.log(child.offsetLeft);
//如果设置的子盒子有定位的父元素,offsetParent就是定位的父元素
//此处子盒子的父元素没有定位,offsetParent就是body
console.log(child.offsetParent);
//父节点--box
console.log(child.parentNode);
</script>

2  scrollHeight  滚动内容的高度(height + padding  不包括边框)

 <style>
#box{
width: 100px;
height: 100px;
background-color: #0000ff;
}
</style>
</head>
<body>
<div id="box">
哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈
</div>
<script>
function $$(id){
return document.getElementById(id);
}
</script>
<script>
var box=$$("box");
console.log(box.scrollHeight);
//获取撑开的大小
console.log(box.offsetHeight);
//获取盒子的大小
</script>

注册 onscroll  滚动事件

<script>
// var scrollTop=document.body.scrollTop||document.documentElement.scrollTop;
//滚动条事件
window.onscroll=function(){
console.log(scroll().scrollTop);
} </script>

//4 获取页面滚动出去的距离
    //document.body.scrollLeft ||
document.documentElement.scrollLeft
    //document.body.scrollTop ||
document.documentElement.scrollTop
这里会产生浏览器的兼容问题,ie8记一下版本不支持document.body.scrollLeft,只支持document.documentElement.scrollLeft因此我们需要封装这个函数

/**scrollTop与scrollLeft兼容性封装
*
* @returns {{scrollTop: number, scrollLeft: number}}
*/
function scroll(){
return{
scrollTop:document.body.scrollTop||document.documentElement.scrollTop,
scrollLeft:document.body.scrollLeft||document.documentElement.scrollLeft
};
}

动画函数的封装

//获取任意样式
function getStyle(element, attr) {
if(window.getComputedStyle) {
return window.getComputedStyle(element,null)[attr];
}else{
return element.currentStyle[attr];
}
}
//在修改单个属性的基础上修改
//1 修改参数,传入对象
//2 遍历对象,获取到所有的属性
//3 把target修改 成 attrs[attr] function animate(element, attrs, fn) {
//清除定时器
if(element.timerId) {
clearInterval(element.timerId);
} element.timerId = setInterval(function () {
//问题:当多个属性的最小值到达之后,动画就会停止
//解决:当所有属性都到达目标值,动画停止 //假设所有的属性都到达目标值,停止定时器
var isStop = true;
//遍历多个属性
for(var attr in attrs) { if(attr === "zIndex") {
element.style[attr] = attrs[attr];
}else if(attr === "opacity") {
//获取当前元素样式属性的值
var current = parseFloat(getStyle(element, attr)) || 0;
current *= 100;
//每一次step的值会越来越小
var step = (attrs[attr]*100 - current)/8; //三目运算符判断步长为正数向上取整 如果为负数向下取整
step = step > 0 ? Math.ceil(step): Math.floor(step); current += step; element.style[attr] = current/100; //更改ie下的透明度(透明度的兼容问题)
element.style.filter = "alpha(opacity="+ current +")"; //如果有一个属性没有到达目标值,不能停止动画
if(step != 0) {
isStop = false;
}
}else{
//获取当前元素样式属性的值
var current = parseInt(getStyle(element, attr)) || 0;
//每一次step的值会越来越小
var step = (attrs[attr] - current)/8; //正数 向上取整 负数 向下取整
step = step > 0 ? Math.ceil(step): Math.floor(step); current += step; element.style[attr] = current + "px"; //如果有一个属性没有到达目标值,不能停止动画
if(step != 0) {
isStop = false;
}
} } //停止定时器

最新文章

  1. Socket网络编程一
  2. B样条曲线曲面(附代码)
  3. ASP.NET利用WINRar实现在线解压缩文件
  4. PowerShell脚本:随机密码生成器
  5. MVC项目实践,在三层架构下实现SportsStore-02,DbSession层、BLL层
  6. [转]EntityFramework走马观花之CRUD(中)
  7. LeetCode258:Add Digits
  8. ZooKeeper 安装部署及hello world
  9. Qt4--加密日记本(子例化QMainWindow文本加密解密)
  10. Duanxx的技术问题:word界面显示模糊
  11. Android之RecyclerView入门
  12. 安卓Acitivity的启动模式
  13. oracle sql developer 出现 : 适配器无法建立连接问题解决方案 The Network Adapter could not establish the connection
  14. DAX/PowerBI系列 - 参数表(Parameter Table) - 大客户分析(Top N)
  15. R语言通过loess去除某个变量对数据的影响--CNV分析
  16. django-models层
  17. mac外接显示器 双屏同时滑动问题
  18. 【opencv基础】imread-第二个参数
  19. 阿里云 邮件发送(Python)
  20. 预留端口避免占用ip_local_reserved_ports

热门文章

  1. csv文件批量导入数据到sqlite。
  2. Codeforces 721D [贪心]
  3. XE6移动开发环境搭建之IOS篇(8):在Mac OSX 10.8中安装XE6的PAServer(有图有真相)
  4. AX 2012 Form and Parts
  5. AX Dynamic 2012 tabletype:TempDB使用
  6. 65. Reverse Integer &amp;&amp; Palindrome Number
  7. Web服务器禁止range请求
  8. C# 将DataTable存储到DBF文件中
  9. win7,xp通用的打开文件浏览对话框的方法
  10. leetcode 160