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

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • redis 缓存技术与memcache的最大差别

    redis 缓存技术与memcache的最大差别

  • django验证码登录_双重认证怎么关闭

    django验证码登录_双重认证怎么关闭djoser是什么?作用:Django认证系统的REST实现。djoser库提供了一组DjangoRestFramework视图,用于处理注册、登录、注销、密码重置和帐户激活等基本操作。它适用于

  • 0xc0000225无法进系统_电脑无法启动,出现0xc0000225一到错误,该怎样解决!

    0xc0000225无法进系统_电脑无法启动,出现0xc0000225一到错误,该怎样解决!展开全部根据系统提示是引导选择失败,因为需要的设备不可访问。由此可知,可e69da5e887aa3231313335323631343130323136353331333363386666能是硬盘或者引导有故障。可依照以下步骤进行一一排查并修复。第一步,制作一个U盘启动盘,1、事先准备4G以上U盘一个,网上下载pe软件(譬如大白菜、u当家、老毛桃等等)程序。2、首先要把U盘制成pe启动盘,百度搜索…

  • LabVIEW入门教程

    LabVIEW从初学到入门LabVIEW简介如何入门LabVIEW我该去哪找相应学习资源LabVIEW简介LabVIEW是一款图形化编程语言(G语言),由美国国家仪器研制(NationalInstruments,NI)研制,被称为虚拟仪器(VirtualInstrument,VI)。它提供了整套的工具用来对信号进行采集、分析、保存及后续的处理。优点:界面美观程序模块化强与设备交…

  • java的反射(reflection)机制_腹部反射区图解

    java的反射(reflection)机制_腹部反射区图解1.什么是java反射java的反射机制是指在运行状态中,      对于任意一个类,都能知道这个类的所有属性和方法;      对于任意一个对象,都能够调用它的任意一个属性和方法;      这种动态获取的类的信息,和动态调用对象的方法的功能就是反射机制。2.反射的作用     在运行时,构造任意一个类的对象      在运行时,判断任意一个类的完整结构,比如属性,方法,内…

    2022年10月25日
  • VS2013密钥 VS2013专业版密钥 VS2013旗舰版密钥

    VS2013密钥 VS2013专业版密钥 VS2013旗舰版密钥旗舰版VisualStudioUltimate2013KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9专业版VisualStudioProfessional2013  KEY(密钥):XDM3T-W3T3V-MGJWK-8BFVD-GVPKY

发表回复

您的电子邮箱地址不会被公开。

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