Skip to content

Instantly share code, notes, and snippets.

@hmowais
Last active June 15, 2019 10:09
Show Gist options
  • Save hmowais/e67251edc4ffae65ff4c919d46e9b9c1 to your computer and use it in GitHub Desktop.
Save hmowais/e67251edc4ffae65ff4c919d46e9b9c1 to your computer and use it in GitHub Desktop.
Add toTop Button with WordPress Hook
<?php
// Add scripts to wp_footer()
function totop_script() { ?>
<script>
jQuery(function ($) {
var $window = $(window);
var $buttonTop = $('.button-top');
var scrollTimer;
$buttonTop.on('click', function () {
$('html, body').animate({
scrollTop: 0,
}, 400);
});
$window.on('scroll', function () {
clearTimeout(scrollTimer);
scrollTimer = setTimeout(function() {
if ($window.scrollTop() > 100) {
$buttonTop.addClass('button-top-visible');
} else {
$buttonTop.removeClass('button-top-visible');
}
}, 250);
});
})
</script>
<button class="button-top">↑</button>
<?php }
add_action( 'wp_footer', 'totop_script' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment