0

When I scroll down the bigger image disappears and the smaller image appears. However when I use this on a smaller screen it's crab so I want it to only work for screens that are above 992px.

I want the function to only be active when people have a resolution of 992px or above. I have no clue how to that.

$(window).resize(function() {
  if ($(this).width() > 992) {
    $(window).scroll(function() {
      if ($(window).scrollTop() < 20) {
        $('.navbar-brand').stop(true, true).fadeIn("slow");
        $('.image-scroll').css('display', 'None');
      } else {
        $('.navbar-brand').stop(true, true).fadeOut("slow");
        $('.image-scroll').css('display', 'block');
      }
    });
  }
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Benjamin45
  • 52
  • 7

2 Answers2

1

You can use jquery

if ($(window).width() > 992) {
   alert('More than 992');
}
else {
   alert('Less than 960');
}

For your situation I guess your code should look something like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 <script> 
if ($(window).width() > 992) {
    alert('More than 992');
 $(window).scroll(function(){
   if($(window).scrollTop()<20){
         $('.navbar-brand').stop(true,true).fadeIn("slow");
        $('.image-scroll').css('display', 'None');
   } else {
         $('.navbar-brand').stop(true,true).fadeOut("slow");  
        $('.image-scroll').css('display', 'block');
   }
});
}else{
    alert('Less than 992');
}
  </script>
J.vee
  • 623
  • 1
  • 7
  • 26
1

While you want to execute the function on window resize and scroll .. it'll be better to create a function .. you need to put the if statement inside an event not the event inside the if

$(document).ready(function(){
  $(window).on('scroll resize' , Scroll_Action)
});

function Scroll_Action(){
    if($(window).scrollTop() < 20 && $(window).width() > 992){
        $('.navbar-brand').stop(true,true).fadeIn("slow");
        $('.image-scroll').css('display', 'None');
    } else {
        $('.navbar-brand').stop(true,true).fadeOut("slow");  
        $('.image-scroll').css('display', 'block');
    }
}
Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28