如何在窗口上运行脚本调整大小?

时间:2014-09-24 13:47:47

标签: javascript jquery responsive-design window-resize onresize

我在重新调整大小时使用Jquery来保持纵横比。在小屏幕上,我想在不同的类上使用此脚本。这一切似乎在首次加载时都能正常工作。

但是当我将窗口从大> 768调整为小< 767脚本不会工作,除非我重新加载。奇怪的是,当我在一个小窗口中加载网站< 767并将其调整为大> 768时,脚本确实起作用并在重新调整大小时继续工作。知道如何解决这个问题吗?

if ($(window).width() > 768) {
    /* square featured-column1 box aanpassing */
    equalheight = function(container) {
        var currentTallest = 0,
            currentRowStart = 0,
            rowDivs = new Array(),
            $el,
            topPosition = 0;
        $(container).each(function() {
            $el = $(this);
            $($el).height('auto')
            topPostion = $el.position().top;

            if (currentRowStart != topPostion) {
                for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
                    rowDivs[currentDiv].height(currentTallest);
                }
                rowDivs.length = 0; // empty the array
                currentRowStart = topPostion;
                currentTallest = $el.height();
                rowDivs.push($el);
            } else {
                rowDivs.push($el);
                currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
            }
            for (currentDiv = 0; currentDiv < rowDivs.length; currentDiv++) {
                rowDivs[currentDiv].height(currentTallest);
            }
        });
    }
    $(window).load(function() {
        equalheight('.featured-column');
    });

    $(window).resize(function() {
        equalheight('.featured-column');
    });
    /* Aspect ratio box (responsive rechthoek) aanpassing */
    $(document).ready(function() {
        aspectResize();
        $(window).resize(aspectResize);
    });
    aspectResize = function() {
        var $aspect = $('div.up-to-date-bar');
        var width = $aspect.width();
        $aspect.height(width / 3 * 1);
    }
}
/* Aspect ratio box (responsive rechthoek) aanpassing up-to-date-column*/
if ($(window).width() < 767) {
    $(document).ready(function() {
        aspectResize();
        $(window).resize(aspectResize);
    });
    aspectResize = function() {
        var $aspect = $('div.up-to-date-column');
        var width = $aspect.width();
        $aspect.height(width / 1 * 1);
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用http://api.jquery.com/resize/在窗口调整大小时调用函数。

$( window ).resize(function() {
    // Your code here
});