Problems to anchor with jquery

Asked

Viewed 82 times

0

Hello, I’m making an application in angular 7, and I’m wanting to use anchors in the menus, as I found no way to do via typescript made through jquery but the anchorage does not work as expected, when I’m at the top of the page works smoothly, but when I click on one anchor and after another it does not go towards the id of the demarcated ancoma. I’d like your opinion on how I can solve this problem:

HTML:

<div class="collapse navbar-collapse" id="navbarSupportedContent-333">
  <ul class="navbar-nav ml-auto nav-flex-icons">
    <li class="nav-item">
      <a onclick="rolar_para('#section1')" class="scroll nav-link waves-effect waves-light link">O Tabaco</a>
    </li>
    <li class="nav-item">
      <a onclick="rolar_para('#section2')" class="scroll nav-link waves-effect waves-light link">
       Missão e Valores
      </a>
    </li>
    <li class="nav-item">
      <a onclick="rolar_para('#section3')" class="scroll nav-link waves-effect waves-light link">
        Quem somos
      </a>
    </li>
  </ul>
</div>

Jquery:

function rolar_para(elemento) {
  $('html, body').animate({
    scrollTop: $(elemento).offset().top
  }, 2000);
}
  • 1

    Hello Victor! How can you click on another anchor if the screen goes down and hides the navbar by clicking on one?

  • the navbar I left fixed, so if I give scroll the navbar will be there

  • "but when I click on one anchor and after another it does not go towards the id of the demarcated ancoma"... what happens? The page stands still? Go somewhere else?

  • If you are interested here you have an option only with CSS https://answall.com/questions/380399/conflito-entre-%C3%A2ncora-e-rolagem-suave/380403#380403 (the support is not yet full... but sometimes breaks its branch)

  • 1

    Complete html with fixed navbar and sections. Repeat with me: minimum, complete and verifiable example!

  • Sam, when I clicked on the anchored Manu that I was already clicked on 1 time, it wasn’t going to the anchorage that I had clicked on, it was going up the scroll

  • hugocsl, your example worked completely here, I will test compatibility for other browsers

Show 2 more comments
No answers

Browser other questions tagged

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