tui-scroll-top.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. props: {
  29. //回顶部按钮距离底部距离 rpx
  30. bottom: {
  31. type: Number,
  32. default: 180
  33. },
  34. //回顶部按钮距离右侧距离 rpx
  35. right: {
  36. type: Number,
  37. default: 25
  38. },
  39. //距离顶部多少距离显示 px
  40. top: {
  41. type: Number,
  42. default: 200
  43. },
  44. //滚动距离
  45. scrollTop: {
  46. type: Number
  47. },
  48. //回顶部滚动时间
  49. duration: {
  50. type: Number,
  51. default: 120
  52. },
  53. //是否显示返回首页按钮
  54. isIndex: {
  55. type: Boolean,
  56. default: false
  57. },
  58. //是否显示分享图标
  59. isShare: {
  60. type: Boolean,
  61. default: false
  62. },
  63. //自定义分享(小程序可使用button=>open-type="share")
  64. customShare: {
  65. type: Boolean,
  66. default: false
  67. },
  68. //回顶部icon
  69. topIcon: {
  70. type: String,
  71. default: '/static/components/scroll-top/icon_top_3x.png'
  72. },
  73. //回首页icon
  74. indexIcon: {
  75. type: String,
  76. default: '/static/components/scroll-top/icon_index_3x.png'
  77. },
  78. //分享icon
  79. shareIcon: {
  80. type: String,
  81. default: '/static/components/scroll-top/icon_share_3x.png'
  82. }
  83. },
  84. watch: {
  85. scrollTop(newValue, oldValue) {
  86. this.change();
  87. }
  88. },
  89. data() {
  90. return {
  91. //判断是否显示
  92. visible: false,
  93. //控制显示,主要解决调用api滚到顶部fixed元素抖动的问题
  94. toggle: true
  95. };
  96. },
  97. methods: {
  98. goTop: function() {
  99. this.toggle = false;
  100. uni.pageScrollTo({
  101. scrollTop: 0,
  102. duration: this.duration
  103. });
  104. setTimeout(() => {
  105. this.toggle = true;
  106. }, 220);
  107. },
  108. goIndex: function() {
  109. this.$emit('index', {});
  110. },
  111. share() {
  112. this.$emit('share', {});
  113. },
  114. change() {
  115. let show = this.scrollTop > this.top;
  116. if ((show && this.visible) || (!show && !this.visible)) {
  117. return;
  118. }
  119. this.visible = show;
  120. }
  121. }
  122. };
  123. </script>
  124. <style scoped>
  125. .tui-scroll-top_box {
  126. width: 80rpx;
  127. height: 270rpx;
  128. position: fixed;
  129. z-index: 9999;
  130. right: 30rpx;
  131. bottom: 30rpx;
  132. font-weight: 400;
  133. }
  134. .tui-scroll-top_item {
  135. width: 80rpx;
  136. height: 80rpx;
  137. position: relative;
  138. }
  139. .tui-scroll-item_top {
  140. margin-top: 30rpx;
  141. }
  142. .tui-scroll-top_img {
  143. width: 80rpx;
  144. height: 80rpx;
  145. display: block;
  146. }
  147. .tui-scroll-top_text {
  148. width: 80rpx;
  149. text-align: center;
  150. font-size: 24rpx;
  151. line-height: 24rpx;
  152. transform: scale(0.92);
  153. transform-origin: center center;
  154. position: absolute;
  155. left: 0;
  156. bottom: 15rpx;
  157. }
  158. .tui-color-white {
  159. color: #fff;
  160. }
  161. .tui-share-btn {
  162. background: transparent !important;
  163. padding: 0;
  164. margin: 0;
  165. display: inline;
  166. border: 0;
  167. }
  168. .tui-share-btn::after {
  169. border: 0;
  170. }
  171. </style>