Jquery Anchor extremely accurate!

Asked

Viewed 316 times

3

I’m using the following code:

<?php       
   foreach($sqlPortfolio2 as $dPortfolio2):
    echo '<li><a href="#guia'.$dPortfolio2['id'].'" class="scroll">'.$dPortfolio2['nome'.$lg].'</a></li>';
                endforeach;
                ?>


<script type="text/javascript">
jQuery(document).ready(function($) { 
    $(".scroll").click(function(event){        
        event.preventDefault();
        $('html,body').animate({scrollTop:$(this.hash).offset().top}, 800);
   });
});
</script>

The problem is that I use the fixed menu, IE, when I click on a menu item, the menu comes together, and ends up staying over the title of the element I clicked! It has how to make an extremely precise anchor?

  • post your html so I can see what’s happening

1 answer

1


You should discount the height of the menu when performing the scroll.

<script type="text/javascript">
jQuery(document).ready(function($) { 
    $(".scroll").click(function(event){        
        event.preventDefault();
        $('html,body').animate({scrollTop:$(this.hash).offset().top - $("#MENU").outerHeight(true) - 20}, 800);
   });
});
</script>

We discounted 20 pixels so that the menu is not "glued" to the top element. So it is visually more pleasant.

  • This #MENU has to be set a height in its css?

  • @user3081 No, this #MENU is your menu id (switch to your menu id). This code will pick up the actual height of your menu (height of it) regardless of whether you spelled it out in CSS or not.

  • worked! But the menu is glued to the title, it has how to determine a height, instead of being automatic?

  • @user3081 what happens is that the exact height of the menu has been discounted, so it is pasted. You can discount another 20 pixels to make it visually more pleasant.

  • see my reply issue.

  • @user3081 you can determine a fixed height yes, in place of the code we took the height of the menu can be a fixed value, but do not recommend doing this, because if some day you change the height of the menu will have to remember to change this code too. It is much simpler and better to do as I demonstrated in the last issue of the answer. Take the full menu size and discount another fixed pixel value (in case 20)

  • Thank you very much Erick Gallani! Gave straight! Thanks very much!!!

Show 2 more comments

Browser other questions tagged

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