scroll-btn.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <view class="scroll-btn" :style="{ bottom: bottom }" @click.stop="fnScroll()">
  3. <text v-if="_scrollTop >= 180" class="iconfont icon-long-arrow-up"></text>
  4. <text v-else class="iconfont icon-long-arrow-down"></text>
  5. </view>
  6. </template>
  7. <script>
  8. import throttle from '@/utils/throttle.js';
  9. export default {
  10. name: 'scroll-btn',
  11. props: {
  12. bottom: {
  13. type: String,
  14. default: '180rpx'
  15. },
  16. scrollTop: {
  17. type: Number,
  18. default: 0
  19. }
  20. },
  21. data() {
  22. return { timer: null, _scrollTop: 0 };
  23. },
  24. watch: {
  25. scrollTop(val) {
  26. this._scrollTop = val;
  27. this.$forceUpdate();
  28. }
  29. },
  30. created() {
  31. this._scrollTop = this.scrollTop;
  32. },
  33. methods: {
  34. fnScroll() {
  35. throttle(() => {
  36. this.$emit('on-status', false);
  37. const isTop = this._scrollTop >= 180;
  38. this._scrollTop = isTop ? 0 : 999999999999;
  39. uni.pageScrollTo({
  40. duration: 500,
  41. scrollTop: this._scrollTop,
  42. success: () => {
  43. clearTimeout(this.timer);
  44. this.timer = setTimeout(() => {
  45. this.$emit('on-status', true);
  46. }, 500);
  47. }
  48. });
  49. this.$emit('update:scrollTop', this._scrollTop);
  50. });
  51. }
  52. }
  53. };
  54. </script>
  55. <style scoped lang="scss">
  56. .scroll-btn {
  57. position: fixed;
  58. // bottom: 200rpx;
  59. // right: -90rpx;
  60. right: 52rpx;
  61. width: 80rpx;
  62. height: 80rpx;
  63. display: flex;
  64. align-items: center;
  65. justify-content: center;
  66. box-sizing: border-box;
  67. border-radius: 50%;
  68. border: 4rpx solid #ffffff;
  69. background-color: #bfe9ef;
  70. box-shadow: 0rpx 4rpx 24rpx rgba(0, 0, 0, 0.1);
  71. z-index: 99;
  72. transition: right 0.5s ease-in-out;
  73. &.is-show {
  74. right: 52rpx;
  75. }
  76. .iconfont {
  77. font-size: 36rpx;
  78. color: #555;
  79. }
  80. }
  81. </style>