优化webkit浏览器的javascript浏览器大小调整器
我要保持这个简短而甜蜜。在webkit浏览器中,我在[my site][1]上应用的自动调整大小功能的响应似乎很慢。只有在调整浏览器后,事情才会发生。下面是我的JS的一个片段优化webkit浏览器的javascript浏览器大小调整器,javascript,jquery,html,webkit,window-resize,Javascript,Jquery,Html,Webkit,Window Resize,我要保持这个简短而甜蜜。在webkit浏览器中,我在[my site][1]上应用的自动调整大小功能的响应似乎很慢。只有在调整浏览器后,事情才会发生。下面是我的JS的一个片段 function setTheHeight() { if( $('.col-small, .img_hover').length ) { //Get value of highest element var maxHeight = Math.max.apply(Math, $('.
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();
});
我遗漏了什么吗???尝试优化,不需要在某些时候重新计算
$('.col-small, .img_hover')
像
.children()
此外,还应尽量减少使用map和每个
也许是这样的事
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);
fiddle=>尝试优化,无需在某些时候重新计算
$('.col-small, .img_hover')
像
.children()
此外,还应尽量减少使用map和每个
也许是这样的事
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);
fiddle=>更改最后一次的getHeight,[n++]不工作您需要使用。eq(n++)和参数反转,i和elet us更改最后一次的getHeight,[n++]不工作您需要使用。eq(n++)和参数反转,i和elet us