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