2

i know there is already topics about this problem but all solutions didn't worked for me.

i want my menu to be fixed on top of my page when user is scrolling.

i found a script and adapted it for my needs :

var win         = $(document.body);
var fxel        = $('#stickynav');
var eloffset    = $('#stickynav').offset().top;
console.log(win.scrollTop());

win.scroll(function() {

    if (eloffset < win.scrollTop()) {
        console.log('fixed');
        fxel.addClass("fixed");
    } else {
        console.log(eloffset + ' != ' + win.scrollTop());
        fxel.removeClass("fixed");
    }
});

it's working on firefox and IE8 but not in chrome, win.scrollTop() is always returning 0.

i tried everything win = $(document), $(window) and $('body, html') and it's always returning 0 or nothing.

Can somebody help me?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
loubradou
  • 21
  • 2
  • This should work, could you provide a fiddle ? – Hacketo Jan 16 '15 at 09:46
  • i can't because it should work, here's is th url of the website if you want to take a look : http://www.littlebastardparis.com/ – loubradou Jan 16 '15 at 09:57
  • Erm, is there a reason you're not using "position: fixed;"...? –  Jan 16 '15 at 13:13
  • when i scroll i add the class "fixed" who contains position:fixed to my menu, like this : http://stackoverflow.com/questions/19158559/how-to-fix-a-header-on-scroll – loubradou Jan 16 '15 at 14:15

1 Answers1

1

I'm using this on one of my projects, it may work for you.

//window events
$(window).on('scroll', function(){
    that = $(this); 
    if(that.scrollTop()>break_point){
        //YOUR CODE
    }
});

UPDATE

I've worked on a work around for your problem. First of all try updating your jQuery, if the problem persists try using this: I already tested this on your page, it seems to be working fine.

    $(window).on('scroll', function(){
        that = $(this);
        //because you are wrapping all contents
        var offsetTop = Math.abs($('.wrapper-1').offset().top);

        if(offsetTop>break_point){
            //CODE HERE
        }
    });

Honestly hope it works. Cheers

Gonsalo Sousa
  • 495
  • 4
  • 9