Skip to content

Instantly share code, notes, and snippets.

@getneerajk
Created March 1, 2024 07:07
Show Gist options
  • Save getneerajk/7f4ac6f2c40cbf09001a85321f58efa0 to your computer and use it in GitHub Desktop.
Save getneerajk/7f4ac6f2c40cbf09001a85321f58efa0 to your computer and use it in GitHub Desktop.
Back to top #back2top #wp #hook
<?php
//add the hook in page.php after entry-content
/* <?php do_action('si_after_entry_content_action'); ?>*/
// Back to Top
add_action( 'si_after_entry_content_action', function () { if ( !is_admin() ) {
?>
<div id="back-to-top" class="back-to-top" >
<!--add image here-->
</div>
<style>
/* Back To Top */
.back-to-top.active-to-top {
pointer-events: auto;
opacity: 1;
}
.back-to-top {
position: fixed;
bottom: 5rem;
right: 1.39rem;
display: flex;
text-decoration: none;
opacity: 0;
pointer-events: none;
transition: all .4s;
z-index: 20;
cursor: pointer;
border: 0.111rem solid var(--white);
border-radius: 50%;
background: var(--blue);
}
.back-to-top:hover {
animation: btt 1s linear infinite alternate;
}
@keyframes btt {
0% {
transform: scale(1);
}
50% {
transform: scale(1.1);
}
100% {
transform: scale(1);
}
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
var back2top = document.getElementById("back-to-top");
window.onscroll = function() {scrollFunction()};
function scrollFunction() {
if (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) {
if(back2top)
back2top.classList.add('active-to-top');
} else {
if(back2top)
back2top.classList.remove('active-to-top');
}
}
// Scroll to top on button click
if(back2top)
back2top.addEventListener('click',function(){
window.scrollTo({
top: 0,
behavior: "smooth"
});
});
</script>
<?php }},100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment