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