简体   繁体   中英

Smooth Anchor Link Scrolling Conflict

I am using the code below to have smooth scrolling on anchor links

jQuery(function() {
  jQuery('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = jQuery(this.hash);
      target = target.length ? target : jQuery('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        jQuery('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

However this conflicts with some hidden divs that I use to display information. Here is one example.

<div style="display:none;">
<div id="contact-email" >
[gravityform id=15 ajax=true title=false description=false tabindex=20]
</div>
</div>

If I use the code above the hidden divs are not displayed at all.

Is it possible to exclude the anchor links that correspond to my hidden divs - i only have a couple..

ok the solution is to use:

$(".scroll").click(function(event){     
        event.preventDefault();
        $('html,body').animate({scrollTop:$(this.hash).offset().top}, 500);
    });

and then use

<a href="#comments" class="scroll">Scroll to comments</a>

for the anchor links that I would like to have smooth scrolling.

I am just wondering how efficient and browser compatible is this code..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM