3

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.

4
  • Downvoters should explain the reason? Its not hypothetical to expect before event, e.g. OSX notifications do has such thing as WillResize, DidResize etc. Commented Mar 24, 2017 at 15:38
  • Possible duplicate of $(window).resize(): Before Commented 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. Commented Mar 24, 2017 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. Commented Mar 25, 2017 at 10:32

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.