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


相关推荐

  • [.NET控件]Telerik RadControls for ASP.NET AJAX 2008 Q1 net 2.0 Web.UI「建议收藏」

    [.NET控件]Telerik RadControls for ASP.NET AJAX 2008 Q1 net 2.0 Web.UI「建议收藏」这里下载TelerikRadControlsforASP.NETAJAX2008Q1net2.0Web.UI完美激活成功教程DLL文件:[Post=1]迅雷专用高速下载    Telerik.RadControls.for.ASP.NET…

    2022年7月24日
    8
  • Java和C++的区别

    Java和C++的区别Java和C++的区别:1.Java是解释型语言,所谓的解释型语言,就是源码会先经过一次编译,成为中间码,中间码再被解释器解释成机器码。对于Java而言,中间码就是字节码(.class),而解释器在JVM中内置了。2.C++是编译型语言,所谓编译型语言,就是源码一次编译,直接在编译的过程中链接了,形成了机器码。3.C++比Java执行速度快,但是Java可以利用JVM跨平台。4….

    2022年7月7日
    24
  • json到底是什么(c# json)

    &gt;如果点进来的刚巧是个高手,那……你就当是过来做个鉴定的吧。很多从事数据分析工作的伙伴都是从“最简单”的神器—Excel开始入门的,对编程语言、计算机世界的很多工具都不甚了解。要做结构化的数据,就赶紧跑去自学sql,学习sqlserver或是mysql数据库。要做半结构化、非结构化的数据又得去学习诸如mongo数据库的东西。&gt;天天听对面的程序员小哥说我给你个打包个j…

    2022年4月16日
    47
  • windows oracle11g安装教程_oracle11g安装包

    windows oracle11g安装教程_oracle11g安装包1、Oracle11gR2安装手册(图文教程)ForWindows安装前大家需要确认以下几点:你的内存没有问题(这一点很重要,如果你的机子经常蓝屏那就不要装了,不然有你哭的)你的系统已经激活计算机已安装.NetFramework4.0,不然第一步就会有弹出框告诉你“oui.exe已停止工作”即使是64位的系统也可以安装32位的Oracle2.解压两个压缩包到同一目录,即”database”…

    2022年9月15日
    0
  • Python学习:关键字global和nonlocal的用法说明

    Python学习:关键字global和nonlocal的用法说明一、globalglobal关键字用来在函数或其他局部作用域中使用全局变量。1.1如果局部要对全局变量修改,而不使用global关键字。count=0defglobal_test():count+=1print(count)global_test()会出现如下错误:1.2如果局部要对全局变量修改,应在局部声明该全局变量。co…

    2022年9月6日
    3
  • 【小白视角】大数据基础实践(五) MapReduce编程基础操作

    【小白视角】大数据基础实践(五) MapReduce编程基础操作目录1.MapReduce简介1.1起源1.2模型简介1.3MRv1体系结构1.4YARN1.4.1YARN体系结构1.4.2YARN工作流程2.MapReduce工作流程3.JavaApi要点4.实验过程最后1.MapReduce简介1.1起源在函数式语言里,map表示对一个列表(List)中的每个元素做计算,reduce表示对一个列表中的每个元素做迭代计算。它们具体的计算是通过传入的函数来实现的,map和reduce提供的是计算的框架。在MapReduce

    2022年6月22日
    31

发表回复

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

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