2

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.

Nitesh
  • 2,681
  • 4
  • 27
  • 45
  • 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 Mar 24 '17 at 15:38
  • Possible duplicate of [$(window).resize(): Before](http://stackoverflow.com/questions/11791537/window-resize-before) – Nelson Teixeira Mar 24 '17 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 Mar 24 '17 at 15:49
  • 2
    This 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 Mar 25 '17 at 10:32

2 Answers2

2

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.

Chalibou
  • 438
  • 5
  • 12
1

There are no events that fire before resize. Only during resize. Most solutions to do something on rezise use setTimeout to check when a certain amount of time passed since the last resize event. So I guess you're out of luck.

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
  • Ok. Thanks. I did look for but didn't found such an event, so I was afraid that IS the case. – Nitesh Mar 25 '17 at 10:35