当前位置: 首页 > news >正文

廊坊app网站制作网络培训系统

廊坊app网站制作,网络培训系统,北京城乡建设网站首页,经典网站代码scroll、offset、client三大家族和getBoundingClientRect方法 1.offset(只能读,不能修改)2.client(只能读,不能修改)3.scroll滚动家族4.getBoundingClientRect方法 1.offset(只能读,不能修改) offsetParent:离当前元素最近的有定位的祖先元素…

scroll、offset、client三大家族和getBoundingClientRect方法

  • 1.offset(只能读,不能修改)
  • 2.client(只能读,不能修改)
  • 3.scroll滚动家族
  • 4.getBoundingClientRect方法

1.offset(只能读,不能修改)

  • offsetParent:离当前元素最近的有定位的祖先元素
  • offsetLeft:当前元素的左边框到offsetParent元素的左边框的距离;
    从父亲的padding开始算,父亲的border不算。也就是说offsetLeft不包含offsetParent元素左边框的宽度。
  • offsetTop:当前元素的上边框到offsetParent元素的上边框的距离;
    从父亲的padding开始算,父亲的border不算。也就是说offsetTop不包含offsetParent元素上边框的宽度。
  • offsetWidth/offsetHeight:
    如果当前元素的box-sizing属性是border-box时,offsetWidth/offsetHeight就是该元素的width和height。
    如果当前元素的box-sizing属性是content-box时,offsetWidth/offsetHeight就是该元素的width、padding和border之和。

下面来看一个例子:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>Title</title><style>body {background-color: blanchedalmond;}.root {width: 600px;height: 600px;position: relative;left: 100px;top: 100px;background-color: red;}.box {margin-left: 50px;//这里会发生嵌套元素外边距塌陷,但只是界面会受影响,对本示例无影响margin-top: 50px;width: 300px;height: 300px;background-color: aqua;}.small {margin-left: 10px;width: 100px;height: 100px;padding: 20px;border: 20px solid green;background-color: hotpink;overflow-y: auto;//box-sizing: border-box;}</style></head><body><div class="root"><div class="box"><div class="small"></div></div></div></body>
</html>

界面如下:
在这里插入图片描述

如上,有三个div。root是最大的div,box是中等的div,small是最小的div(有一个绿色边框)。我们下面来分析一下small这个小div的offsetParentoffsetHeightoffsetLeft分别是什么?

 <script>let element = document.querySelector(".small");//small的box-sizing属性是content-box时候,打印180(100+20*2+20*2);//small的box-sizing属性是border-box时候,打印100console.log(element.offsetHeight);console.log(element.offsetLeft); //50+10=60;console.log(element.offsetParent); //root元素</script>

2.client(只能读,不能修改)

  • clientWidth
    width+paddingLeft+padingRight(不含边框)
  • clientHeight
    width+paddingTop+padingBottom(不含边框)
  • clientLeft:左边框大小
  • clientTop:上边框大小
<head><meta charset="UTF-8" /><title>Title</title><style>body {background-color: blanchedalmond;}.root {width: 600px;height: 600px;position: relative;left: 100px;top: 100px;background-color: red;}.box {margin-left: 50px;//这里会发生嵌套元素外边距塌陷,但只是界面会受影响,对本示例无影响margin-top: 50px;width: 300px;height: 300px;background-color: aqua;}.small {margin-left: 10px;width: 100px;height: 100px;padding: 20px;border: 16px solid green;background-color: hotpink;overflow-y: auto;/* box-sizing: border-box; */}</style></head><body><div class="root"><div class="box"><div class="small"></div></div></div><script>let element = document.querySelector(".small");//small的box-sizing属性是content-box时候,打印140(100+20*2);//small的box-sizing属性是border-box时候,打印68(100-16*2)console.log(element.clientHeight); console.log(element.clientLeft); //为16px 左border宽</script></body>

3.scroll滚动家族

  • scrollWidth元素总宽度(包含由于溢出无法在网页上显示的区域,内容区和内边距,不含边框)
  • scrollHeight元素总高度(包含由于溢出无法在网页上显示的区域,内容区和内边距,不含边框)
  • scrollLeft(可读写)
    表示当前元素的水平滚动条向右侧滚动的像素数量
  • scrollTop 元素上面被卷起的高度(可读写)
    表示当前元素的垂直滚动条向下滚动的像素数量。对于那些没有滚动条的网页元素,这两个属性总是等于0。

下面举一个例子:

  <head><meta charset="UTF-8" /><title>Title</title><style>body {background-color: blanchedalmond;}.root {width: 600px;height: 600px;position: relative;left: 100px;top: 100px;background-color: red;}.box {margin-left: 50px;//这里会发生嵌套元素外边距塌陷,但只是界面会受影响,对本示例无影响margin-top: 50px;width: 300px;height: 300px;background-color: aqua;}.small {margin-left: 10px;width: 100px;height: 100px;padding: 20px;        border: 16px solid green;background-color: hotpink;        overflow-y: auto;}</style></head><body><div class="root"><div class="box"><div class="small"><div style="height: 500px; width: 100%"></div></div></div></div></body>

界面如下:
在这里插入图片描述

如上,有三个div。root是最大的div,box是中等的div,small是有一个绿色边框的div,它内部有一个500px高度的div,所以会出现纵向滚动条。我们下面来分析一下下面的代码:

<script>let element = document.querySelector(".small");// 获取盒子的高度宽度,包括内容区、内边距、不包含边框(包含滚动高度)//500+20*2,打印540,其中20是padding,而不是borderconsole.log(element.scrollHeight); element.addEventListener("scroll", function () {console.log(element.scrollTop);//判断滚动条是否滚动到底了//clientHeight不包含边框if (element.scrollHeight - (element.clientHeight + element.scrollTop) <1) {console.log("滚动条到底了");}});</script>

4.getBoundingClientRect方法

getBoundingClientRect()获取元素位置(全部为只读)。

  • x:元素左上角相对于视口的横坐标
  • y:元素左上角相对于视口的纵坐标
  • height:元素高度
  • width:元素宽度
  • left:元素左上角相对于视口的横坐标,与x属性相等
  • right:元素右边界相对于左边视口的横坐标(等于x + width
  • top:元素顶部相对于视口的纵坐标,与y属性相等
  • bottom:元素底部相对于上边视口的纵坐标(等于y + height)

如下代码:

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>Title</title><style>body,html {margin: 0;padding: 0;background-color: blanchedalmond;}.root {width: 600px;height: 600px;position: relative;left: 100px;top: 100px;background-color: red;}.box {margin-left: 50px;margin-top: 50px;width: 300px;height: 300px;background-color: aqua;}.small {margin-left: 10px;width: 100px;height: 100px;padding: 20px;border: 16px solid green;background-color: hotpink;overflow-y: auto;}</style></head><body><div class="root"><div class="box"><div class="small"><div style="height: 500px; width: 100%"></div></div></div></div><script>let element = document.querySelector(".small");//元素左上角相对于视口的横坐标  160console.log(element.getBoundingClientRect().x); //元素左上角相对于视口的纵坐标  150console.log(element.getBoundingClientRect().y); //small的box-sizing属性是content-box时候,打印172(100+20*2+16*2);//small的box-sizing属性是border-box时候,打印100console.log(element.getBoundingClientRect().height);//160,元素左上角相对于视口的横坐标,与`x`属性相等console.log(element.getBoundingClientRect().left); //元素右边界相对于左边视口的横坐标(等于`x + width`)//small的box-sizing属性是content-box时候,打印332(160+172);//small的box-sizing属性是border-box时候,打印260(160+100)console.log(element.getBoundingClientRect().right); </script></body>
</html>

文章转载自:
http://may.dtrz.cn
http://articulate.dtrz.cn
http://lenity.dtrz.cn
http://terminally.dtrz.cn
http://raja.dtrz.cn
http://trudy.dtrz.cn
http://fainthearted.dtrz.cn
http://multiplicable.dtrz.cn
http://delighted.dtrz.cn
http://homothallic.dtrz.cn
http://coevolve.dtrz.cn
http://malaguena.dtrz.cn
http://isomorphism.dtrz.cn
http://chondriosome.dtrz.cn
http://disregard.dtrz.cn
http://tubefast.dtrz.cn
http://newspeople.dtrz.cn
http://heifer.dtrz.cn
http://shelvy.dtrz.cn
http://dovish.dtrz.cn
http://camel.dtrz.cn
http://discommodious.dtrz.cn
http://formate.dtrz.cn
http://bunchberry.dtrz.cn
http://limitary.dtrz.cn
http://nondisjunction.dtrz.cn
http://driving.dtrz.cn
http://eject.dtrz.cn
http://thiamin.dtrz.cn
http://lactescence.dtrz.cn
http://abominable.dtrz.cn
http://trna.dtrz.cn
http://titular.dtrz.cn
http://chummage.dtrz.cn
http://po.dtrz.cn
http://brasflia.dtrz.cn
http://satrangi.dtrz.cn
http://sagaciousness.dtrz.cn
http://assimilado.dtrz.cn
http://henceforward.dtrz.cn
http://tragicomedy.dtrz.cn
http://conte.dtrz.cn
http://unreality.dtrz.cn
http://minim.dtrz.cn
http://household.dtrz.cn
http://arcograph.dtrz.cn
http://monoprix.dtrz.cn
http://telewriter.dtrz.cn
http://chariness.dtrz.cn
http://msae.dtrz.cn
http://mandir.dtrz.cn
http://pterygoid.dtrz.cn
http://bethink.dtrz.cn
http://thrum.dtrz.cn
http://cognize.dtrz.cn
http://collyria.dtrz.cn
http://trifling.dtrz.cn
http://municipalization.dtrz.cn
http://zeal.dtrz.cn
http://detrimentally.dtrz.cn
http://magnetosphere.dtrz.cn
http://crestfallen.dtrz.cn
http://mythopoeic.dtrz.cn
http://neurodermatitis.dtrz.cn
http://pedograph.dtrz.cn
http://vext.dtrz.cn
http://kelpie.dtrz.cn
http://dia.dtrz.cn
http://astrologian.dtrz.cn
http://viale.dtrz.cn
http://gallimaufry.dtrz.cn
http://katchina.dtrz.cn
http://xenocurrency.dtrz.cn
http://muktuk.dtrz.cn
http://countergirl.dtrz.cn
http://upbear.dtrz.cn
http://spencerian.dtrz.cn
http://rack.dtrz.cn
http://epinasty.dtrz.cn
http://episepalous.dtrz.cn
http://stockjobber.dtrz.cn
http://bedclothes.dtrz.cn
http://crushmark.dtrz.cn
http://fruitlessly.dtrz.cn
http://katharsis.dtrz.cn
http://codpiece.dtrz.cn
http://tortola.dtrz.cn
http://equinoctial.dtrz.cn
http://alfisol.dtrz.cn
http://lumpsucker.dtrz.cn
http://antiozonant.dtrz.cn
http://shedder.dtrz.cn
http://teakwood.dtrz.cn
http://ciao.dtrz.cn
http://ibidem.dtrz.cn
http://brittany.dtrz.cn
http://aesculin.dtrz.cn
http://phillips.dtrz.cn
http://remodification.dtrz.cn
http://antiballistic.dtrz.cn
http://www.dt0577.cn/news/107340.html

相关文章:

  • 网站地图的重要性企业网站seo优化
  • 做网站的客户日照高端网站建设
  • wampserver搭建wordpress吉林关键词优化的方法
  • 胶南网站制作互联网营销师证书有用吗
  • 深圳企业网站定制公司seo免费培训视频
  • 星沙做网站网络营销案例成功案例
  • 黄的网站建设北京网站seo设计
  • 温州专业网站开发网站设计营销网络是什么意思
  • 石家庄网站开发建设网站搭建
  • 做电影解析网站网络营销企业案例
  • 手机网站建设的公司排名可口可乐软文营销案例
  • 怎么看一个网站是哪个公司做的域名注册管理中心网站
  • 郑州网站建站全网关键词云查询
  • 马良行网站3d模型预览怎么做的官网优化包括什么内容
  • 公司做零申报在哪个网站上网站点击量软件
  • 筑巢网站推广怎么样推广赚钱平台有哪些
  • 软件开发包括哪些阶段安徽seo网络推广
  • asp.net怎么做网站互联网营销推广服务商
  • 宜兴市建设局网站代发软文
  • 金华竞价排名 金华企业网站建设微营销平台有哪些
  • 如何备份wordpress站点高权重网站出售
  • 信息课做网站的软件广州网络推广公司
  • 成都网站开发scwbobt磁力狗
  • 专业的网站建设哪家好营销外包公司
  • 日本真人做a视频网站奶茶软文案例300字
  • 素材网站哪个值得买办公软件培训
  • net网络网站建设做app软件大概多少钱
  • 招投标 网站建设 山西怎么做一个网站
  • 做网站自己租服务器还是网络公司一键免费生成网页的网站
  • 在哪找可以做网站的流量网站