博客列表 >第五章offset_client_scroll家族

第五章offset_client_scroll家族

刘静的博客
刘静的博客原创
2020年08月21日 22:39:49616浏览

第五章offset_client_scroll家族

本章内容介绍

核心:ECMAScript

  1. 文档对象模型:DOM

  2. 浏览器对象模型:BOM

  3. offset/client/scroll家族

  4. 事件

    4.1事件流;
    4.2事件处理程序;//1.HTML事件处理程序 2.DOM 0级事件处理程序 3.DOM 2级事件处理程序 4.IE事件处理程序
    4.3事件对象;//1.获取事件对象 2.事件目标 3.事件代理 4.事件冒泡 5.事件流阶段 6.阻止默认行为
    4.4事件对象属性;//1.坐标位置

  5. 动画:基本动画;匀速动画;缓慢动画;透明度动画;动画封装

offsetParent讲解

offset demension 偏移量
1.定位父级 offsetParent 找到有定位的父级
2.offsetLeft offsetTop
3.offsetHeight offsetWidth

html代码如下:

  1. <div id="grandFather" style="position:relative;">
  2. <div id="father">
  3. <div id="box"></div>
  4. </div>
  5. </div>

javascript代码如下:

  1. // offsetParent定义:与当前元素最近的经过定位的父级元素
  2. <!-- 1.元素自身有fixed定位,offsetParentnull style="position:fixed;"-->
  3. var box =document.getElementById('box');
  4. console.log(box.offsetParent);
  5. // 2.元素自身无fixed定位,offsetParent是body
  6. //3.元素自身无定位,父级元素存在定位,offsetParent是最近的经过定位的父级元素
  7. //4.body元素offsetParent是null
  8. console.log(document.body.offsetParent);

offsetWidth和offsetHeight讲解

css代码如下:

  1. <style type="text/css">
  2. #box{width:200px;height:200px;background-color: red;padding:10px;margin:10px;border:1px solid #0000FF;}
  3. </style>

html代码如下:

  1. <div id="box" style="width:100px;height:100px;"></div>

javascript代码如下:

  1. //offsetWidth = width + border-left-width + border-right-width + padding-left-width + border-right-width
  2. var box = document.getElementById('box');
  3. console.log(box.offsetWidth,box.offsetHeight);
  4. //只能获取行内的属性
  5. console.log(box.style.width,box.style.height);
  6. // offsetWidth,offsetHeight 是只读属性
  7. // box.offsetWidth = 500;
  8. box.style.height = 500 + 'px';
  9. box.style.width = 500 +'px';

offsetTop和offsetLeft讲解

css代码如下:

  1. <style type="text/css">
  2. *{margin:0px;margin:0px;}
  3. #father{width:400px;height:400px;position:relative;background-color:red;margin:40px;}
  4. #son{width:200px;height:100px;background-color: #0000FF;margin-left:20px;margin-top:30px;border:5px solid #ccc;}
  5. </style>

html代码如下:

  1. <div id="father">
  2. <div id="son"></div>
  3. </div>

javascript代码如下:

  1. //1.offsetTop 当前元素的上边框到offsetParent上边框的距离 与边框么有关系
  2. //1.offsetLeft 当前元素的左边框到offsetParent左边框的距离
  3. var son = document.getElementById('son');
  4. //如果有父元素定位
  5. console.log(son.offsetParent);
  6. console.log(son.offsetTop,son.offsetLeft);//0,20
  7. //如果没有父元素定位
  8. // console.log(son.offsetParent);
  9. // console.log(son.offsetTop,son.offsetLeft);//40,60
  10. //总结:相对于父元素(父元素是否有定位,如果有定位,以父元素为基准,如果没有往上寻找到,则以body为基准)

如何求出当前元素在页面上的偏移量

css代码如下:

  1. <style type="text/css">
  2. /* *{margin:0px;margin:0px;} */
  3. #grandfater{
  4. border:5px solid red;
  5. padding:30px;
  6. position:absolute;
  7. }
  8. #father{
  9. padding:20px;
  10. border:1px solid #000;
  11. position:relative;
  12. }
  13. #son{
  14. width:100px;
  15. height:100px;
  16. margin:10px;
  17. background-color: red;
  18. }
  19. </style>

html代码如下:

  1. <div id="grandfater">
  2. <div id="father">
  3. <div id="son"></div>
  4. </div>
  5. </div>

javascript代码如下:

  1. var son = document.getElementById('son');
  2. console.log(getElementLeft(son));
  3. console.log(getElementTop(son));
  4. function getElementLeft(obj){
  5. //1.获取当前元素的左边偏移量
  6. var actualLeft = obj.offsetLeft;
  7. //2.求出定位父级
  8. var parent = obj.offsetParent;
  9. //3.循环
  10. while(parent != null){
  11. //3.1求出当前的左方偏移量
  12. actualLeft = actualLeft + parent.clientLeft + parent.offsetLeft;
  13. console.log(parent);
  14. //3.2更新定位父级;
  15. parent = parent.offsetParent;
  16. }
  17. return actualLeft + 'px';
  18. }
  19. function getElementTop(obj){
  20. //1.获取当前元素的左边偏移量
  21. var actualTop = obj.offsetTop;
  22. //2.求出定位父级
  23. var parent = obj.offsetParent;
  24. //3.循环
  25. while(parent != null){
  26. //3.1求出当前的左方偏移量
  27. actualTop = actualTop + parent.clientTop + parent.offsetTop;
  28. console.log(parent);
  29. //3.2更新定位父级;
  30. parent = parent.offsetParent;
  31. }
  32. return actualTop + 'px';
  33. }

client客户端大小的使用

html代码如下:

  1. <div id="box" style="width: 200px;height: 200px;border: 1px solid #ccc;background-color: red;padding:20px;"></div>

javascript代码如下:

  1. //client 客户端大小:指的是元素内容到内边距占据的空间大小
  2. // 不包含border
  3. //1.clientWidth = width + padding-left + padding-right
  4. //2.clientHeight = height + padding-top + padding-bottom
  5. //3.clientLeft 左边框的大小
  6. //4.clientTop 上边框的大小
  7. var box = document.getElementById('box');
  8. console.log(box.clientWidth,box.clientHeight);
  9. console.log(box.clientTop,box.clientLeft);
  10. // 获取当前页面元素的大小
  11. console.log(document.documentElement.clientWidth);
  12. console.log(document.documentElement.clientHeight);
  13. //注意:1.所有client是只读的 静态失败
  14. // box.clientHeight = 400;
  15. // cosole.log(box.clientHeight);
  16. //2.如果给元素设置display:none ,客户端属性client属性都为 0
  17. //3.尽量避免重复访问这些属性
  18. console.time('time');
  19. var b = box.clientHeight;
  20. for(var i = 0;i <100000;i++){
  21. // console.log(box.clientHeight);
  22. var a = b; //time: 1.81201171875ms
  23. }
  24. console.timeEnd('time');//time: 26.23291015625ms

scrollWidth和scrollHeight讲解

css代码如下:

  1. <style type="text/css">
  2. #box{
  3. width:100px;
  4. height:100px;
  5. border:1px solid #000;
  6. padding:10px;
  7. margin:10px;
  8. overflow: scroll;
  9. }
  10. </style>

html代码如下:

  1. <div id="box">
  2. <p>内容</p>
  3. <p>内容</p>
  4. <p>内容</p>
  5. <p>内容</p>
  6. <p>内容</p>
  7. <p>内容</p>
  8. <p>内容</p>
  9. <p>内容</p>
  10. <p>内容</p>
  11. <p>内容</p>
  12. <p>内容</p>
  13. <p>内容</p>
  14. <p>内容</p>
  15. <p>内容</p>
  16. <p>内容</p>
  17. <p>内容</p>
  18. <p>内容</p>
  19. <p>内容</p>
  20. <p>内容</p>
  21. <p>内容</p>
  22. </div>
  23. <button id="btn1">向下滚动</button>

javascript代码如下:

  1. //1.scrollHeight 表示元素的总高度,包含由于溢出而在网页上面均不可见部分
  2. //1.scrollWidth 表示元素的总宽度,包含由于溢出而在网页上面均不可见部分
  3. var box = document.getElementById('box');
  4. var btn1 = document.getElementById('btn1');
  5. //1.无滚动条的时候,scrollHeight和clientHeight属性结果是相同的
  6. // console.log(box.scrollHeight);
  7. // console.log(box.scrollWidth);
  8. //2.有滚动条的时候
  9. // console.log(box.scrollHeight);
  10. // console.log(box.scrollWidth);
  11. //3.scrollTop 元素被卷起的高度
  12. // box.onscroll = function(){
  13. // console.log(box.scrollTop,box.scrollHeight);
  14. // //当滚动条滚动到底部是,符合下公式:
  15. // //scrollHeight = clientHeight + scrollTop;
  16. // }
  17. //注意:scrollTop是可读写的
  18. btn1.onclick = function(){
  19. box.scrollTop += 20;
  20. console.log(box.scrollTop);
  21. }
  22. //4.scrollLeft 元素被卷起的宽度

页面滚动

css代码如下:

  1. <style type="text/css">
  2. #box{
  3. width:100px;
  4. height: 100px;
  5. border:1px solid #000;
  6. padding:10px;
  7. margin:10px;
  8. overflow:scroll;
  9. }
  10. #btn{
  11. position:fixed;
  12. top:200px;
  13. left:30px;
  14. }
  15. </style>

html代码如下:

  1. <body style="height:2000px;">
  2. <div id="box">
  3. <p>内容</p>
  4. <p>内容</p>
  5. <p>内容</p>
  6. <p>内容</p>
  7. <p>内容</p>
  8. <p>内容</p>
  9. <p>内容</p>
  10. <p>内容</p>
  11. <p>内容</p>
  12. <p>内容</p>
  13. <p>内容</p>
  14. <p>内容</p>
  15. <p>内容</p>
  16. <p>内容</p>
  17. <p>内容</p>
  18. <p>内容</p>
  19. <p>内容</p>
  20. <p>内容</p>
  21. <p>内容</p>
  22. <p>内容</p>
  23. </div>
  24. <button id="btn">回到顶部</button>

javascript代码如下:

  1. window.onscroll = function(){
  2. //console.log(document.documentElement.scrollTop);//chrome浏览器,火狐浏览器可以,ssrifi步行
  3. //console.log(document.body.scrollTop);//chrome浏览器步行,火狐浏览器可以,srifi可以
  4. //兼容代码
  5. var doScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
  6. console.log(doScrollTop);
  7. var btn = document.getElementById('btn');
  8. btn.onclick = scrollTop;
  9. function scrollTop(){
  10. if(doScrollTop){
  11. // document.documentElement.scrollTop = 0;
  12. // document.body.scrollTop = 0;
  13. //简化写法
  14. document.documentElement.scrollTop = document.body.scrollTop = 0;
  15. }
  16. }
  17. }

实用的页面滚动

html代码如下:

  1. <body style="height:2000px;">
  2. <button id="backTop" style="position: fixed;">回到顶部</button>

javascript代码如下:

  1. var backTop = document.getElementById('backTop');
  2. backTop.onclick = function(){
  3. window.scrollTo(0,100);
  4. }
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议