How do I get the toggle menu to close when clicking and take me to the beginning of the section correctly?

Asked

Viewed 210 times

2

When I add the code below, the menu even closes when I click, but that doesn’t get me to the starting point of Section. This takes me a few pixels down, unlike what happens in my original code without adding the script.

script in question:

$('.navbar-nav>li>a').on('click', function(){
$('.navbar-collapse').collapse('hide');
});

Could someone show me where I’m going wrong? How do I close the toggle menu on mobile by clicking and descending correctly to the beginning of the section and not a few pixels down. The correct behavior happens without this code that closes the menu when clicking, but I would like to have a menu that closes automatically when clicking. Someone gives a light?

MY ORIGINAL CODE WORKING: https://www.codeply.com/go/gD2F6iVFm0

CODE WITH PROBLEM: https://www.codeply.com/go/eIKdPVCBih

Gif with the problem below - at the end of the click, I show him where he takes me and then I go up to the place I’d like him to take me.

<blockquote class="imgur-embed-pub" lang="en" data-id="a/S2FZwJL" data-context="false" ><a href="//imgur.com/a/S2FZwJL"></a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>

  • Buddy, just with that piece of code, you can’t help yourself. Edit your question, and include as little code as possible to at least simulate the problem you’re having there. Put HTML/CSS and JS

  • I just included the Codeply links, Hugo. I pressed something that ended up deleting them before I posted, thanks for the alert.

1 answer

0

Close Ollapse in the same event that makes the animation, before the animation:

$('a[href*="#"]:not([href="#"])').click(function() {
  $('.navbar-collapse').collapse('hide'); // AQUI!!!
  var target = $(this.hash)
  $('html, body').animate({
    scrollTop: target.offset().top-60
  }, 900)
})

And remove the excerpt from the script you made:

$('.navbar-nav>li>a').on('click', function(){
   $('.navbar-collapse').collapse('hide');
});

inserir a descrição da imagem aqui

  • Thanks for the quick response, Sam. I just removed the last excerpt from the JS and added the first code with the closing of Collapse in the same event of the animation but it keeps going down to the text and not the title of the secction as desired. I put the modified code here: https://www.codeply.com/go/pql3BmnjS7

  • Here’s a beautiful working.

  • On mobile? I will try to record a gif of how you are here.

  • Here’s a link to the problem in Gif, Sam: https://imgur.com/Sa3boQF - at the end of the click, I show you where he takes me and then I go up to the place you’d like him to take me.

  • I just saw the gif you added, Sam. AMAZING, this is the behavior that I dream for my site, I’m just not able to reproduce it here by clicking on the codeply above it gives you exactly this result that you recorded or you saved in a local file? I ask because the gif I recorded still presents the problem. :/

  • I recorded the link you sent.

  • I tried it here on Chrome and Edge and I can’t play it. Is it something here on my pc? I’ll try it on mobile phone and a notebook. I’m using Windows 10.

  • Weird. I tested it here in Chrome in Win 10 and it worked normal.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.