Miguel Machado
Miguel Machado

Reputation: 298

How to detect window width changes without resizing

I already looked for the .resize() jquery function but the jquery .resize() only triggers when the window is being resized, what i wanted was a trigger that shoots when the width changes for example, instead of changing the browser width the user clicks in the button to maximize and the function fails to trigger, i have a function that fires a function on resize(), is there any function that is like on("windowwidthchanges") ?

Upvotes: 2

Views: 1240

Answers (3)

yezzz
yezzz

Reputation: 3020

You can move the code to a named function and call it on resize and when clicking the button.

Or, you can trigger the resize when clicking the button:

$("button").on("click", function(){
   $(window).trigger("resize");
});

Examples: http://jsfiddle.net/cde7fwrb/

EDIT: I see you were talking about the browser maximise button. Oh well....

Upvotes: 0

Gregg Duncan
Gregg Duncan

Reputation: 2725

I've checked Chrome, Firefox and IE11. All 3 browsers trigger the alert when the maximize button is clicked.

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

What, specifically, are you trying to do that this doesn't work for you?

Upvotes: 0

Tyler
Tyler

Reputation: 874

You can detect both events and just execute code when it's a width change:

var width = $(window).width();
$(window).resize(function(){
   if($(this).width() != width){
      width = $(this).width();
       console.log(width);
   }
});

Upvotes: 3

Related Questions