0

I have written code for the mobile site so that when you click the menu item, the page body becomes fixed and cannot scroll (so that we are able to scroll through the drop down menu, without the background scrolling). However, whenever I click the menu button, the page jumps to the top of the website. I am trying to get it so that when you click the menu button, the page stays where it is, and does not jump to the top. Any help is greatly appreciated.

OUR WEBSITE

jQuery( function($) { 
$( ".x-btn-navbar" ).on('click', function(e) {
  $('body').toggleClass("noScroll");
e.preventDefault();
});
});
.noScroll {
  position: fixed;
}

EDIT I have tried putting e.preventDefault(); into my jQuery but it still doesn't work. The "duplicate" page that I was linked to does not work. This has been marked as a duplicate but it is not. I am trying to get help so that I can get my website to work. Please unmark as duplicate.

  • @Rory McCrossan You marked this as a duplicate of almost a decade year old question?? Their answer doesn't work for us. Can you unmark this question as a duplicate. –  Jul 26 '18 at 10:01
  • It's an old answer because this is an exceptionally common problem which is easily solved by adding a `preventDefault()` call. – Rory McCrossan Jul 26 '18 at 10:08
  • I have literally copied and pasted the code you updated your question with in to the console on your website and it works; the page no longer scrolls up when you click the burger on the mobile view. I would suggest checking the console for errors as there's likely a mistake somewhere in your code. – Rory McCrossan Jul 26 '18 at 10:11
  • @Rory McCrossan The background page still scrolls though, and we do not want that. No need to be rude about it. –  Jul 26 '18 at 10:15
  • There's nothing rude there, so I'm not sure what you're referring to. To stop the page scrolling when an overlay is shown, do this: https://stackoverflow.com/questions/47918560/prevent-background-scrolling-when-overlay-appears – Rory McCrossan Jul 26 '18 at 10:18
  • @RoryMcCrossan I appreciate your help, I have tried that but it still not working. –  Jul 26 '18 at 10:21
  • I would suggest starting a new question in that case, as the problem to be solved here is to stop the page jump, which has been covered by the duplicate. – Rory McCrossan Jul 26 '18 at 10:22
  • @RoryMcCrossan It is part of my original question & I can't post for another hour... Having the code from the "duplicate" has the same effect as having none of the code at all, they have the same outcome. So no it hasn't solved my problem at all. –  Jul 26 '18 at 10:26
  • @RoryMcCrossanAre you there??? –  Jul 26 '18 at 10:41

0 Answers0