简体   繁体   English

如何通过 javascript 以像素为单位获取引导网格行或网格容器宽度

[英]Howto get bootstrap grid row or grid container width in pixels by javascript

Howto get bootstrap grid row or grid container width in pixels by javascript.如何通过 javascript 获取引导网格行或网格容器宽度(以像素为单位)。

I'm using aurelia for javascript but the code can be in standard js (please no jquery).我将 aurelia 用于 javascript 但代码可以是标准 js(请不要使用 jquery)。

I tried to find this on the bootstrap website but I'm still confused because there are several dimensions for width: col-xs, colmd... but in fact, we are just using 4 dimensions: col-3, col-6, col-9, col-12, and nothing more, so basically I need to get that full width in order to translate pixels to col-x again to match resizing from an HTML element.我试图在 bootstrap 网站上找到这个,但我仍然感到困惑,因为宽度有几个维度:col-xs、colmd ......但实际上,我们只是使用 4 个维度:col-3、col-6、 col-9,col-12,仅此而已,所以基本上我需要获得完整的宽度才能将像素再次转换为 col-x 以匹配从 HTML 元素调整大小。

get your element using document.querySelectorAll("your html query")[0].offsetWidth.使用 document.querySelectorAll("your html 查询")[0].offsetWidth 获取您的元素。

this command will return the element width in pixels including padding.此命令将返回以像素为单位的元素宽度,包括填充。

I've made an example in jsFiddle:我在 jsFiddle 中做了一个例子:

https://jsfiddle.net/dgf0okrj/ https://jsfiddle.net/dgf0okrj/

document.querySelectorAll("your html query")[0].offsetWidth

just open your console and run it.只需打开您的控制台并运行它。

You can update the width and max-width of bootstrap row or container or column in css:您可以在 css 中更新引导行或容器或列的宽度和最大宽度:

.row{
 max-width: 200px !important;
 width: 200px !important;
}

Alternatively you can also use % to update the width but remember to always update the max-width in your css.或者,您也可以使用 % 更新宽度,但请记住始终更新 css 中的最大宽度。

To get the width of the grid or row, or any element really, in pixels, you can do the following:要获取网格或行或任何元素的宽度(以像素为单位),您可以执行以下操作:

const pixels = document.querySelector('.grid').clientWidth;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM