tui-scroll-top.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="tui-scroll-top_box" v-show="isIndex || isShare || (visible && toggle)" :style="{ bottom: bottom + 'rpx', right: right + 'rpx' }">
  3. <view class="tui-scroll-top_item" v-if="isIndex" @tap.stop="goIndex">
  4. <image class="tui-scroll-top_img" :src="indexIcon"></image>
  5. <view class="tui-scroll-top_text">首页</view>
  6. </view>
  7. <button open-type="share" class="tui-share-btn" v-if="isShare && !customShare">
  8. <view class="tui-scroll-top_item" :class="{ 'tui-scroll-item_top': isIndex }"><image class="tui-scroll-top_img" :src="shareIcon"></image></view>
  9. </button>
  10. <view class="tui-scroll-top_item" :class="{ 'tui-scroll-item_top': isIndex }" v-if="isShare && customShare" @tap.stop="share">
  11. <image class="tui-scroll-top_img" :src="shareIcon"></image>
  12. </view>
  13. <view class="tui-scroll-top_item" :class="{ 'tui-scroll-item_top': isIndex || isShare }" v-show="visible && toggle" @tap.stop="goTop">
  14. <image class="tui-scroll-top_img" :src="topIcon"></image>
  15. <view class="tui-scroll-top_text tui-color-white">顶部</view>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. /**
  21. * 注意:组件中使用的图片地址,将文件复制到自己项目中
  22. * 如果图片位置与组件同级,编译成小程序时图片会丢失
  23. * 拷贝static下整个components文件夹
  24. * 也可直接转成base64(不建议)
  25. * */
  26. export default {
  27. name: 'tuiScrollTop',
  28. emits: ['index','share'],
  29. props: {
  30. //回顶部按钮距离底部距离 rpx
  31. bottom: {
  32. type: Number,
  33. default: 180
  34. },
  35. //回顶部按钮距离右侧距离 rpx
  36. right: {
  37. type: Number,
  38. default: 25
  39. },
  40. //距离顶部多少距离显示 px
  41. top: {
  42. type: Number,
  43. default: 200
  44. },
  45. //滚动距离
  46. scrollTop: {
  47. type: Number
  48. },
  49. //回顶部滚动时间
  50. duration: {
  51. type: Number,
  52. default: 120
  53. },
  54. //是否显示返回首页按钮
  55. isIndex: {
  56. type: Boolean,
  57. default: false
  58. },
  59. //是否显示分享图标
  60. isShare: {
  61. type: Boolean,
  62. default: false
  63. },
  64. //自定义分享(小程序可使用button=>open-type="share")
  65. customShare: {
  66. type: Boolean,
  67. default: false
  68. },
  69. //回顶部icon
  70. topIcon: {
  71. type: String,
  72. default: '/static/components/scroll-top/icon_top_3x.png'
  73. },
  74. //回首页icon
  75. indexIcon: {
  76. type: String,
  77. default: '/static/components/scroll-top/icon_index_3x.png'
  78. },
  79. //分享icon
  80. shareIcon: {
  81. type: String,
  82. default: '/static/components/scroll-top/icon_share_3x.png'
  83. }
  84. },
  85. watch: {
  86. scrollTop(newValue, oldValue) {
  87. this.change();
  88. }
  89. },
  90. data() {
  91. return {
  92. //判断是否显示
  93. visible: false,
  94. //控制显示,主要解决调用api滚到顶部fixed元素抖动的问题
  95. toggle: true
  96. };
  97. },
  98. methods: {
  99. goTop: function() {
  100. this.toggle = false;
  101. uni.pageScrollTo({
  102. scrollTop: 0,
  103. duration: this.duration
  104. });
  105. setTimeout(() => {
  106. this.toggle = true;
  107. }, 220);
  108. },
  109. goIndex: function() {
  110. this.$emit('index', {});
  111. },
  112. share() {
  113. this.$emit('share', {});
  114. },
  115. change() {
  116. let show = this.scrollTop > this.top;
  117. if ((show && this.visible) || (!show && !this.visible)) {
  118. return;
  119. }
  120. this.visible = show;
  121. }
  122. }
  123. };
  124. </script>
  125. <style scoped>
  126. .tui-scroll-top_box {
  127. width: 80rpx;
  128. height: 270rpx;
  129. position: fixed;
  130. z-index: 9999;
  131. right: 30rpx;
  132. bottom: 30rpx;
  133. font-weight: 400;
  134. }
  135. .tui-scroll-top_item {
  136. width: 80rpx;
  137. height: 80rpx;
  138. position: relative;
  139. }
  140. .tui-scroll-item_top {
  141. margin-top: 30rpx;
  142. }
  143. .tui-scroll-top_img {
  144. width: 80rpx;
  145. height: 80rpx;
  146. display: block;
  147. }
  148. .tui-scroll-top_text {
  149. width: 80rpx;
  150. text-align: center;
  151. font-size: 24rpx;
  152. line-height: 24rpx;
  153. transform: scale(0.92);
  154. transform-origin: center center;
  155. position: absolute;
  156. left: 0;
  157. bottom: 15rpx;
  158. }
  159. .tui-color-white {
  160. color: #fff;
  161. }
  162. .tui-share-btn {
  163. background: transparent !important;
  164. padding: 0;
  165. margin: 0;
  166. display: inline;
  167. border: 0;
  168. }
  169. .tui-share-btn::after {
  170. border: 0;
  171. }
  172. </style>