onresize can be used for knowing window has been resized. Is there any similar notification/event before window resizes. I want to set will-change on some elements before window resize starts and subsequently remove that after window has resized.
-
Downvoters should explain the reason? Its not hypothetical to expect before event, e.g. OSX notifications do has such thing as WillResize, DidResize etc.Nitesh– Nitesh2017-03-24 15:38:41 +00:00Commented Mar 24, 2017 at 15:38
-
Possible duplicate of $(window).resize(): BeforeNelson Teixeira– Nelson Teixeira2017-03-24 15:48:24 +00:00Commented Mar 24, 2017 at 15:48
-
It wasn't me who downvoted, but maybe the downvote was because of the lack of research. There are plenty of questions on this matter.Nelson Teixeira– Nelson Teixeira2017-03-24 15:49:47 +00:00Commented Mar 24, 2017 at 15:49
-
2This happens when you ask something in which you have relatively less experience. :( I did search for this and even had looked into the $(window).resize(): Before. whose answer provided way to solve user's problem but didn't clarifies if the before event is triggered or not, which way you done properly in your answer here. Thanks.Nitesh– Nitesh2017-03-25 10:32:20 +00:00Commented Mar 25, 2017 at 10:32
Add a comment
|
2 Answers
It seems there are no direct solutions but you can achieve both start|end events with some workaround using window.addEventListener("resize",yourFunction); and some global variables (it is a workaround, it is not perfect).
//Accesible variables
var atStart = true;
var timer;
function yourFunction(){
return ()=>{
if(atStart){
console.log("START");
//Some code to be executed once at start
atStart = false;
}
console.log("WHILE");
//Some code to be executed while resizing
if (timer) clearTimeout(timer);
timer= setTimeout(atEnd, 500);
}
}
function atEnd(){
//Some code to be executed at the end of resizing
//..well some 500ms after the end of resizing
console.log("END")
atStart = true;
}
window.addEventListener("resize",yourFunction());
Hope it helps someone.