There are many posts on this site that I read, but nothing works for me, and I'm not sure why.
I'm trying to create a menu that “sticks” to the top of the page when you scroll it, and vice versa (stops sticking when scrolling up)
Javascript
$(document).ready(function(){ var top = $('#FloatingMenu').offset().top - parseFloat($('#FloatingMenu').css('marginTop').replace(/auto/,100)) document.addEventListener("scroll",Scroll,false); document.addEventListener("gesturechange",Scroll,false); function Scroll() { var y = $(this).scrollTop(); if (y >= top) { $('#FloatingMenu').addClass('fixed'); } else { $('#FloatingMenu').removeClass('fixed'); }
CSS
#FloatingMenu.fixed { position: fixed; top: 0; left: 0; z-index: 1; width: 100%; background-color: black; color: red; } #FloatingMenu { background-color: red; color: black; width: 100%; text-align: center; }
I tried to redraw, I tried to stop the “inertia” scrolling (which I cannot stop in Chrome on iOS). Anyway, everything I tried has the same results. Works great on PC or Android, but on the iPhone the menu will not be redrawn and “stuck” at the top until scrolling stops and the finger is removed from the screen.
Is there a fix for this? Everything that I read suggests that there is only one solution that has not changed anything for me.
The strange thing is: if your scroll up (the menu is already stuck at the top) and you scroll it, it automatically peels off (even when scrolling) and works fine.
The only time a problem occurs is when it is “repainted” by a “fixed” menu.
I hope there is a solution. Everything suggests that after iOS 8 it was fixed (and I'm testing 10+), but it will not show the menu while scrolling until you stop and release it. Tested on iPhone 6 and iPad Air 2. Safari and Chrome, the same results in all directions.