简体   繁体   English

jquery - 设置浮动div的动态相等高度

[英]jquery - setting dynamic equal height of a floated div

I have 2 div containers, one navigation on the left, one content right to that: 我有2个div容器,左边有一个导航,右边有一个内容:

#leftnav_static
{
padding:5px;
margin-top: 5px;
margin-left: 5px;
height: 1000px;
width: 195px;
font-size: 1.35em;
float:left;
background-image: url('pagenav.jpg');
}

#content_dynamic
{
margin-top: 5px;
margin-left: 215px;
height: auto;
width: 700px;
padding: 5px;
background-image: url('pagenav.jpg');
font-size: 1em;
line-height:1.6em;
white-space:nowrap;
}

Now I want to set leftnav to the same height as content (no faux columns if possible): 现在我想将leftnav设置为与内容相同的高度(如果可能,不使用虚拟列):

$('#leftnav_static').height($("#content_dynamic").height());

or 要么

$('#leftnav_static').innerHeight($("#content_dynamic").innerHeight());

dont seem to work. 似乎没有工作。

any suggestions as to why that is? 有什么建议吗?

It does work. 它确实有效。 See this jsfiddle . 看到这个jsfiddle

Are you running the code in a jQuery ready block? 你在jQuery ready块中运行代码吗? Also, if you want to maintain this height relationship through text size changes from browser 'zooms', you will need to respond to resize events. 此外,如果您希望通过浏览器“缩放”中的文本大小更改来维持此高度关系,则需要响应调整大小事件。 If at some point you make your content_dynamic div have a width of auto, you will also need to resize the sidebar div when the height of the content_dynamic div changes (again, by responding to a resize event). 如果在某些时候你使你的content_dynamic div的宽度为auto,你还需要在content_dynamic div的高度发生变化时再次调整侧边栏div的大小(再次,通过响应resize事件)。

jQuery only allows you to attach to a resize event at the window level, but there are plugins that ease translating that to a div level resize event. jQuery只允许你在窗口级别附加到resize事件,但有些插件可以轻松地将其转换为div级别resize事件。

HTML: HTML:

 <div id="leftnav_static"></div>
 <div id="content_dynamic">Lorem ipsum dolor sit amet, 
   consectetur adipiscing elit. Etiam iaculis ornare 
   sapien sit amet condimentum. Aliquam a diam vel eros
   tristique fermentum vitae at turpis. Etiam fringilla,
   enim nec viverra interdum, metus tortor vehicula mauris,
   in luctus felis massa ut nulla. Proin et leo vel nunc ornare
   pulvinar. Vestibulum quis lectus  vel arcu tristique aliquet.
   Fusce malesuada nisi non ante egestas semper. 
  </div>

CSS: CSS:

#leftnav_static {
    padding:5px;
    margin-top: 5px;
    margin-left: 5px;
    height: 1000px;
    width: 195px;
    font-size: 1.35em;
    float:left;
    background-color: blue;
}

#content_dynamic {
    margin-top: 5px;
    margin-left: 215px;
    height: auto;
    width: 700px;
    padding: 5px;
    background-color: red;
    font-size: 1em;
    line-height:1.6em;
    //white-space:nowrap;  //This makes the content div only one line, 
                           //I commented this out to make the sizing clear.
 }

JavaScript : (Assuming that the jQuery library is already loaded) JavaScript :(假设已经加载了jQuery库)

$(function() {
    $('#leftnav_static').height($("#content_dynamic").height());
});

Note: The benefit of a faux columns or other pure CSS approaches is that you don't need to worry about zooming or resizes as much--and your site would work for people that have JavaScript turned off. 注意:虚拟列或其他纯CSS方法的好处是您不必担心缩放或调整大小 - 并且您的站点适用于关闭JavaScript的人。

You have to understand that you are manipulating CSS attributes. 您必须了解您正在操纵CSS属性。

var myHeight = $("#content_dynamic").css("height");
$('#leftnav_static').css({"height": myHeight});

should do the trick. 应该做的伎俩。

Add display block to #leftnav_static 将显示块添加到#leftnav_static

#leftnav_static
{
  display: block;
}

...and this will work ......这会奏效

$(document).ready(function() {
    $('#leftnav_static').height( $('#content_dynamic').height() );
});

See my example; 看看我的例子; http://jsfiddle.net/D3gTy/ http://jsfiddle.net/D3gTy/

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

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