js判断div高度。

js判断div高度。,第1张

超出固定高度显示滚动条,只要设置style属性中的overflowY="scroll";

documentgetElementById('scroll')styleoverflowY="scroll";

获取div的高度,如果获取不到,可用下面的方法,一下方法是获取div的编辑完成后的高度:

/

   obj:需要获取属性的html对象

   prop:需要获取的obj对象的属性

  /

function getCurrentStyle (obj, prop) { 

    if (objcurrentStyle) {       

        return objcurrentStyle[prop];     

    }      

    else if (windowgetComputedStyle) {        

        propprop = propreplace (/([A-Z])/g, "-$1");           

        propprop = proptoLowerCase ();        

        return documentdefaultViewgetComputedStyle (obj,null)[prop];     

    }      

    return null;   

}   

var div = documentgetElementsByTagName("div")[0];

var height = getCurrentStyle(div,"height");

可以使用offset()方法来获取元素距离浏览器的边距,offset()

方法返回或设置匹配元素相对于文档的偏移(位置)。

工具原料:编辑器、浏览器

1、首先设置一个div然后使用offset()方法来获取其距离浏览器顶部的高度,简单的代码示例如下:

<body style="margin: 0;padding: 0;">

<div style="width: 200px;height: 200px;border: 1px solid red;margin-top:100px;"></div>

<script>

alert($('div')offset()top);

</script>

</body>

2、因为div距离顶部的距离是100像素,所以d出的应该是100,如下图:

用clientHeight或offsetHeight。

对于没有具体设置高度的元素,不能直接用height获取高度。

clientHeight表示内容可见区域高度;offsetHeight不同浏览器数值不尽相同,除了clientHeight的高度外还包括边框等的高度。

function AdjustColumnsHeight() {

    var mainCol = windowdocumentgetElementById('MainColumn');

    var leftCol = windowdocumentgetElementById('MainLeft');

    var rightCol = windowdocumentgetElementById('MainRight');

    var hMainCol =  mainCol offsetHeight;

    var hLeftCol = leftColoffsetHeight;

    var hRightCol = rightColoffsetHeight;

    var maxHeight = Mathmax( hMainCol , Mathmax(hLeftCol, hRightCol));

    mainColstyleheight = maxHeight + 'px';

    leftColstyleheight = maxHeight + 'px';

    rightColstyleheight = maxHeight + 'px';

}

上面的代码是取得3个div的高度,并判断高度最高的div高度值,让其余两个比较矮的div高度等于最高这个,自己琢磨下,很简单。

以上就是关于js判断div高度。全部的内容,包括:js判断div高度。、如何获取div距离浏览器顶部的高度,宽度,内容、怎么用js获取到外部样式表写的高度为auto的div的真实高度呢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10184083.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存