tui-scroll-top.vue 5.2 KB

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