<template>Scroll down to see the bottom-right button.<el-backtop target=".page-component__scroll .el-scrollbar__wrap"></el-backtop>
</template>
把target指向你要產生“回到頂部”按鈕的組件,
這個組件一定要是產生滾動條的組件!
這個組件一定要是產生滾動條的組件!
這個組件一定要是產生滾動條的組件!
舉個栗子,App.vue中的栗子
<template><div id="app" class="wrapper" style="height:520px;overflow: auto;"><div class="wrapper-content" style="height:2020px;background-color:aliceblue;text-align:center;position:relative;"><!-- 回到頂部 --><template><el-backtop target=".wrapper-content"></el-backtop></template><div>頂部</div><div style="position:absolute;bottom:0;left:49%;">底部</div></div></div>
</template><script>
export default {};
</script><style scoped>
html,body,#app {height: 100%;
}
</style>
這樣是不會產生按鈕的,因為wrapper-content組件不會產生滾動條,更不會有滾動事件,要把target改成 target=“.wrapper”,因為真正產生滾動事件的是wrapper。
注意這句樣式:
<div id="app" class="wrapper" style="height:520px;overflow: auto;">
要給父組件overflow: auto,和高度,否則父組件會被自動撐開,不產生滾動事件(如果有滾動條那也是上級的,如果上級一直沒有設置height那就是window的);把520px設置為100%也是沒有用的,可以用document.body.offsetHeight
這個方法設置適合屏幕的高度;若不想設置具體高度則加上下面這句樣式,把上級高度全部100%也是管用的
html,body,#app {height: 100%;}
附上官網介紹
- target最好用id,因為唯一
- template必不可少
- 使用target以外的屬性時一定要用冒號(v-bind),因為它接收的是數字,不用冒號只能傳字符串,如
<el-backtop target=".wrapper" :visibility-height="200" :right="40" :bottom="40"></el-backtop>