mescroll-top.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <!-- 回到顶部的按钮 -->
  2. <template>
  3. <image
  4. v-if="mOption.src"
  5. class="mescroll-totop"
  6. :class="[value ? 'mescroll-totop-in' : 'mescroll-totop-out', {'mescroll-totop-safearea': 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. @supports (bottom: constant(safe-area-inset-bottom)) or (bottom: env(safe-area-inset-bottom)) {
  64. .mescroll-totop-safearea {
  65. margin-bottom: calc(var(--window-bottom) + constant(safe-area-inset-bottom)); /* window-bottom + 适配 iPhoneX */
  66. margin-bottom: calc(var(--window-bottom) + env(safe-area-inset-bottom));
  67. }
  68. }
  69. /* 显示 -- 淡入 */
  70. .mescroll-totop-in {
  71. opacity: 1;
  72. }
  73. /* 隐藏 -- 淡出且不接收事件*/
  74. .mescroll-totop-out {
  75. opacity: 0;
  76. pointer-events: none;
  77. }
  78. </style>