Stop scroll menu in a certain resolution

2

I have a menu that goes down along with the scroll. I used Jquery to add css attributes for this to occur. I do not want this to happen when the site has less than 768px of width. So let it be stopped

Media queries did not help me resolve because these attributes were added regardless of resolution. Then I tried the following:

$(window).scroll(function() {
 if ($(window).width() < 760)
     {
        $('#menu').css({'position' : 'absolute'});
     }
 });

Or this

$(function() {
if ( $(window).width() < 760) {     
  $('#menu').css({'position' : 'relative'});
}
});

But it did not roll in both cases. I'm new to Jquery. How to complement this code to make it work? Am I going the right way?

This is the code that makes my scroll menu

$(function() {
$(window).scroll(function()
{
var topo = $('#topo').height(); // altura do topo
var rodape = $('#rodape').height(); // altura do rodape
var scrollTop = $(window).scrollTop(); // qto foi rolado a barra
var tamPagina = $(document).height(); // altura da p?gina

if(scrollTop > topo){
  $('#menu').css({'position' : 'fixed', 'top' : '0'});
}else{
  $('#menu').css({'position' : 'relative', 'margin-top' : 0});
}
});
});
    
asked by anonymous 09.02.2015 / 14:17

3 answers

1

You should use $(window).resize() to jQuery detect that the screen has been resized

Example:

$(function() {
    $(window).resize(function(){
      if ( $(this).width() < 760) {     
        $('#menu').css({'position' : 'relative'});
      }
    }
});

Update

After your upgrade, I imagine you're wanting something like this:

$(function() {
    $(window).scroll(function()
    {
        var topo = $('#topo').height(); // altura do topo
        var rodape = $('#rodape').height(); // altura do rodape
        var scrollTop = $(window).scrollTop(); // qto foi rolado a barra
        var tamPagina = $(document).height(); // altura da p?gina

        if($(window).width() > 760 && scrollTop > topo){
          $('#menu').css({'position' : 'fixed', 'top' : '0'});
        } else{
          $('#menu').css({'position' : 'relative', 'margin-top' : 0});
        }
    });
});

Because it must meet two conditions: The screen must be greater than 760px and must meet the desired condition when $(window).scrollTop() is greater than the top size

    
09.02.2015 / 14:26
1

It is better to manipulate classes instead of elements directly.
You can start this menu with a class responsible for keeping it fixed at the top and then use Javascript / sup> only to check whether or not this class should be removed depending on the width of $(window) .

Already to handle resizing, use resize . Just check the width of the window and if the element has the class that holds the menu fixed. Example:

// js

$(function(){   
    if($(window).width() < 768){
        $('.menu').removeClass('is-fixed');
    }
});

$(window).resize(function(){
   var menu = $('.menu');

   /*
    * Se for menor que a largura que você definiu e...
    * Se o elemento possuir a classe 'is-fixed', então...
    * ela é removida.
    * 
    * Do contrário a classe é inserida.
    */
   if($(window).width() < 768 && menu.hasClass('is-fixed')){
       menu.removeClass('is-fixed');
   } else {
       menu.addClass('is-fixed');
   }
});

DEMO

I needed to put in JSFiddle because StackOverflow does not work with resize .

    
09.02.2015 / 16:02
1

Try this, because if you do not want it to work if it is less than 760, use > in condition.

 $(window).scroll(function() {
     if ($(window).width() > 760)
         {
            $('#menu').css({'position' : 'absolute'});
         }
     });
    
09.02.2015 / 15:27