博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
clientwidth_JavaScript中的clientWidth和clientHeight
阅读量:2506 次
发布时间:2019-05-11

本文共 2403 字,大约阅读时间需要 8 分钟。

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)

Hello World
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 
** **********************************************/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

    取整值 :将值取整到最接近的整数。 如果需要更精确的值,请使用

  • 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 and .

有关和详细信息,请参见W3C官方规范。

翻译自:

clientwidth

转载地址:http://bphgb.baihongyu.com/

你可能感兴趣的文章
Ubuntu下查看软件版本及安装位置
查看>>
安装IIS
查看>>
动态加载JS(转)
查看>>
shell脚本 inotify + rsync 同步脚本
查看>>
快速实现Magento多语言的设置和产品数据的多语言方法
查看>>
A - Oil Deposits(搜索)
查看>>
hLG2034Fire Maze ---BFS
查看>>
定时Job在IIS中潜在危险-IIS 定期回收
查看>>
Kafka的安装和配置
查看>>
Alpha冲刺(10/10)
查看>>
数组Array的API2
查看>>
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>
No qualifying bean of type available问题修复
查看>>
第四周助教心得体会
查看>>
spfile
查看>>
Team Foundation Service更新:改善了导航和项目状态速查功能
查看>>
0x13 链表与邻接表
查看>>
js封装设置获取cookie
查看>>
二值图像连通区域标记
查看>>
MVC in Javascript
查看>>