vue中上下滚动的方法
How to scroll top and bottom in vue
我正在使用 vuetify 滚动到顶部和底部按钮。
目前,我只能滚动到底部页面。我希望它能够在用户到达页面底部时滚动到页面顶部,反之亦然。
我尝试使用 v-if
和 v-else
但它似乎不起作用。关于如何在一个按钮中实现滚动到顶部和底部的任何想法?
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>
像下面的片段一样尝试:
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/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/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>
我正在使用 vuetify 滚动到顶部和底部按钮。
目前,我只能滚动到底部页面。我希望它能够在用户到达页面底部时滚动到页面顶部,反之亦然。
我尝试使用 v-if
和 v-else
但它似乎不起作用。关于如何在一个按钮中实现滚动到顶部和底部的任何想法?
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>
像下面的片段一样尝试:
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/font@6.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/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>