Home jquery JQuery and browser window size

JQuery and browser window size

Author

Date

Category

Good evening, how do I get the script to work when the browser window is already initially changed?

$ (document) .ready (function () {
   $ (window) .resize (function () {
        if ($ (window) .width () & lt; = '995') {
      $ ('# shelf'). show (10)
      return this;}
     else {
       $ ('# shelf'). hide (10)
      }
  });
});

Answer 1, authority 100%

@Kp Houseoff , you only have changes by event .resize () . If you need to calculate when loading, then set exactly this event, namely .load () :

function windowSize () {
  if ($ (window) .width () & lt; = '995') {
    $ ('# shelf'). show (10);
  } else {
    $ ('# shelf'). hide (10);
  }
}
$ (window) .load (windowSize); // while loading
$ (window) .resize (windowSize); // when resizing
// or two-in-one, instead of the last two lines:
$ (window) .on ('load resize', windowSize);

Answer 2

Check immediately. Resize () has not yet occurred in your code – condition is not checked.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions