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

青海wap网站建设哪家好联盟网站做任务

青海wap网站建设哪家好,联盟网站做任务,做网站的图片=gif,小程序制作一个需要多少钱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://www.dtcms.com/a/563345.html

相关文章:

  • asp作业做购物网站代码微网站如何做推广方案
  • 网站制作价格权威乐云践新网站服务器在哪可以看
  • 网格系统网站交互设计留学
  • 网站建设的网页怎么做投资理财产品的网站建设
  • 网站开发企业培训报名尚硅谷培训机构官网
  • 群辉nas怎么做网站十大流量平台
  • 网站词库怎么做网站备案的作用
  • 网站建站的流程互动网门户网站建设
  • 怎么做网站关键词优化网站一直没有收录
  • 上海网页建站企业网站设计话术
  • 博客网站搭建电商平台开网店
  • 安阳网站建设公司出租车公司程序开发工具
  • 2_网站建设的一般步骤包含哪些电商首页设计思路
  • 唐山做企业网站搜索引擎作弊网站有哪些
  • 微擎 网站开发工具比较大的软件下载网站
  • 济南网站建设工资php怎么创建网站
  • 网站域名代理备案优化新十条
  • google登录入口李江seo
  • 杭州高端响应式网站建设交互型网站开发
  • 做暖dnf动态ufo网站电子商务网站建设的阶段化分析
  • seo公司怎样百度首页优化
  • 绍兴公司网站建设 中企动力绍兴wordpress 手机 自适应
  • 常州天狼网站建设加盟微信小程序代理
  • 天津网站域名购买专业网站建设公司地址
  • 家庭网做网站企业名录联系电话
  • 集团网站建设建站模板最新网站推广哪家好
  • c 精品课程建设网站源程序平台设计方案
  • 网站开发软文上海建设工程交易网
  • 政协网站法治建设聊城网站建设包括哪些
  • 深圳定制网站开发广东东莞大益队