Page 1 of 1

Free** Sticky fixed menu on scroll

Posted: Mon Nov 20, 2017 5:23 am
by fadichmn
Hello,

I wan't to share a javascript code that makes the menu fixed while scrolling. This is for the default theme.

Put this code anywhere in "catalog/view/theme/default/template/common/menu.twig"

Code: Select all

<script>
$('.navbar').addClass('original').clone().insertAfter('.navbar').addClass('cloned').css('position', 'fixed').css('top', '0').css('margin-top', '0').css('z-index', '500').removeClass('original').hide();

scrollIntervalID = setInterval(stickIt, 10);

function stickIt() {
  var orgElementPos = $('.original').offset();
  orgElementTop = orgElementPos.top;

  if ($(window).scrollTop() >= (orgElementTop)) {
    orgElement = $('.original');
    coordsOrgElement = orgElement.offset();
    leftOrgElement = coordsOrgElement.left;
    widthOrgElement = orgElement.css('width');
    $('.cloned').css('left', leftOrgElement + 'px').css('top', 0).css('width', widthOrgElement).show();
    $('.original').css('visibility', 'hidden');
  } else {
    // not scrolled past the menu; only show the original menu.
    $('.cloned').hide();
    $('.original').css('visibility', 'visible');
  }
}
</script>

Re: Free** Sticky fixed menu on scroll

Posted: Tue Jan 08, 2019 1:35 pm
by Dibakar Dutta
Thank you.it works. but the background color is not there in my case. What should I do in this case?

Re: Free** Sticky fixed menu on scroll

Posted: Sun Mar 03, 2019 10:32 pm
by fadichmn
Dibakar Dutta wrote:
Tue Jan 08, 2019 1:35 pm
Thank you.it works. but the background color is not there in my case. What should I do in this case?
You can use css style for .navbar , .cloned, and .original.
For backgrounds use #menu

Re: Free** Sticky fixed menu on scroll

Posted: Sun Mar 22, 2020 1:28 pm
by stefg
Thank you!

Re: Free** Sticky fixed menu on scroll

Posted: Wed Jul 29, 2020 6:37 pm
by Raslinza
Thank you this worked for me. too.

Much appreciated.