简体   繁体   English

jQuery:如何在页面加载时设置变量,在页面调整大小时销毁变量,然后加载新变量?

[英]jQuery: How do I set a variable on page load, on page resize destroy the variable, and then load a new variable?

So I'm using some jQuery that I found online. 所以我正在使用我在网上找到的一些jQuery。 There's a lot of variations of it that all do the same thing to help you create divs of equal height. 它有很多变化,它们都可以帮助你创建相同高度的div。 The code I'm using is like so when I load a page. 当我加载页面时,我正在使用的代码就像这样。

    var highestCol = Math.max($('.equal_height_div1').height(),$('.equal_height_div2').height());
$('.equal_height').css('min-height', highestCol);

Works like a charm. 奇迹般有效。

To get this to work when resizing the browser window, I re-use the same code from above but just put it inside the resize function: 为了在调整浏览器窗口大小时使其工作,我重新使用上面的相同代码,但只是将其放在resize函数中:

    $(window).resize(function() {
var highestCol = Math.max($('.equal_height_div1').height(),$('.equal_height_div2').height());
$('.equal_height').css('min-height', highestCol);
});

Here's the challenge: I'm using the 1140grid with responsive layout (http://cssgrid.net/). 这是挑战:我正在使用具有响应式布局的1140grid(http://cssgrid.net/)。 It resizes divs as you resize the window. 它会在调整窗口大小时调整div的大小。 So my divs are shrinking in width and the 2nd set of code above works great when the window resizes. 所以我的div在宽度上缩小了,当窗口调整大小时,上面的第二组代码工作得很好。

The problem I'm having and would love insight from anyone is how do I destroy the value of the min height on page resize, then run the function again to get the new min-height from the tallest div? 我遇到的问题并且非常喜欢任何人的洞察力是如何在页面调整大小时破坏最小高度的值,然后再次运行该函数以从最高的div获得新的最小高度? The problem is that once the div becomes tall and I resize the window to be wider, the div needs to decrease in height but it is staying with the highest div min-height it reached. 问题是,一旦div变高,我将窗口调整为更宽,div需要降低高度,但它保持最高的div最小高度。 This will allow me to constantly update the min-height of the div with a new value and re-calcuate the actual height of the largest div. 这将允许我不断用新值更新div的最小高度,并重新计算最大div的实际高度。

Unset the min-height so that the div gets height gets recalculated. 取消设置最小高度,以便重新计算div的高度。

$('document').ready(function(){

    var $equal_height = $('.equal_height'),
        $col_a = $('.equal_height_div1');
        $col_b = $('.equal_height_div2');

    // calc the highest column
    // we assign this the return value of a function to make sure its always fresh.
    var heighest = function($col_a, $col_b){
        // Cache variables in closure
        var $col_a, $col_b;
        // this is where the magic happens
        return function(){    
            return Math.max( $col_a.height(), $col_b.height() );
        }
    }($col_a, $col_b);

    // Run once on ready to equalize columns
    $equal_height.css('minHeight', heighest);

    $(window).resize(function(){
       // By unsetting minHeight the column gets its real size back 
       $equal_height.css('minHeight', 0)
            .css('minHeight', heighest);
    });  

});

If I understand you right, you need to remove the min-attribute if you scale the width. 如果我理解正确的话,如果缩放宽度,则需要删除min属性。 Maybe it helps to set the css to the following: 也许将css设置为以下内容会有所帮助:

.css('min-height',  'none');

Maybe just a workaround but maybe it works. 也许只是一种解决方法,但也许它有效。

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

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