JS如何设置和获取盒模型对应的宽和高

㈠方式一:通过DOM节点的 style 样式获取

 dom.style.width/height  只能获取使用内联样式的元素的宽和高。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>>js获取盒模型宽和高的方法</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #div1{
            width: 100px;
            height: 100px;
            padding: 10px;
            border:10px solid #000000;
            background: #ffff00;
        }
        #div2{
            width: 100px;
            height: 100px;
            padding: 10px;
            border:10px solid #0000ff;
            background: #ff0000;
            box-sizing: border-box;

        }
    </style>
</head>
<body>
  <p>dom.style.width/height;</p>       
 <div id="div1" style="width: 100px">哟哟哟</div>       
 <div id="div2">嘿嘿嘿</div>      
 <script>           
 var oDiv1 = document.getElementById("div1");         
 console.log(oDiv1.style.width ) ;       
 </script>
 </body>
</html>

效果图:

 缺点:通过这种方式,只能获取行内样式,不能获取内嵌的样式和外链的样式。

㈡方式二(通用型)

⑴window.getComputedStyle(dom).width/height   

⑵这种方法获取的是浏览器渲染以后的元素的宽和高,这种写法兼容性更好一些。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>>js获取盒模型宽和高的方法</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #div1{
            width: 100px;
            height: 100px;
            padding: 10px;
            border:10px solid #000000;
            background: #ffff00;
        }
        #div2{
            width: 100px;
            height: 100px;
            padding: 10px;
            border:10px solid #0000ff;
            background: #ff0000;
            box-sizing: border-box;

        }
    </style>
</head>
<body>
<p>window.getComputedStyle(element).width/height;</p>       
<div id="div1" >111</div>
<div id="div2">222</div>
<script>
var oDiv1 = document.getElementById("div1");
console.log( window.getComputedStyle(oDiv1).width ) ;
</script>

</body>
</html>

效果图:

 

㈢方式三(IE独有的)

⑴dom.currentStyle.width/height

⑵这种方法获取的是浏览器渲染以后的元素的宽和高,无论是用何种方式引入的css样式都可以,但只有IE浏览器支持这种写法。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>>js获取盒模型宽和高的方法</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #div1{
            width: 100px;
            height: 100px;
            padding: 10px;
            border:10px solid #000000;
            background: #ffff00;
        }
        #div2{
            width: 100px;
            height: 100px;
            padding: 10px;
            border:10px solid #0000ff;
            background: #ff0000;
            box-sizing: border-box;

        }
    </style>
</head>
<body>
<p>dom.currentStyle.width/height;</p>       
<div id="div1" >哟哟哟</div>
<div id="div2">呵呵呵</div>
<script>
var oDiv1 = document.getElementById("div1");
console.log( oDiv1.currentStyle.width);
</script>

</body>
</html>

效果图:

㈣方式四

⑴dom.getBoundingClientRect().width/height

⑵这种方式获得到的宽度是内容content+padding+border

⑶此 api 的作用是:获取一个元素的绝对位置。绝对位置是视窗 viewport 左上角的绝对位置。

⑷此 api 可以拿到四个属性:left、top、width、height。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>>js获取盒模型宽和高的方法</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        #div1{
            width: 100px;
            height: 100px;
            padding: 10px;
            border:10px solid #000000;
            background: #ffff00;
        }
        #div2{
            width: 100px;
            height: 100px;
            padding: 10px;
            border:10px solid #0000ff;
            background: #ff0000;
            box-sizing: border-box;

        }
    </style>
</head>
<body>
<p>dom.getBoundingClientRect().width/height;</p>       
<div id="div1" >111</div>
<div id="div2">222</div>
<script>
var oDiv1 = document.getElementById("div1");
console.log(oDiv1.getBoundingClientRect().width);
</script>

</body>
</html>

效果图:

原文地址:https://www.cnblogs.com/shihaiying/p/11600229.html

时间: 2024-08-25 23:48:03

JS如何设置和获取盒模型对应的宽和高的相关文章

使用js如何设置、获取盒模型的宽和高

第一种: dom.style.width/height 这种方法只能获取使用内联样式的元素的宽和高. 第二种: dom.currentStyle.width/height 这种方法获取的是浏览器渲染以后的元素的宽和高,无论是用何种方式引入的css样式都可以,但只有IE浏览器支持这种写法. 第三种: window.getComputedStyle(dom).width/height 这种方法获取的也是浏览器渲染以后的元素的宽和高,但这种写法兼容性更好一些. 第四种: dom.getBounding

JS设置和获取盒模型的宽和高

dom.style.width/height:只能取出内联样式的宽度和高度 dom.currentStyle.width/height:获取即时的计算的样式,但是只有IE支持 window.getComputedStyle(dom).width:获取即时计算的样式,支持其他浏览器,兼容性更好 dom.getBoundingClientRect( ).width/height:计算盒模型在页面中的绝对位置,比较少用. dom.offsetWidth/offectHeight:返回元素实际大小 一.

JS获取盒模型对应的宽高

## 获取内联样式宽高 只能获取内联设置的样式,在style或者.css文件中设置的无法获取 1 let div = document.querySelect('.test'); 2 let width = div.style.width 3 let height = div.style.height ## currentStyle和getComputedStyle获取所有样式 两者只能获取样式,不能设置样式 let div = document.querySelect('.test'); le

QT获取字符串的像素的宽与高

//设置字体 QFont font; font.setFamily("Microsoft YaHei"); font.setPointSize(8); QFontMetrics fm(font); QRect rec = fm.boundingRect("这是要获取宽度和高度的字符串"); //字符串所占的像素宽度,高度 int textWidth = rec.width(); int textHeight = rec.height(); 方便用于delegate 

【转】获取Android控件的宽和高

我们都知道在onCreate()里面获取控件的高度是0,这是为什么呢?我们来看一下示例: 首先我们自己写一个控件,这个控件非常简单: public class MyImageView extends ImageView { public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); } public MyImageView(Context context) { super(context); }

Android初级教程_获取Android控件的宽和高

通过以下方法可以在onCreate中获取宽高 1 //------------------------------------------------方法一 2 int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 3 int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 4 imageView.measure

获取浏览器和设备的宽、高

/*获取设备及浏览器的等的高度和宽度 */ var height1=window.screen.height; var width1=window.screen.width; var height2=window.screen.availHeight; var width2=window.screen.availWidth; var height3=window.screenTop; var left3=window.screenLeft; var height4=document.body.c

css盒模型

css盒子模型是为了让我们充分理解div+css模型的定位功能,盒子模型在学习div+css布局方式中必须要学习的一个模型. 那什么是css盒模型呢? 网页设计中常听到的属性名:内容(content).填充(padding).边框(border).边界(margin).css盒模型都具备这些属性.这些属性和日常生活中盒子的属性是一样的.内容就是盒子里面装的东西,而填充就是像盒子里装的一些反震的材料,边框就是箱子的本身,边界呢就像盒子之间的空隙. 盒子的模型有2种,分别是IE盒子模型和标准盒子模型

深入理解CSS盒模型

如果你在面试的时候面试官让你谈谈对盒模型的理解,你是不是不知从何谈起.这种看似简单的题其实是最不好答的. 下面本文章将会从以下几个方面谈谈盒模型. 基本概念:标准模型 和IE模型 CSS如何设置这两种模型 JS如何设置获取盒模型对应的宽和高 实例题(根据盒模型解释边距重叠) BFC(边距重叠解决方案) 基本概念 盒模型的组成大家肯定都懂,由里向外content,padding,border,margin. 盒模型是有两种标准的,一个是标准模型,一个是IE模型. 从上面两图不难看出在标准模型中,盒