CodePen上的响应式Grid JQuery问题

时间:2015-07-26 13:30:21

标签: jquery grid

我试图让这个响应式网格在CodePen上运行。由于某种原因,当页面加载时它没有正确排队。当我调整浏览器大小时,它会正确调整大小。如何在页面加载时让它工作?

http://codepen.io/KoniG87/pen/vyqds(原作者说,在使用百分比宽度时它只能起作用,但我尝试了像素宽度,但也没有效果。)

这是有问题的CSS:

    #big_box{
max-width:100%;
background:#fff;
margin:0 auto; 
height:auto;    
}

这是jQuery:

    $(document).ready(function() {
     $(window).resize(function() {
            var winWidth = $(window).width();
            var parentWidth = $('#big_box').innerWidth();
             var divider = parentWidth /96 ; 

                $('#big_box img').css( "margin", divider );

            if(winWidth < 321) {
            parentWidth = 320;
           $('#big_box').innerWidth('100%');
            $('.box-1').width( divider * 94) ;        //1 column
            $('.box-2').width( divider * 94) ;
            $('.box-3').width( divider * 94) ;
            $('.box-4').width( divider * 94) ;
            } 
                else if(winWidth < 480) {
            parentWidth = 480;
                  $('#big_box').innerWidth('100%');
            $('.box-1').width(divider * 46).height(divider * 46) ;    //2 col
            $('.box-2').width(divider * 94).height(divider * 46) ;
            $('.box-3').width(divider * 46).height(divider * 94) ;
            $('.box-3').css( "float","left" ) ;
            $('.box-4').width(divider * 94).height(divider * 94) ;
            } 
                else if(winWidth < 768) {
            parentWidth = 768;
                   $('#big_box').innerWidth(480);
            $('.box-1').width(divider * 30).height(divider * 30) ;    //3 col
            $('.box-2').width(divider * 62).height(divider * 30);
            $('.box-3').width(divider * 30).height(divider * 62);
            $('.box-3:first').css( "float","right" ) ;
            $('.box-4').width(divider * 62).height(divider * 62) ;
            } 
                else if(winWidth < 1024) {
            parentWidth = 1024;
                  $('#big_box').innerWidth(720);
            $('.box-1').width(divider * 22).height(divider * 22) ;    //4 col
            $('.box-2').width(divider * 46).height(divider * 22) ;
            $('.box-3').width(divider * 46).height(divider * 94) ;
            $('.box-3').css( "float","right" ) ;
            $('.box-4').width(divider * 46).height(divider * 46) ;

            }
                else if(winWidth < 1224) {
            parentWidth = 1224;
                  $('#big_box').innerWidth(960);
            $('.box-1').width(divider * 14).height(divider * 14);    //6 col
            $('.box-2').width(divider * 30).height(divider * 14) ;
            $('.box-2:first').width(divider * 62).height(divider * 30) ;
            $('#big_box img:nth-child(12)').width(divider * 62).height(divider * 30) ;
            $('#big_box img:nth-child(23)').width(divider * 62).height(divider * 30) ;
            $('.box-3').width(divider * 14).height(divider * 30) ;
            $('.box-3').css( "float","left" ) ;
            $('.box-4').width(divider * 30).height(divider * 30) ;
            }
                else {
            parentWidth = 1824;

            };
        });
$(window).trigger('resize');
});

1 个答案:

答案 0 :(得分:0)

老实说,我从未想过在没有实际调整窗口大小的情况下尝试触发调整大小。我建议采用这种方法:

function stuffToDo(){
   //do all the things
}

$(document).ready(stuffToDo);
$(window).resize(stuffToDo);

请记住,调整大小会引发大量调整,因此您可能需要考虑添加去抖功能(from David Walsh)。

// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};