- This topic has 18 replies, 3 voices, and was last updated 8 years, 3 months ago by
Tom.
-
AuthorPosts
-
May 17, 2017 at 3:50 pm #319434
TWMA Support
Hi
Ok so I have the smooth scroll working on a site I am working on, but…
The smooth scroll only works once and then it jumps to each section. I think its to do with the /#[anchor here] after the domain address. How do I fix this?
Also I want the scrolling to stop when it gets to the top of the section. Currently it over shoots the section often covering the title (especially when using a sticky menu as this sits over the title too). How can I fix this too?
Also I am using the slide out menu on mobile so would like to disable the smooth scroll on mobile as it doesnt work quite as well in that instance.
Thanks
May 17, 2017 at 9:13 pm #319542Leo
StaffCustomer SupportHi there,
How are you implementing smooth scroll? Can you provide a link to your site?
May 17, 2017 at 11:17 pm #319602TWMA Support
May 17, 2017 at 11:59 pm #319639TWMA Support
Hi
Ok so as an update…I have got it to scroll to a section point (changed the
<a>
to<a>
), so that works fine. Only issue now is that the smooth scroll only works once and then jumps to next anchor point after the first scroll.Thanks
May 18, 2017 at 12:00 am #319640Tom
Lead DeveloperLead DeveloperWhat code are you using exactly?
May 18, 2017 at 12:00 am #319642TWMA Support
This one…
<script>
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;
}
}
});
});
</script>May 18, 2017 at 12:02 am #319645Tom
Lead DeveloperLead DeveloperI personally use this:
jQuery(function($) { $('a.smooth[href*="#"]:not([href="#"])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html,body').animate({ scrollTop: target.offset().top }, 500); return false; } } }); });
You just have to add the class smooth to your links:
<a href="whatever" class="smooth">Link</a>
May 18, 2017 at 12:08 am #319649TWMA Support
Didnt work at all just jumps to the #
May 18, 2017 at 12:13 am #319652Tom
Lead DeveloperLead DeveloperHmm, using it here on this site: https://generatepress.com/install-generatepress/
Where’d you add the code?
May 18, 2017 at 12:14 am #319654TWMA Support
wp footer hook section
May 18, 2017 at 12:16 am #319655Tom
Lead DeveloperLead DeveloperIn that case you need to put the JS between script tags:
<script> code in here </script>
May 18, 2017 at 12:17 am #319656TWMA Support
Yip done that too
May 18, 2017 at 12:21 am #319661Tom
Lead DeveloperLead DeveloperTry this:
<script> jQuery(function($) { $(document).on('click', 'a[href*="#"]:not([href="#"])', function(e) { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { e.preventDefault(); $('html,body').animate({ scrollTop: target.offset().top }, 500); } } }); }); </script>
May 18, 2017 at 12:26 am #319667TWMA Support
Scrolls fine for the first menu item click then jumps to next item and the next and so on. Only works once…
May 18, 2017 at 12:34 am #319674Tom
Lead DeveloperLead DeveloperTry the adjusted code again: https://generatepress.com/forums/topic/smooth-scroll-on-single-page-site/#post-319661
-
AuthorPosts
- You must be logged in to reply to this topic.