Created
May 7, 2019 20:21
-
-
Save deeman/3c456ff0f4a510959b886cd277a70570 to your computer and use it in GitHub Desktop.
Gensis: Smooth scrool to top
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Enqueue To Top script | |
| add_action( 'wp_enqueue_scripts', 'to_top_script' ); | |
| function to_top_script() { | |
| wp_enqueue_script( 'to-top', get_stylesheet_directory_uri() . '/js/to-top.js', array( 'jquery' ), '1.0', true ); | |
| } | |
| // Add To Top button | |
| add_action( 'genesis_before', 'genesis_to_top'); | |
| function genesis_to_top() { | |
| echo '<a href="#0" class="to-top" title="Back To Top">↑</a>'; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery(document).ready(function($){ | |
| // Scroll (in pixels) after which the "To Top" link is shown | |
| var offset = 300, | |
| //Scroll (in pixels) after which the "back to top" link opacity is reduced | |
| offset_opacity = 1200, | |
| //Duration of the top scrolling animation (in ms) | |
| scroll_top_duration = 700, | |
| //Get the "To Top" link | |
| $back_to_top = $('.to-top'); | |
| //Visible or not "To Top" link | |
| $(window).scroll(function(){ | |
| ( $(this).scrollTop() > offset ) ? $back_to_top.addClass('top-is-visible') : $back_to_top.removeClass('top-is-visible top-fade-out'); | |
| if( $(this).scrollTop() > offset_opacity ) { | |
| $back_to_top.addClass('top-fade-out'); | |
| } | |
| }); | |
| //Smoothy scroll to top | |
| $back_to_top.on('click', function(event){ | |
| event.preventDefault(); | |
| $('body,html').animate({ | |
| scrollTop: 0 , | |
| }, scroll_top_duration | |
| ); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment