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.

133 lines
3.8 KiB

4 months ago
  1. <template>
  2. <u-transition
  3. mode="fade"
  4. :customStyle="backTopStyle"
  5. :show="show"
  6. >
  7. <view
  8. class="u-back-top"
  9. :style="[contentStyle]"
  10. v-if="!$slots.default && !$slots.$default"
  11. @click="backToTop"
  12. >
  13. <u-icon
  14. :name="icon"
  15. :custom-style="iconStyle"
  16. ></u-icon>
  17. <text
  18. v-if="text"
  19. class="u-back-top__text"
  20. >{{text}}</text>
  21. </view>
  22. <slot v-else />
  23. </u-transition>
  24. </template>
  25. <script>
  26. import { props } from './props';
  27. import { mpMixin } from '../../libs/mixin/mpMixin';
  28. import { mixin } from '../../libs/mixin/mixin';
  29. import { addUnit, addStyle, getPx, deepMerge, error } from '../../libs/function/index';
  30. // #ifdef APP-NVUE
  31. const dom = weex.requireModule('dom')
  32. // #endif
  33. /**
  34. * backTop 返回顶部
  35. * @description 本组件一个用于长页面滑动一定距离后出现返回顶部按钮方便快速返回顶部的场景
  36. * @tutorial https://uview-plus.jiangruyi.com/components/backTop.html
  37. *
  38. * @property {String} mode 返回顶部的形状circle-圆形square-方形 默认 'circle'
  39. * @property {String} icon 自定义图标 默认 'arrow-upward' 见官方文档示例
  40. * @property {String} text 提示文字
  41. * @property {String | Number} duration 返回顶部滚动时间 默认 100
  42. * @property {String | Number} scrollTop 滚动距离 默认 0
  43. * @property {String | Number} top 距离顶部多少距离显示单位px 默认 400
  44. * @property {String | Number} bottom 返回顶部按钮到底部的距离单位px 默认 100
  45. * @property {String | Number} right 返回顶部按钮到右边的距离单位px 默认 20
  46. * @property {String | Number} zIndex 层级 默认 9
  47. * @property {Object<Object>} iconStyle 图标的样式对象形式 默认 {color: '#909399',fontSize: '19px'}
  48. * @property {Object} customStyle 定义需要用到的外部样式
  49. *
  50. * @example <u-back-top :scrollTop="scrollTop"></u-back-top>
  51. */
  52. export default {
  53. name: 'u-back-top',
  54. mixins: [mpMixin, mixin, props],
  55. computed: {
  56. backTopStyle() {
  57. // 动画组件样式
  58. const style = {
  59. bottom: addUnit(this.bottom),
  60. right: addUnit(this.right),
  61. width: '40px',
  62. height: '40px',
  63. position: 'fixed',
  64. zIndex: 10,
  65. }
  66. return style
  67. },
  68. show() {
  69. return getPx(this.scrollTop) > getPx(this.top)
  70. },
  71. contentStyle() {
  72. const style = {}
  73. let radius = 0
  74. // 是否圆形
  75. if(this.mode === 'circle') {
  76. radius = '100px'
  77. } else {
  78. radius = '4px'
  79. }
  80. // 为了兼容安卓nvue,只能这么分开写
  81. style.borderTopLeftRadius = radius
  82. style.borderTopRightRadius = radius
  83. style.borderBottomLeftRadius = radius
  84. style.borderBottomRightRadius = radius
  85. return deepMerge(style, addStyle(this.customStyle))
  86. }
  87. },
  88. emits: ["click"],
  89. methods: {
  90. backToTop() {
  91. // #ifdef APP-NVUE
  92. if (!this.$parent.$refs['u-back-top']) {
  93. error(`nvue页面需要给页面最外层元素设置"ref='u-back-top'`)
  94. }
  95. dom.scrollToElement(this.$parent.$refs['u-back-top'], {
  96. offset: 0
  97. })
  98. // #endif
  99. // #ifndef APP-NVUE
  100. uni.pageScrollTo({
  101. scrollTop: 0,
  102. duration: this.duration
  103. });
  104. // #endif
  105. this.$emit('click')
  106. }
  107. }
  108. }
  109. </script>
  110. <style lang="scss" scoped>
  111. @import '../../libs/css/components.scss';
  112. $u-back-top-flex:1 !default;
  113. $u-back-top-height:100% !default;
  114. $u-back-top-background-color:#E1E1E1 !default;
  115. $u-back-top-tips-font-size:12px !default;
  116. .u-back-top {
  117. @include flex;
  118. flex-direction: column;
  119. align-items: center;
  120. flex:$u-back-top-flex;
  121. height: $u-back-top-height;
  122. justify-content: center;
  123. background-color: $u-back-top-background-color;
  124. &__tips {
  125. font-size:$u-back-top-tips-font-size;
  126. transform: scale(0.8);
  127. }
  128. }
  129. </style>