此文章轉載於福建開源社區http://www.fjkysq.com/blog/77.html
一、前言
elementUI有說明文檔,但我為什么還要重新寫一下呢?因為文檔也有坑,一開始使用時你復制進去,可能都沒有效果。也不知道原因在哪,就如Backtop回到頂部的組件,不去看源碼,真心不知道是怎么個所以然。一開始,我把這個組件放到我頁面的底部,結果是無效果的,而且還會報css的這兩個樣式錯誤(.page-component__scroll .el-scrollbar__wrap),看完這個文檔,也沒找到這兩個是什么東西,在哪設置。全文搜索,也沒找到這兩個css。最后逼我進去看Backtop組件源碼,看懂后,刪除了沒必要的東西,放置的位置調整一下,完美解決。這也是本站使用的回到頂部的效果。以下我會貼出官方文檔及源碼,還有解決思路
二、官方文檔 https://element.eleme.cn/#/zh-CN/component/backtop
Backtop 回到頂部
返回頁面頂部的操作按鈕
基礎用法
滑動頁面即可看到右下方的按鈕。
<template>
Scroll down to see the bottom-right button. <el-backtop target=".page-component__scroll .el-scrollbar__wrap"></el-backtop> </template>
自定義顯示內容
顯示區域被固定為 40px * 40px 的區域, 其中的內容可支持自定義。
<template> Scroll down to see the bottom-right button. <el-backtop target=".page-component__scroll .el-scrollbar__wrap" :bottom="100"> <div style="{ height: 100%; width: 100%; background-color: #f2f5f6; box-shadow: 0 0 6px rgba(0,0,0, .12); text-align: center; line-height: 40px; color: #1989fa; }" > UP </div> </el-backtop> </template>
如果沒試過的可以先跟着官方的文檔試下,看是否可行,若不可行,接着往下看
三、el-backtop組件源碼
<template> <transition name="el-fade-in"> <div v-if="visible" @click.stop="handleClick" :style="{ 'right': styleRight, 'bottom': styleBottom }" class="el-backtop"> <slot> <el-icon name="caret-top"></el-icon> </slot> </div> </transition> </template> <script> import throttle from 'throttle-debounce/throttle'; export default { name: 'ElBacktop', props: { visibilityHeight: { type: Number, default: 200 }, target: [String], right: { type: Number, default: 40 }, bottom: { type: Number, default: 40 } }, data() { return { el: null, container: null, visible: false }; }, computed: { styleBottom() {