mescroll-top.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <!-- 回到顶部的按钮 -->
  2. <template>
  3. <image
  4. v-if="mOption.src"
  5. class="mescroll-totop"
  6. :class="[value ? 'mescroll-totop-in' : 'mescroll-totop-out', {'mescroll-safe-bottom': mOption.safearea}]"
  7. :style="{'z-index':mOption.zIndex, 'left': left, 'right': right, 'bottom':addUnit(mOption.bottom), 'width':addUnit(mOption.width), 'border-radius':addUnit(mOption.radius)}"
  8. :src="mOption.src"
  9. mode="widthFix"
  10. @click="toTopClick"
  11. />
  12. </template>
  13. <script>
  14. export default {
  15. props: {
  16. // up.toTop的配置项
  17. option: Object,
  18. // 是否显示
  19. value: false
  20. },
  21. computed: {
  22. // 支付宝小程序需写成计算属性,prop定义default仍报错
  23. mOption(){
  24. return this.option || {}
  25. },
  26. // 优先显示左边
  27. left(){
  28. return this.mOption.left ? this.addUnit(this.mOption.left) : 'auto';
  29. },
  30. // 右边距离 (优先显示左边)
  31. right() {
  32. return this.mOption.left ? 'auto' : this.addUnit(this.mOption.right);
  33. }
  34. },
  35. methods: {
  36. addUnit(num){
  37. if(!num) return 0;
  38. if(typeof num === 'number') return num + 'rpx';
  39. return num
  40. },
  41. toTopClick() {
  42. this.$emit('input', false); // 使v-model生效
  43. this.$emit('click'); // 派发点击事件
  44. }
  45. }
  46. };
  47. </script>
  48. <style>
  49. /* 回到顶部的按钮 */
  50. .mescroll-totop {
  51. z-index: 9990;
  52. position: fixed !important; /* 加上important避免编译到H5,在多mescroll中定位失效 */
  53. right: 20rpx;
  54. bottom: 120rpx;
  55. width: 72rpx;
  56. height: auto;
  57. border-radius: 50%;
  58. opacity: 0;
  59. transition: opacity 0.5s; /* 过渡 */
  60. margin-bottom: var(--window-bottom); /* css变量 */
  61. }
  62. /* 适配 iPhoneX */
  63. .mescroll-safe-bottom{
  64. margin-bottom: calc(var(--window-bottom) + constant(safe-area-inset-bottom)); /* window-bottom + 适配 iPhoneX */
  65. margin-bottom: calc(var(--window-bottom) + env(safe-area-inset-bottom));
  66. }
  67. /* 显示 -- 淡入 */
  68. .mescroll-totop-in {
  69. opacity: 1;
  70. }
  71. /* 隐藏 -- 淡出且不接收事件*/
  72. .mescroll-totop-out {
  73. opacity: 0;
  74. pointer-events: none;
  75. }
  76. </style>