clientwidth_JavaScript中的clientWidth和clientHeight[通俗易懂]

clientwidth_JavaScript中的clientWidth和clientHeight[通俗易懂]clientwidthUsingclientWidthandclientHeightyou’reabletogetthepixeldimensionsofanHTMLelement.ThedimensionsarecalculatedusingthedimensionsofcontentinsidetheHTMLelement,alongw…

大家好,又见面了,我是你们的朋友全栈君。

clientwidth

Using clientWidth and clientHeight you’re able to get the pixel dimensions of an HTML element. The dimensions are calculated using the dimensions of content inside the HTML element, along with the padding.

使用clientWidthclientHeight您可以获取HTML元素的像素尺寸。 尺寸是使用HTML元素内的内容尺寸以及填充来计算的。

Note: Borders, margins, or scrollbars (if present) are excluded when computing clientWidth and clientHeight

注意 :计算clientWidthclientHeight时,边框,边距或滚动条(如果存在)被排除在外

使用clientWidth和clientHeight (Using clientWidth and clientHeight)

<div id="foo">
  Hello World
</div>
const clientWidth = document.querySelector('#foo').clientWidth;
const clientHeight = document.querySelector('#foo').clientHeight;

学习练习 (Learning Exercises)

As an exercise, try calculating the value of clientWidth and clientHeight of the following HTML element:

作为练习,请尝试计算以下HTML元素的clientWidthclientHeight的值:

/**********************************************
 **  If the HTML element is <div id="foo"/>  **
 **********************************************/
const clientWidth = document.querySelector('div#foo').clientWidth;
const clientHeight = document.querySelector('div#foo').clientHeight;
console.log(clientWidth, clientHeight);
// --> 200, 100

How was it calculated? Add the padding, with the content inside the HTML element, and ignore the margins and borders:

它是如何计算的? 添加填充以及HTML元素内的内容,并忽略边距和边框:

(10 + 50) + 140   // clientWidth === 200
(30) + 70         // clientHeight === 100

Let’s try another! Try calculating the clientWidth and clientHeight of this HTML element:

让我们再试一次! 尝试计算此HTML元素的clientWidthclientHeight

(10 + 10) + 230   // clientWidth === 250
(30 + 20) + 70    // clientHeight === 120

笔记 (Notes)

  • Block-level: clientWidth and clientHeight does NOT work with inline HTML elements (like span, em, or a). It’ll just return 0!

    块级 : clientWidthclientHeight不适用于内联HTML元素(例如spanema )。 它只会返回0

  • Rounded Values: Values are rounded to the nearest integer. If you need more precise values use getBoundingClientRect()

    取整值 :将值取整到最接近的整数。 如果需要更精确的值,请使用getBoundingClientRect()

  • Read-Only: You can’t assign a new value to change the dimensions of the HTML element. For example this doesn’t do anything: someElement.clientWidth = 30

    只读 :您无法分配新值来更改HTML元素的尺寸。 例如,这什么也不做: someElement.clientWidth = 30

clientWidth and clientHeight are supported on all major desktop and mobile browsers.

所有主要的台式机和移动浏览器均支持clientWidthclientHeight

See the official W3C specs for detailed information about clientWidth and clientHeight.

有关clientWidthclientHeight详细信息,请参见W3C官方规范。

翻译自: https://www.digitalocean.com/community/tutorials/js-clientwidth-and-clientheight

clientwidth

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/163041.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据库课程设计(学校运动会管理系统)[通俗易懂]

    数据库课程设计(学校运动会管理系统)[通俗易懂]这学期学习了数据库之后,到结尾写个数据库课程设计,下面这个课程设计可是我花了好长时间才写完,先供大家参考目录1需求分析…………………………………………………………4      1.1功能性需求分析……………………………………………4      1.2性能分析……………………………………………………4      1.

    2022年5月19日
    53
  • 未将对象引用设置到对象的实例–可能出现的问题总结

    未将对象引用设置到对象的实例–可能出现的问题总结

    2021年11月29日
    40
  • 详解 CAP 定理 Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性)…

    详解 CAP 定理 Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性)…CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性)、Availability(可用性)、Partitiontolerance(分区容错性),三者不可得兼。分布式系统(distributedsystem)正变得越来越重要,大型网站几乎都是分布式的。分布式系统的最大难点,就是各个节点的状态如何同步。CAP定理是这方面的基本定理,也是理解分布式…

    2022年7月25日
    7
  • epplus word html,EPPlus简介

    epplus word html,EPPlus简介简介:Epplus是一个使用OpenOfficeXML(Xlsx)文件格式,能读写Excel2007/2010文件的开源组件功效:支持对excel文档的汇入汇出,图表(excel自带的图表基本都可以实现)的列印使用:首先应该下载Epplus的dll文件1.添加dll文件至工程bin文件中2.添加引用usingOfficeOpenXml;usingOfficeOpenXml.Drawing…

    2022年6月30日
    46
  • 天气预报代码调用

    天气预报代码调用

    2021年10月11日
    51
  • javaweb酒店管理系统源代码_java简单酒店管理系统代码

    javaweb酒店管理系统源代码_java简单酒店管理系统代码大家好,我是全微毕设团队的创始人,本团队擅长JAVA(SSM,SSH,SPRINGBOOT)、PYTHON、PHP、C#、安卓等多项技术。今天将为大家分析一个小型酒店管理系统(当前酒店管理系统已经得到了充足的发展和完善,一些大的酒店购买了功能强大、数据量处理快速、客户行为研究的软件,借此来提升酒店的核心竞争力。但是对于一些中小规模的酒店而言,昂贵的系统花费、专人的维护是不现实的,也是非常不灵活的。…

    2022年9月25日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号