mardi 31 août 2021

if else javascript syntax generating error mediaQuery not applied [closed]

I want this running to be able to have different js functions loaded for different screen sizes (mainly for very large screens).

// Create a mediaQuery that targets viewports from at least 1900px wide
const mediaQuery = window.matchMedia('(min-width: 1900px)')
// Check if the media query is true
if (mediaQuery.matches) {
  // Then run this
  $(window).on("load", function() {
    AOS.init();
  })
  // else run this:
  else {
    $(window).on("scroll", function() {
      AOS.init();
    });
  };
}

After many hours of trying and testing, I still can not get the syntax right. What is my fault here? The error output is:

Uncaught SyntaxError: expected expression, got keyword 'else'app.js:231:2

Which is referring to the else part.

I know the syntax need to be like this:

 if (condition) {
      //  block of code to be executed if the condition is true
    } else {
      //  block of code to be executed if the condition is false
    }

But I can not get it right. Any advice on this thanks.

Aucun commentaire:

Enregistrer un commentaire