Reputation: 1827
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..
Upvotes: 1
Views: 1461
Reputation: 1827
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..
Upvotes: 4