Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrolling back to top slowly using Vuetify

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);
           }
        }
like image 346
seyet Avatar asked Apr 01 '19 20:04

seyet


People also ask

How do I programmatically trigger scrolling in vuetify?

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.

What is vuetify Vue?

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.

How to get breakpoints from the app using vuetify?

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.

How do I watch the scrolling of a V-scroll element?

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.


3 Answers

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

like image 131
RwwL Avatar answered Oct 12 '22 23:10

RwwL


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;
  },
},
like image 30
Chris Keith Avatar answered Oct 13 '22 00:10

Chris Keith


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>
like image 42
Kamil Kiełczewski Avatar answered Oct 13 '22 00:10

Kamil Kiełczewski