In a comment you've said, that refreshing after resize is OK. To achieve this, you need a debouncer. Debouncer is a function, which checks, if an event hasn't fired after a given delay, and only then it fires the event handler you've passed for the event. Like so:
var oldSide = window.innerWidth - 1200; // Stores positive value, if current size is greater than 1200, else zero or negative
// This is the debouncer, it delays the execution of the given handler untill user stops resizing
function debounce(fn, delay) {
var delayId, delay = delay || 200;
return function () {
var scope = this, args = arguments;
clearTimeout(delayId);
delayId = setTimeout(function () {
fn.apply(scope, Array.prototype.slice.call(args));
}, delay);
}
}
// Here we add the resize listener, notice, that we call the debouncer, and pass our actual handler to that
$(window).resize(debounce(function (e) {
var newSide = window.innerWidth - 1200;
// newSide * oldSide is negative, if 1200 is acrossed in either directions during the last resizing
if (newSide * oldSide <= 0) {
// Window size acrossed 1200 during the last resize, refresh the page
}
// Window size didn't across 1200 during the last resize, set a new value to oldSide, and keep listening
oldSide = newSide;
}));
A working demo at jsFiddle (logs only to console, no refresh).
Notice, that if you're not going to actually refresh when 1200 is acrossed, you must to return from the if
block.
You can also detect the "border across" in the debouncer (just after clearTimeout
), that way it will be more real time, but might have a gap of few pixels.
The debouncer I've used here originates to BGerrissen's great answer at SO.