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


相关推荐

  • QT安装具体图解

    QT安装具体图解文章目录QT安装包下载Qt安装QT安装包下载我下载的版本是最新的5.14.2下载链接在下面,https://download.qt.io/archive/qt/5.14/5.14.2/找到Windows版本下载(ps:如果需要下载其他版本的话点击父目录就可以去选择不同版本了,建议5.9以上。)(ps:Linux上的Qt安装日后会更新,见谅)Qt安装1.双击下载好的安装包,出现界面点击next2.根据个人情况选择填写信息,有账号就登陆,没有就注册一个(注意:注册时候的密码需要英文大写,

    2022年5月17日
    49
  • golang 2021. 激活码(JetBrains全家桶)

    (golang 2021. 激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~1…

    2022年3月27日
    260
  • 【Redis】五种数据类型「建议收藏」

    【Redis】五种数据类型「建议收藏」【Redis】五种数据类型

    2022年4月25日
    43
  • vue怎么和后端对接_vue搭配什么后端

    vue怎么和后端对接_vue搭配什么后端简单分享一下jeeplus框架部署liunx服务器跨域问题 ,因为我这个是前后端服务器分离所以配置了俩份java后端配置1.跨域后端配置nginx(图-1)上配置 server { listen 80; server_name xxx.xx.xxx; #后端服务域名 #charset koi8-r; #access_log logs/host.access.log main; locatio

    2022年8月19日
    8
  • 大数据与云计算,物联网三者的区别和关联是_云计算侧重于数据分析

    大数据与云计算,物联网三者的区别和关联是_云计算侧重于数据分析大数据与云计算  为解决互联网应用对大规模计算能力、数据存储能力的迫切需求,云计算的概念被提出。云计算是一种分布式计算平台,通过虚拟技术将海量的硬件资源和虚拟资源虚拟成虚拟资源池,并根据需求任务的大小,向虚拟资源池获取相应的计算和存储资源。  在大数据处理的需求下,出现了许多优秀的云计算平台,例如Apache开源的Hadoop、Google的MapReduce、微软的Dryad等。  在处…

    2022年10月6日
    3
  • dedecms利用memberlist标签调用自定义会员模型的会员信息

    dedecms利用memberlist标签调用自定义会员模型的会员信息

    2021年9月25日
    32

发表回复

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

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