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.

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

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

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

使用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元素的clientWidth和clientHeight的值:

/**********************************************
 **  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元素的clientWidth和clientHeight :

(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!

    块级 : clientWidth和clientHeight不适用于内联HTML元素(例如span , em或a )。 它只会返回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.

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

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

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

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

clientwidth

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

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

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

相关推荐

  • 历年奥斯卡获奖影片(1927—2014)

    历年奥斯卡获奖影片(1927—2014)

    2021年11月30日
  • Vue学习之样式处理

    Vue学习之样式处理Vue学习之样式处理

    2022年4月23日
  • onedrive无限容量_onedrive家庭版扩容5t

    onedrive无限容量_onedrive家庭版扩容5t由于百度网盘的限速,不得已开始捣鼓微软的OneDrive网盘。相对于百度网盘,Onedrive最大的优势是不限速,速度也是难得的友好。相对于百度网盘,Onedrive最大的劣势是空间大小,个人用户注册仅有5G。曾经无数次因为这么小的空间而望而却步,只能硬着头皮给百度交钱。最近无意间发现了Onedrive的空间可以很大,最大的竟然达到了5T。原来Onedrive的5T版本已经流行…

    2022年9月9日
  • J2EE架构简介_手机架构

    J2EE架构简介_手机架构J2EE体系结构简介J2EE(Java2Platform,EnterpriseEdition)即Java2平台企业版,它提供了基于组件的方式来设计、开发、组装和部署企业应用。J2EE使用多层分布式的应用模型,这个多层通常通过三层或四层来实现:①客户层,运行在客户计算机上的组件。②Web层,运行在J2EE服务器上的组件。③业务层,同样是运行在J2EE服务器上的组件。

    2022年10月11日
  • ASP.NET中DropDownList 的使用

    ASP.NET中DropDownList 的使用1.如何避免DropDownList下拉框中重复值出现?AppendDataBoundItems:为是否填加重复值。真为添加,假为不填加 将DropDownList控件中AppendDataBoundItems属性设置为“False”即可。 2.如何给DropDownList添加项?//1.PreRender事件,在呈现该页前激发protectedvoidDropDow…

    2022年10月17日
  • 智能运维百度百科_智能运维决策时间单位

    智能运维百度百科_智能运维决策时间单位解读 2018 之运维篇:我们离高效智能的运维还有多远

    2022年4月21日

发表回复

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

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