爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.3 KiB

2 weeks ago
  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. <script>
  19. export default {
  20. name: 'u-back-top',
  21. emits: ["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. this.$emit('top');
  121. }
  122. }
  123. }
  124. </script>
  125. <style lang="scss" scoped>
  126. @import "../../libs/css/style.components.scss";
  127. .u-back-top {
  128. width: 80rpx;
  129. height: 80rpx;
  130. position: fixed;
  131. z-index: 9;
  132. @include vue-flex;
  133. flex-direction: column;
  134. justify-content: center;
  135. background-color: #E1E1E1;
  136. color: $u-content-color;
  137. align-items: center;
  138. transition: opacity 0.4s;
  139. &__content {
  140. @include vue-flex;
  141. flex-direction: column;
  142. align-items: center;
  143. &__tips {
  144. font-size: 24rpx;
  145. transform: scale(0.8);
  146. line-height: 1;
  147. }
  148. }
  149. }
  150. </style>