最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to scroll top and bottom in vue - Stack Overflow

programmeradmin0浏览0评论

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>

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>
Share Improve this question edited Jun 1, 2022 at 8:38 Gavin Teo Juen asked Jun 1, 2022 at 5:37 Gavin Teo JuenGavin Teo Juen 3202 gold badges8 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Try like following snippet:

Vue.ponent('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/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr/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>

发布评论

评论列表(0)

  1. 暂无评论