long-press-menu.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view>
  3. <view @longpress.stop="onLongPress($event)" @touchmove="onTouchMove" @touchend="onTouchEnd">
  4. <slot></slot>
  5. </view>
  6. <view v-if="isShowMenu" class="menu-mask" @touchstart="onClose()" @contextmenu.prevent=""></view>
  7. <view v-if="isShowMenu" class="menu" :style="menuStyle">
  8. <view class="menu-item" v-for="(item) in items" :key="item.key" @click.prevent="onSelectMenu(item)">
  9. <text :style="itemStyle(item)"> {{ item.name }}</text>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. name: "long-press-menu",
  17. data() {
  18. return {
  19. isShowMenu: false,
  20. isTouchMove: false,
  21. style: ""
  22. }
  23. },
  24. props: {
  25. items: {
  26. type: Array
  27. }
  28. },
  29. methods: {
  30. onLongPress(e) {
  31. if (this.isTouchMove) {
  32. // 屏幕移动时不弹出
  33. return;
  34. }
  35. uni.getSystemInfo({
  36. success: (res) => {
  37. let touches = e.touches[0];
  38. let style = "";
  39. /* 因 非H5端不兼容 style 属性绑定 Object ,所以拼接字符 */
  40. if (touches.clientY > (res.windowHeight / 2)) {
  41. style = `bottom:${res.windowHeight - touches.clientY}px;`;
  42. } else {
  43. style = `top:${touches.clientY}px;`;
  44. }
  45. if (touches.clientX > (res.windowWidth / 2)) {
  46. style += `right:${res.windowWidth - touches.clientX}px;`;
  47. } else {
  48. style += `left:${touches.clientX}px;`;
  49. }
  50. this.menuStyle = style;
  51. //
  52. this.$nextTick(() => {
  53. this.isShowMenu = true;
  54. });
  55. }
  56. })
  57. },
  58. onTouchMove() {
  59. this.onClose();
  60. this.isTouchMove = true;
  61. },
  62. onTouchEnd() {
  63. this.isTouchMove = false;
  64. },
  65. onSelectMenu(item) {
  66. this.$emit("select", item);
  67. this.isShowMenu = false;
  68. },
  69. onClose() {
  70. this.isShowMenu = false;
  71. },
  72. itemStyle(item) {
  73. if (item.color) {
  74. return `color:${item.color};`
  75. }
  76. return `color:#000;`;
  77. }
  78. }
  79. }
  80. </script>
  81. <style lang="scss" scoped>
  82. .menu-mask {
  83. position: fixed;
  84. left: 0;
  85. top: 0;
  86. right: 0;
  87. bottom: 0;
  88. width: 100%;
  89. height: 100%;
  90. z-index: 999;
  91. }
  92. .menu {
  93. position: fixed;
  94. border-radius: 4px;
  95. overflow: hidden;
  96. background-color: #fff;
  97. z-index: 1000;
  98. box-shadow: $im-box-shadow-dark;
  99. .menu-item {
  100. height: 28px;
  101. min-width: 120rpx;
  102. line-height: 28px;
  103. font-size: $im-font-size-small;
  104. display: flex;
  105. padding: 6px 20px;
  106. justify-content: flex-start;
  107. &:hover {
  108. background: $im-bg-active;
  109. }
  110. .menu-icon {
  111. margin-right: 10rpx;
  112. }
  113. }
  114. }
  115. </style>