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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • QQ农场怎么开挂_qq农场矿山辅助

    QQ农场怎么开挂_qq农场矿山辅助很久没更新博客了,今天就拿开发QQ开心农场外挂的一些问题来写篇短日志充个数。 关于登陆       在web上,QQ登陆的时候是将密码加密后再提交了,打开http://xiaoyou.qq.com,找到了密码加密过程在http://imgcache.qq.com/campus/js/Mlogin.js 文件中,通过解读代码可以了解到,QQ一共进行了4次的MD5运算,具体过程如下:比如

    2025年11月2日
    4
  • 2015年逻辑真题难度(2015年逻辑真题)

    说说你对数据库读写分离的理解读写分离,基本的原理是让主数据库处理事务性增、改、删操作(INSERT、UPDATE、DELETE),而从数据库处理SELECT查询操作。数据库复制被用来把事务性操作导致的变更同步到集群中的从数据库。为什么要分库、分表、读写分?单表的数据量限制,当单表数据量到一定条数之后数据库性能会显著下降。数据多了之后,对数据库的读、写就会很多。分库减少单台数据库的压力。接触过几个分库分表的系统,都是通过主键进行散列分裤分表的。这类数据比较特殊,主键就是唯一的获取该条信息的主要途径。比如

    2022年4月18日
    34
  • 用swift开发仪表盘控件(一)

    用swift开发仪表盘控件(一)

    2022年2月6日
    53
  • Axios跨域请求配置「建议收藏」

    Axios跨域请求配置「建议收藏」    React中用到的网络代理请求是axios,它在使用的过程中,会存在一些问题,今天就分享我遇到过的一些问题1、跨域请求后台配置:在SpringBoot项目中的,需要实现WebMvcConfigurer,并且重写addCrosMappings方法@ConfigurationpublicclassApplicationAutoConfigurationimplementsWebMvcConfigurer{ @Override public

    2025年10月26日
    3
  • 电脑显示已连接网络但是无internet访问

    电脑显示已连接网络但是无internet访问电脑显示已连接网络但是无internet访问自己电脑不知道出什么问题了,连接到无限网络但是无法访问,一开始以为是自己双系统的问题,然后自己在ubuntu那边是可以正常上网的。下面是自己的解决方法方法一:首先打开这个网络的状态,在属性这里,点击进入:打开这个ipv4进行配置,把所有的ip,dns都设置为自动获取地址:如果此时还没有用,在此基础上,再进一步进…

    2022年7月23日
    16
  • emule最新服务器地址,emule更新服务器列表

    emule最新服务器地址,emule更新服务器列表《emule更新服务器列表》由会员分享,可在线阅读,更多相关《emule更新服务器列表(15页珍藏版)》请在人人文库网上搜索。1、emule是通过ED2K网络和KAD网络寻找、连接其他emule客户端的,所以服务器列表和KAD节点文件是emule的必需文件。有些新手由于下载官方原版emule压缩包或其他未集成这些必需文件的emule压缩包,从而出现“连接不上ED2K与KAD”问题。所以学会下载更新…

    2022年6月29日
    494

发表回复

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

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