long-press-menu.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view class="long-press-menu none-pointer-events">
  3. <view @longpress.prevent.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 + 20}px;`;
  42. } else {
  43. style = `top:${touches.clientY + 20}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. this.menuTouch = touches
  56. }
  57. })
  58. },
  59. onTouchMove(e) {
  60. this.isTouchMove = true;
  61. let touches = e.touches[0];
  62. // 屏幕拖动大于50px时,取消菜单
  63. if (Math.abs(touches.clientX - this.menuTouch.clientX) > 50 ||
  64. Math.abs(touches.clientY - this.menuTouch.clientY) > 50) {
  65. this.onClose();
  66. }
  67. },
  68. onTouchEnd() {
  69. this.isTouchMove = false;
  70. },
  71. onSelectMenu(item) {
  72. this.$emit("select", item);
  73. this.isShowMenu = false;
  74. },
  75. onClose() {
  76. this.isShowMenu = false;
  77. },
  78. itemStyle(item) {
  79. if (item.color) {
  80. return `color:${item.color};`
  81. }
  82. return `color:#000;`;
  83. }
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. .long-press-menu {
  89. .menu-mask {
  90. position: fixed;
  91. left: 0;
  92. top: 0;
  93. right: 0;
  94. bottom: 0;
  95. width: 100%;
  96. height: 100%;
  97. z-index: 999;
  98. }
  99. .menu {
  100. position: fixed;
  101. border-radius: 4px;
  102. overflow: hidden;
  103. background-color: #fff;
  104. z-index: 1000;
  105. box-shadow: $im-box-shadow-dark;
  106. .menu-item {
  107. height: 28px;
  108. min-width: 120rpx;
  109. line-height: 28px;
  110. font-size: $im-font-size-small;
  111. display: flex;
  112. padding: 6px 20px;
  113. justify-content: flex-start;
  114. &:hover {
  115. background: $im-bg-active;
  116. }
  117. .menu-icon {
  118. margin-right: 10rpx;
  119. }
  120. }
  121. }
  122. }
  123. </style>