u-back-top.vue 3.8 KB

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