samedi 25 mai 2019

What is a good way to make an event occur when the page has loaded OR on each resize?

I need a query to check the viewport width each time the document is ready or whenever the viewport gets resized. The reason is that I want the main navigation to look and behave differently for different viewport widths.

//EventListener to get viewport width on load
window.addEventListener("load", function () {
    window.loaded = true;
});

//get Window Size on Resize
var width = $(window).width();
$(window).on('resize', function reportWindowSize() {
  if ($(this).width() != width) {
    width = $(this).width();
    console.log(width);
  }
});

Then I think I need a query like this:

If (page loaded OR viewport width changes) {
   if (viewport width > x) {
     do things

} else if (viewport width < x) {
     do other things
  }
};

What would be a good way to form If (page loaded OR viewport width changes) into a JavaScript or jQuery expression?

Aucun commentaire:

Enregistrer un commentaire