guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame^] | 1 | <template> |
| 2 | <view @tap="backToTop" class="u-back-top" :class="['u-back-top--mode--' + mode]" :style="[{ |
| 3 | bottom: bottom + 'rpx', |
| 4 | right: right + 'rpx', |
| 5 | borderRadius: mode == 'circle' ? '10000rpx' : '8rpx', |
| 6 | zIndex: uZIndex, |
| 7 | opacity: opacity |
| 8 | }, customStyle]"> |
| 9 | <view class="u-back-top__content" v-if="!$slots.default && !$slots.$default"> |
| 10 | <u-icon @click="backToTop" :name="icon" :custom-style="iconStyle"></u-icon> |
| 11 | <view class="u-back-top__content__tips"> |
| 12 | {{tips}} |
| 13 | </view> |
| 14 | </view> |
| 15 | <slot v-else /> |
| 16 | </view> |
| 17 | </template> |
| 18 | |
| 19 | <script> |
| 20 | export default { |
| 21 | name: 'u-back-top', |
| 22 | props: { |
| 23 | // 返回顶部的形状,circle-圆形,square-方形 |
| 24 | mode: { |
| 25 | type: String, |
| 26 | default: 'circle' |
| 27 | }, |
| 28 | // 自定义图标 |
| 29 | icon: { |
| 30 | type: String, |
| 31 | default: 'arrow-upward' |
| 32 | }, |
| 33 | // 提示文字 |
| 34 | tips: { |
| 35 | type: String, |
| 36 | default: '' |
| 37 | }, |
| 38 | // 返回顶部滚动时间 |
| 39 | duration: { |
| 40 | type: [Number, String], |
| 41 | default: 100 |
| 42 | }, |
| 43 | // 滚动距离 |
| 44 | scrollTop: { |
| 45 | type: [Number, String], |
| 46 | default: 0 |
| 47 | }, |
| 48 | // 距离顶部多少距离显示,单位rpx |
| 49 | top: { |
| 50 | type: [Number, String], |
| 51 | default: 400 |
| 52 | }, |
| 53 | // 返回顶部按钮到底部的距离,单位rpx |
| 54 | bottom: { |
| 55 | type: [Number, String], |
| 56 | default: 200 |
| 57 | }, |
| 58 | // 返回顶部按钮到右边的距离,单位rpx |
| 59 | right: { |
| 60 | type: [Number, String], |
| 61 | default: 40 |
| 62 | }, |
| 63 | // 层级 |
| 64 | zIndex: { |
| 65 | type: [Number, String], |
| 66 | default: '9' |
| 67 | }, |
| 68 | // 图标的样式,对象形式 |
| 69 | iconStyle: { |
| 70 | type: Object, |
| 71 | default() { |
| 72 | return { |
| 73 | color: '#909399', |
| 74 | fontSize: '38rpx' |
| 75 | } |
| 76 | } |
| 77 | }, |
| 78 | // 整个组件的样式 |
| 79 | customStyle: { |
| 80 | type: Object, |
| 81 | default() { |
| 82 | return {} |
| 83 | } |
| 84 | } |
| 85 | }, |
| 86 | watch: { |
| 87 | showBackTop(nVal, oVal) { |
| 88 | // 当组件的显示与隐藏状态发生跳变时,修改组件的层级和不透明度 |
| 89 | // 让组件有显示和消失的动画效果,如果用v-if控制组件状态,将无设置动画效果 |
| 90 | if(nVal) { |
| 91 | this.uZIndex = this.zIndex; |
| 92 | this.opacity = 1; |
| 93 | } else { |
| 94 | this.uZIndex = -1; |
| 95 | this.opacity = 0; |
| 96 | } |
| 97 | } |
| 98 | }, |
| 99 | computed: { |
| 100 | showBackTop() { |
| 101 | // 由于scrollTop为页面的滚动距离,默认为px单位,这里将用于传入的top(rpx)值 |
| 102 | // 转为px用于比较,如果滚动条到顶的距离大于设定的距离,就显示返回顶部的按钮 |
| 103 | return this.scrollTop > uni.upx2px(this.top); |
| 104 | }, |
| 105 | }, |
| 106 | data() { |
| 107 | return { |
| 108 | // 不透明度,为了让组件有一个显示和隐藏的过渡动画 |
| 109 | opacity: 0, |
| 110 | // 组件的z-index值,隐藏时设置为-1,就会看不到 |
| 111 | uZIndex: -1 |
| 112 | } |
| 113 | }, |
| 114 | methods: { |
| 115 | backToTop() { |
| 116 | uni.pageScrollTo({ |
| 117 | scrollTop: 0, |
| 118 | duration: this.duration |
| 119 | }); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | </script> |
| 124 | |
| 125 | <style lang="scss" scoped> |
| 126 | @import "../../libs/css/style.components.scss"; |
| 127 | |
| 128 | .u-back-top { |
| 129 | width: 80rpx; |
| 130 | height: 80rpx; |
| 131 | position: fixed; |
| 132 | z-index: 9; |
| 133 | @include vue-flex; |
| 134 | flex-direction: column; |
| 135 | justify-content: center; |
| 136 | background-color: #E1E1E1; |
| 137 | color: $u-content-color; |
| 138 | align-items: center; |
| 139 | transition: opacity 0.4s; |
| 140 | |
| 141 | &__content { |
| 142 | @include vue-flex; |
| 143 | flex-direction: column; |
| 144 | align-items: center; |
| 145 | |
| 146 | &__tips { |
| 147 | font-size: 24rpx; |
| 148 | transform: scale(0.8); |
| 149 | line-height: 1; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | </style> |