jQuery - 如何知道窗口是在宽度/高度还是两者都调整大小? [英] jQuery - How to know if window is resizing in width/height or both?

查看:90
本文介绍了jQuery - 如何知道窗口是在宽度/高度还是两者都调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery的函数 .resize()来调整窗口大小有一点问题。我想知道哪个尺寸越来越大 - 宽度或高度。我需要这个,因为如果我只提出两个条件 - 如果宽度比div大50px并且如果高度比div大50px,那么

I have a little problem with window resizing using jQuery's function .resize(). I would like to know which dimension is getting bigger/smaller - width or height. I need this because if I just put two conditions - if width is for 50px bigger than div and if height is for 50px bigger than div,

// (pseudocode)
if width = div.width + 50px
   width = something
if height = div.height + 50px
   height = something

然后只处理一个条件,我只能调整宽度或高度。

then is working on just one condition and I can resize only width or height.

我怎么知道哪个尺寸正在改变或者两者都是?

How could I know which dimension is changing in size or if both are?

推荐答案

通过保存最后一个窗口变量中的大小值。

By saving last window size values in variables.

var h = $(window).height(), w = $(window).width();
$(window).resize(function(){

    var nh = $(window).height(), nw = $(window).width();
     // compare the corresponding variables.
    h = nh; w = nw; // update h and w;
});

这篇关于jQuery - 如何知道窗口是在宽度/高度还是两者都调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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