Questions with anchor link

Asked

Viewed 169 times

2

I created a script as follows:

<script>
$(function() {
  $('#mostrar_planos').bind('click',function(event){
    var $anchor = $(this);
    $('html, body').stop().animate({scrollTop: $($anchor.attr('href')).offset().top}, 500,'swing');
  });
});
</script>

<a href="#mostrar_planos" id="mostrar_planos">Confira os planos</a>

<section id="mostrar_planos"> conteudo </section>

It works normally, but does not leave the ID that was called, in case the section, at the top of the screen.

It sits in the middle of the screen. Is there any way to adjust it?

I need when you click on the link, it swipe the page and leave this section at the top of the page, not in the middle.

  • Guy doesn’t use bind has been discontinued, use on in place and if you need off instead of unbind. Read more on the jQuery API: http://api.jquery.com/? s=bind

1 answer

3


Dude your problem is that you are using the same link ID you’re using in the content you want to go to.

Note that your content does not have a ID equal, repeated, with the same name as ID of the link: id="mostrar_planos" Just take the ID from the link that solves.

<a href="#mostrar_planos" id="mostrar_planos">Confira os planos</a>

<section id="mostrar_planos"> conteudo </section>

OBS: If there is no content left below the element you anchor the screen will stop rising. Look at the code below, I placed an offset for the contents for the top 50px, and below it a div with 500px height. If you test in a small window it looks right. But if the window is more than 500px high the element does not rise anymore. Soon if in your case there is nothing else below the anchored element it goes to before the top.

See working.

$('a.teste').on('click', function(e) {
    e.preventDefault();
    var id = $(this).attr('href'),
    targetOffset = $(id).offset().top;


    $('html, body').animate({ 
    scrollTop: targetOffset - 50 //altura que para antes do topo da tela
    }, 500,'swing');
});
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

<a class="teste" title="segmentos" href="#mostrar_planos">Confira os planos</a>
        
<DIV style="height: 200vh;"></DIV>

<section id="mostrar_planos"> conteudo </section>

<DIV style="height: 500px; background:tomato;"> div com 500px de altura</DIV>

        

  • Really, but I still couldn’t adjust the position that should stop the Section when it slides. There’s a way to adjust it?

  • @Wendler guy edited my answer. Read the OBS I made and execute the code I updated to better understand in a small window and then in the whole screen. Qq thing tells me

  • Can I put this space to appear only when I click on the chor? If I put it right on the page it will get weird.

  • Guy should be fine, I particularly won’t know how to answer you because I don’t know much about JS. If that is your intention I recommend you to mark this issue as resolved if she answered this problem, and open another asking about this new question of yours. Someone who understands more of JS will know how to answer you. But I think the logic is to take the height of the anchored element, subtract from the height of the screen and put that value as margin-bottom something like this...

Browser other questions tagged

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