移动滚动触发jQuery调整大小事件-(仅在使用移动设备时触发,在浏览器视口中正常)

weB*_*Ber 1 mobile jquery resize

这是我的代码,它在浏览器上很好用,但在移动设备上却不能用,不是jQuery的专家,所以如果有任何错误,请原谅我。

var width = $(window).width(), height = $(window).height();
            $(window).on('resize', function() {

                if($(window).width() != width && $(window).height() != height)
                {
                    var width = $(window).width(), height = $(window).height();

                    //do something here;
                }
            });
Run Code Online (Sandbox Code Playgroud)

weB*_*Ber 5

我在StackOverflow本身的解决方案链接中找到了答案。这sidonaldson就是帮助我解决问题的答案。

这是代码

var cachedWidth = $(window).width();
    $(window).resize(function(){
        var newWidth = $(window).width();
        if(newWidth !== cachedWidth){
            //DO RESIZE HERE
            cachedWidth = newWidth;
        }
    });
Run Code Online (Sandbox Code Playgroud)