0

I need to assign a dynamic value to a variable. Specifically, I would like to assign the window width and detect if window has been resized.

To assign window width I know it would be as follows:

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

But I dont't know how to get window width when it has been resized, so that variable would always have the current window width.

Is that possible? I can't find the way to do that!

Than you very much in advanced!

Jur.

4

3 回答 3

1
$(window).on('resize', function() {
    var w = $(window).width();
});

演示

于 2012-11-23T14:50:08.310 回答
1

如果您这样做是为了检查窗口何时被调整大小,为什么不向窗口调整大小事件添加事件处理程序呢?

$(window).resize(function() {
    alert("resized");
});

如果您只希望函数在调整大小完成后运行一次,请尝试以下操作:

var resizeTimer;
var windowWidth;
$(window).resize(function() {
   clearTimeout(resizeTimer);
   resizeTimer = setTimeout(function() {
       windowWidth = $(window).width();
       alert("New width: " + windowWidth);
   }, 1000);
});
于 2012-11-23T14:51:08.983 回答
0

您可以使用此事件:

window.onresize = function(event) {
    windowWidth = $(window).width();
}
于 2012-11-23T14:50:18.280 回答