2

I'm using this script to hide the div with the id #main-slider.

But every time I load my page I can see the div for like 1 or 2 seconds and then it disappears, is there any way to make it take effect sooner?

Thank you.

window.onload = function() {
    if($(window).width() > 991) 
    {
        $('#main-slider').removeClass('hidden');
    }
    else
    {
        $('#main-slider').addClass('hidden');
    }
}
Yangshun Tay
  • 33,183
  • 26
  • 102
  • 123
Uchiwayas
  • 33
  • 1
  • 8

4 Answers4

1

Maybe you should use css @media for that. Try that:

@media (min-width: 991px) {
  #my-element {
      display: none;
  }
}
Lukas Gund
  • 469
  • 1
  • 6
  • 23
0

Use document.onload instead, it's fired earlier. Reference: https://stackoverflow.com/a/588048/4108884

Samuil Petrov
  • 564
  • 1
  • 12
  • 24
  • Yeap, [Here is an example](http://web.archive.org/web/20150405114023/http://ie.microsoft.com/testdrive/HTML5/DOMContentLoaded/Default.html) – nick zoum Jun 02 '17 at 09:14
0

window.onload waits for every element to be loaded correctly. You can either use document.onload or you can use the event DOMContentLoaded:

document.addEventListener("DOMContentLoaded", function(event) {
    // do something
});
StuntHacks
  • 427
  • 4
  • 15
0

Add the class 'hidden' to the 'main-slider' html element in the html page. So the class get added at first, so that the main-slider element is hidden and then according to the condition it gets visible or not. Now with your check, it should work

Haroon
  • 111
  • 6