- This topic has 5 replies, 3 voices, and was last updated 4 years, 7 months ago by
Tom.
-
AuthorPosts
-
October 29, 2017 at 3:12 am #412263
Max
Tom
I got your smooth scroll script from another thread (displayed below).
Works fine in GP footer hook. However I want to run the script in my functionality plugin rather than in the footer hook.
Can you please let me know how to do this.
Kind Regards
Max
<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>
GeneratePress 1.4GP Premium 1.4.3October 29, 2017 at 9:19 am #412451Leo
StaffCustomer SupportHi there,
What’s your functionality plugin? Like your own plugin?
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/October 29, 2017 at 12:36 pm #412549Max
Yes, I have my own plugin that I use to run various snippets. That is why I want to add Tom’s script to my plugin rather than having in the footer hook.
Kind Regards
Max
October 29, 2017 at 7:51 pm #412709Tom
Lead DeveloperLead DeveloperYou could do this:
add_action( 'wp_footer', 'tu_add_script' ); function tu_add_script() { ?> Your script in here <?php }
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-developmentOctober 29, 2017 at 10:27 pm #412741Max
Thanks Tom
For anyone who is interested in this thread, here is the full snippet to run smooth scroll from your own functionality plugin:
Note: this only works to smooth scroll to an anchor on the same page. It does not work if your anchor is on another page.
//////////////////// GP SMOOTH SCROLL //////////////////////////////// add_action( 'wp_footer', 'tu_add_script' ); function tu_add_script() { ?> <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> <?php } //////////////////// END GP SMOOTH SCROLL ///////////////////////////////
Kind Regards
Max
October 30, 2017 at 8:58 am #413034Tom
Lead DeveloperLead DeveloperThanks for sharing, Max! 🙂
Documentation: http://docs.generatepress.com/
Adding CSS: http://docs.generatepress.com/article/adding-css/
Ongoing Development: https://generatepress.com/ongoing-development -
AuthorPosts
- You must be logged in to reply to this topic.