I have a button on my page that scrolls back to top of the page. It works but I want the sliding to be smooth and not instantly. What's a good way to do it?
Please note there are similar questions on this site but neither of them use Vuetify.
This is my button:
<v-btn
class="md-5 mr-3 elevation-21"
dark
fab
button
right
color="indigo darken-3"
fixed
@click="top"
>
This is my function:
methods:{
top(){
window.scrollTo(0,0);
}
}
Using the behavior option triggers an animated scroll in browsers that support it. Show activity on this post. A Vuetify solution can be achieved by programmatically triggering scrolling in your application by using the goTo method found on the $vuetify object.
Vuetify — A Material Design Framework for Vue.js Vuetify is a Material Design component framework for Vue.js. It aims to provide all the tools necessary to create beautiful content rich applications.
We have the v-touch directive with the value being an object with the left , right , up and down properties that have functions that run when the swipe are in those directions. We can get the breakpoints from the app with the this.$vuetify.breakpoint property.
We can watch any element’s scrolling, including the element that v-scroll is applied to. To do that, we add the self modifier to the v-scroll directive.
Using the behavior
option triggers an animated scroll in browsers that support it.
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth'
});
https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo
A Vuetify solution can be achieved by programmatically triggering scrolling in your application by using the goTo
method found on the $vuetify
object.
<v-btn
class="md-5 mr-3 elevation-21"
dark
fab
button
right
color="indigo darken-3"
fixed
@click="$vuetify.goTo('#topMostElement', goToOptions)
>
Options can be bound to control duration and style of easing:
data: () => {
return {
goToOptions: {
duration: 10000,
offset: 0,
easing: 'easeInOutCubic',
},
};
},
Additionally, the button can initially be hidden until the page has begun to scroll by using Vuetify's v-scroll
directive to provide callbacks when the window or target element is scrolled. In this instance we're detecting scroll on the window.
<v-btn
class="md-5 mr-3 elevation-21"
dark
fab
button
right
color="indigo darken-3"
fixed
@click="$vuetify.goTo('#topMostElement', goToOptions)
v-scroll="onScroll"
v-show="showGoToTop"
>
And then capturing the offset from the top of the page and using an arbitrary travel of scroll to unhide the button.
data: () => {
return {
offsetTop:0,
goToOptions: {
duration: 10000,
offset: 0,
easing: 'easeInOutCubic',
},
};
},
computed:{
showGoToTop () {
return this.offsetTop > 200;
},
},
methods: {
onScroll (event) {
this.offsetTop = event.target.scrollingElement.scrollTop;
},
},
try (open snippet on full-page)
html {
scroll-behavior: smooth;
}
function scrollme(selector) {
console.log('xx');
document.querySelector(selector).scrollIntoView();
}
html {
scroll-behavior: smooth;
}
.d { width: 100px; height: 1000px; background: #eee; }
.d:nth-child(odd) {background: #ddd; height: 100px; }
<div class="d start"></div>
<button onclick="scrollme('.end')">scroll</button>
<div class="d"></div>
<div class="d"></div>
<div class="d"></div>
<button onclick="scrollme('.start')">scroll</button>
<div class="d end"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With