【问题标题】:Calculate div max-height using javascript使用 javascript 计算 div 最大高度
【发布时间】:2013-04-18 14:07:00
【问题描述】:

我有一个 div,假设它夹在许多元素之间,并且位于侧边栏上。我需要计算 div 和它下面的元素之间有多少空间。然后将我的 div 的最大高度设置为它的当前高度加上下面的空间,这样它就永远不会超过那个高度(从而扩展页面)。初始的最大高度,将是一个非常小的值,应该由 JS 更改。 所以,基本上:

<thing></thing>
<div id="mydiv"></div>
<another></another>

我需要类似(jQuery 示例):

var spacebelow = space between mydiv and <another>    
var daheight = (currentheight + spacebelow); 
    $("#mydiv").attr("style", "max-height:" + daheight + "px");

有没有办法做到这一点?

【问题讨论】:

    标签: javascript jquery document


    【解决方案1】:

    我想你想要这样的东西:-

        var divoffset=$(#divID).offset();
        var divanotheroffset=$(#divanotherID).offset();
        var spacebelow =divanotheroffset.top-divoffset.top;
        var currentheight =$(#divID).height();
        var daheight = (currentheight + spacebelow); 
        //set the max-height:-
         $("#mydiv").attr("max-height",daheight );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      • 2018-02-28
      • 2014-01-03
      • 2014-03-25
      • 2015-09-24
      • 1970-01-01
      相关资源
      最近更新 更多