Skip to content

Instantly share code, notes, and snippets.

@xerardoo
Created June 5, 2020 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xerardoo/2d9758a3e770e2e36499672bcd4f9e99 to your computer and use it in GitHub Desktop.
Save xerardoo/2d9758a3e770e2e36499672bcd4f9e99 to your computer and use it in GitHub Desktop.
scoll top vue component
<template>
<button @click="goUp" class="btn-up" v-bind:class="{ 'd-block': show }">
<font-awesome-icon :icon="['fas', 'arrow-circle-up']"/>
</button>
</template>
<script>
// Based on: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp
export default {
name: "ScrollTop",
props: ['top'],
data: function () {
return {
show: false,
topPixels: this.top
}
},
methods: {
scrolling: function () {
this.show = (document.body.scrollTop > this.topPixels ||
document.documentElement.scrollTop > this.topPixels)
},
goUp: function () {
document.body.scrollTop = 0; // For Safari
document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera
}
},
created() {
window.addEventListener('scroll', this.scrolling);
},
destroyed() {
window.removeEventListener('scroll', this.scrolling);
}
}
</script>
<style scoped>
.btn-up {
display: none;
position: fixed; /* Fixed/sticky position */
bottom: 20px; /* Place the button at the bottom of the page */
right: 30px; /* Place the button 30px from the right */
z-index: 99; /* Make sure it does not overlap */
border: none; /* Remove borders */
outline: none; /* Remove outline */
background-color: red; /* Set a background color */
color: white; /* Text color */
cursor: pointer; /* Add a mouse pointer on hover */
padding: 10px; /* Some padding */
border-radius: 10px; /* Rounded corners */
}
.btn-up:hover {
background-color: #555; /* Add a dark-grey background on hover */
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment