Home > Software engineering >  How to scroll top and bottom in vue
How to scroll top and bottom in vue

Time:06-01

I am working on scroll to top and bottom button using vuetify.

Currently, i only able to scroll to bottom page. I want it to be able to scroll to the top of the page when the user reaches the bottom of the page and vice-versa.

I try to use v-if and v-else but it seems like it does not work. Any idea on how to implement scroll to top and bottom in one single button ?

ScrollToTopBottom.vue

<template>
    <div>
      <v-btn v-if = "!isVisible"
          v-scroll="onScroll" v-show="isVisible" fab fixed bottom right color="primary" @click="toBottom">
          <v-icon>mdi-arrow-down-bold-box-outline</v-icon>
      </v-btn>

      <v-btn v-else
          v-scroll="onScroll" v-show="isVisible" fab fixed bottom right color="primary" @click="toTop">
          <v-icon>mdi-arrow-up-bold-box-outline</v-icon>
      </v-btn>

    </div>
</template>

<script>

export default{
    data () {
        return {
        isVisible: false
    }
  },
   methods: {
    onScroll () {
       this.isVisible = window.scrollY > 50
    },
    toTop () {
      window.scrollTo({
        top: 0,
        left: 0,
        behavior: 'smooth'
      }
      )
    },
    toBottom(){
      window.scrollTo({
        top: document.body.scrollHeight,
        behavior: 'smooth'
      })
    }
  }
}

</script>

CodePudding user response:

Try like following snippet:

Vue.component('scrollToTopButton', {
  template: `
    <div v-scroll="onScroll">
      <v-btn v-if="!isVisible" fab fixed bottom right color="primary" @click="toBottom">
        <v-icon>mdi-arrow-down-bold-box-outline</v-icon>
      </v-btn>
      <v-btn v-else fab fixed bottom right color="primary" @click="toTop">
        <v-icon>mdi-arrow-up-bold-box-outline</v-icon>
      </v-btn>
    </div>
  `,
  data () {
    return {
      isVisible: false
    }
  },
   methods: {
    onScroll() {
      this.isVisible = window.scrollY > 50
    },
    toTop() {
      window.scrollTo({
        top: 0,
        left: 0,
        behavior: 'smooth'
      })
    },
    toBottom(){
      window.scrollTo({
        top: document.body.scrollHeight,
        behavior: 'smooth'
      })
    }
  }
})
new Vue({
  el: "#demo",
  vuetify: new Vuetify(),
  
})
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
<div id="demo" >
  <v-app>
    <v-main>
      <v-container>
        <div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div><div>xxx</div>
        <scroll-to-top-button />
      </v-container>
    </v-main>
  </v-app>
</div>

  • Related