1

我创建了一个函数来评估每个部分的宽度,将它们相加,然后将容器宽度设置为所有子部分的总和。这适用于页面加载,但不适用于调整大小。下面的代码:

var sunWidth = 0;

function sizePanels(){
    var sections = $("section");

    sections.each(function(){

        var totalWidth = 0,
        select = $(this).find('.mainScroll div');

        //we loop through all child content within a section and sum it's widths
        select.each(function(index) {
            totalWidth += parseInt($(this).outerWidth(true), 10);
        });

        //here we add up all of the section widths so we can set a master width for the container
        sunWidth += parseInt(totalWidth, 10);

        //we set the width of each section
        $(this).css({ 'width' : totalWidth});

        //now we set the width of the container
        //this works great on page load, but not on resize! HERE IS THE PROBLEM
        $("#sectionWrap").css({'width' : sunWidth});

    });

}

sizePanels();
$(window).resize(sizePanels);

就像我说的,这个功能在页面加载时效果很好。但是在窗口调整大小时,$("sectionWrap")的宽度呈指数增长。我的计算方式一定有问题sunWidth。知道如何解决这个问题吗?

4

1 回答 1

2

您确实声明了sunWidth唯一一次并将其初始化为零,但每次sizePanels调用时您都会继续添加它。它应该是

function sizePanels(){
    var sunWidth = 0; // reset!
    $("section").each(…
于 2013-07-30T16:10:13.543 回答