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)
上一篇 2022年7月22日 下午4:46
下一篇 2022年7月22日 下午5:00


相关推荐

  • 【算法】素数(质数)判断方法「建议收藏」

    【算法】素数(质数)判断方法「建议收藏」素数(质数)的判断在算法问题中经常遇到,这里小结几种常用的判断方法。首先,我们来看一下素数(质数)的定义:质数又称素数。一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数。我们可以从它的定义得到判断素数的第一个方法:从2到n-1,判断是否存在能被n整除的数,既(n%i==0,2<=i<=n-1),如果有就不是素数,否则为素数。(这里为了比

    2022年6月18日
    33
  • PowerShell应用之-事务复制

    PowerShell应用之-事务复制

    2021年8月15日
    73
  • Instruments性能检测

    Instruments性能检测2019 独角兽企业重金招聘 Python 工程师标准 gt gt gt

    2026年3月16日
    1
  • libxml2编译_etc在哪里安装

    libxml2编译_etc在哪里安装本文着重介绍解析xml的libxml2库的安装及使用,举例说明创建和解析xml的过程。是针对C语言开发人员使用你若想详细学习前端的一套东西,即xmlhtmlcssjavascript(JS)等,可以登录这个网站http://www.runoob.com/一、libxml2的安装  关于libxml2的介绍请参考官方网址http://xmlsoft.org/,下载最……

    2025年8月22日
    9
  • vue路由懒加载的3种方法

    vue路由懒加载的3种方法这里写自定义目录标题欢迎使用 Markdown 编辑器新的改变功能快捷键合理的创建标题 有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中 居左 居右 SmartyPants 创建一个自定义列表如何创建一个注脚注释也是必不可少的 KaTeX 数学公式新的甘特图功能 丰富你的文章 UML 图表 FLowchart 流程图导出与导入导出导入欢迎使用 Markdown 编辑器你好 这是你第一次使用 Markdown 编辑器所展示的欢迎页 如果你想学习如何使用 Mar

    2026年3月17日
    1
  • 无需插件只使用浏览器下载b站视频

    无需插件只使用浏览器下载b站视频2017.10.07更新:由于现在bilibili更改了refer的Host并使用了防盗链,原文的方法直接下载会有403错误,在博主琢磨出新的抓包方法之前可以先使用以下的方法:在bilibili网址前加上kan,然后回车,加载出来的东西应该就很直白了。例子:地址栏中的https://www.bilibili.com/video/av11175437/加上kan以后变成https:

    2022年7月12日
    25

发表回复

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

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