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


相关推荐

  • JAVA遍历数组的三种方法_如何遍历一个数组

    JAVA遍历数组的三种方法_如何遍历一个数组我们也了解Java也已经很久了,那今天小编想问大家是否知道java遍历数组的方式有哪些?是不是内心已经已经有答案了?让就跟着小编的步伐一起看看吧。1.for循环遍历这是最基本的遍历方式通常遍历数组都是使用for循环来实现。遍历一维数组很简单,遍历二维数组需要使用双层for循环,通过数组的length属性可获得数组的长度。2.Arrays的toString方法debug快速查看方法利用Array…

    2022年9月19日
    2
  • MySQL数据库常见面试题总结

    MySQL数据库常见面试题总结

    2021年4月10日
    137
  • 运算放大电路在音频放大电路中的应用研究与实现「建议收藏」

    运算放大电路在音频放大电路中的应用研究与实现「建议收藏」1、导言放大电路是构成各种功能模拟电路的基础电路,也是对模拟信号最基本的处理。音频信号可以分解成若干频率的正玄波之和,其频率分为在20Hz~20KHz。不当的放大电路会造成音频信号的失真,亦会带来干扰和噪声。所有电子信息系统组成的原则都应包含:1、满足功能和性能要求,2、尽量简单,3、电磁兼容,4、调试应用简单。因此本文就来研究在不会增大电路复杂度的前提下,如何实现音频信号放大的同时对信号进…

    2022年5月29日
    35
  • SpringBoot整合RabbitMQ之实战

    SpringBoot整合RabbitMQ之实战实战前言RabbitMQ作为目前应用相当广泛的消息中间件,在企业级应用、微服务应用中充当着重要的角色。特别是在一些典型的应用场景以及业务模块中具有重要的作用,比如业务服务模块解耦、异步通信、高并发限流、超时业务、数据延迟处理等。其中课程的学习链接地址:https://edu.csdn.net/course/detail/9314RabbitMQ官网拜读首先,让我们先拜读Ra…

    2022年5月14日
    38
  • amazement的用法_release的用法

    amazement的用法_release的用法Mutex中提供了WiteOne,ReleaseMutex两个实例方法~WiteOne的作用是”阻塞当前线程,提供对该线程的原子操作”也就是说当一个线程遇到WiteOne的时候,如果在WiteOne里面没有线程在操作,则此线程进去操作而里面有线程的时候,所有到此的线程均需要排队等候里面的线程执行完毕~而控制这样操作的结束标记就是使用ReleaseMutex方法!就好比WiteO…

    2022年4月19日
    52
  • javascript高级程序设计第四版pdf下载_javascript高级程序设计第四版

    javascript高级程序设计第四版pdf下载_javascript高级程序设计第四版分享关于JavaScript高级程序设计(第3版)[美]NicholasC.Zakas著李松峰曹力译一书供大家学习!!!链接:https://pan.baidu.com/s/1RD4EXuQnTqH3kUfHWFaOYw提取码:vias复制这段内容后打开百度网盘手机App,操作更方便哦…

    2022年8月20日
    8

发表回复

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

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