$(窗口).resize()不起作用 [英] $(window).resize() not working

查看:464
本文介绍了$(窗口).resize()不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下jquery代码来调整一组元素的大小,以使它们都与最高元素的高度相同.当我刷新屏幕时,该功能运行良好,但是$(window).resize()似乎不起作用.

jQuery(document).ready(function($) {

    function equal_height(target) {
        var maxHeight = -1;

        $(target).each(function() {
            maxHeight = maxHeight > $(this).height() ? maxHeight : $(this).height();
        });

        $(target).each(function() {
            $(this).height(maxHeight);
        }); 
    };

    var responsive_viewport = $(window).width();

    if (responsive_viewport >= 768) {

        equal_height('.cta-text');

        $(window).resize(function() {
            equal_height('.cta-text');
        });
    }

});

解决方案

解决了!

问题不是调用函数的方式或时间,它不起作用,因为它基于新的大小,而该大小是在第一次运行该函数时设置的.

我已将其添加到equal_height函数的开头,以将元素的高度重置为其自然高度.

$(target).each(function() { 
    $(this).height('auto');
}); 

I have the following jquery code to resize a set of elements so that they are all the same height of the tallest element. The function works fine when i refresh the screen, but the $(window).resize() doesn't seem to work.

jQuery(document).ready(function($) {

    function equal_height(target) {
        var maxHeight = -1;

        $(target).each(function() {
            maxHeight = maxHeight > $(this).height() ? maxHeight : $(this).height();
        });

        $(target).each(function() {
            $(this).height(maxHeight);
        }); 
    };

    var responsive_viewport = $(window).width();

    if (responsive_viewport >= 768) {

        equal_height('.cta-text');

        $(window).resize(function() {
            equal_height('.cta-text');
        });
    }

});

解决方案

Worked it out!

The problem isn't how or when the function is called, it's not working because it's basing the new size on the size that was set the first time the function was run.

I've added this to the start of the equal_height function, to reset the height of the element to it's natural height.

$(target).each(function() { 
    $(this).height('auto');
}); 

这篇关于$(窗口).resize()不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆