uni-events-helper-wx
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.

137 lines
3.9 KiB

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