1

我要保持这个简短而甜蜜。在 webkit 浏览器中,我应用到 [我的网站][1] 的自动调整大小功能似乎响应缓慢,只有在调整浏览器后才会出现。下面是我的 JS 的一个片段。

function setTheHeight() {

    if( $('.col-small, .img_hover').length ) {
        //Get value of highest element
        var maxHeight = Math.max.apply(Math, $('.col-small, .img_hover').map (
            function() {
                var totalHeight = 0;

                $(this).children().each(function() {
                    totalHeight += $(this).outerHeight(); 
                });

                return totalHeight;
            }
        ));
        console.log(maxHeight);
        $('.col-small, .img_hover').height(maxHeight);
    }   
}

setTheHeight();

$(window).resize(function() {
    setTheHeight();
});

我错过了什么吗?

4

1 回答 1

1

尝试优化,无需重新计算

$('.col-small, .img_hover')

.children()

也尝试减少使用 map 和 each

也许是这样的?:

var elems = [], $elems = $('.col-small, .img_hover');

$elems.each(function(i){
    var $t = $(this);
    var $c = $t.children();
    elems[i] = { $:$t, $c:$c, nb:$c.length };
});

function getHeight(e,i){
    var total=0,n=0;
    while(n < e.nb)
        total += e.$c.eq(n++).outerHeight();
    return total;
}

function setTheHeight(){
    $elems.height( Math.max.apply(Math, $.map(elems,getHeight)) );
}

$(window).resize(setTheHeight).resize();

$elems.find("img").on("load",setTheHeight);

您也可以尝试在每个像素更改时不进行有效调整大小

var timer = false;
$(window).resize(function(){
    if(timer !== false) return; // or clearTimeout(timer);
    timer = setTimeout(function(){
        setTheHeight();
        timer = false;
    },20); // 20ms => 50fps max
}).resize();

小提琴 => http://jsfiddle.net/r043v/HvmmM/

于 2013-05-23T23:25:21.397 回答