pop-menu.vue 2.3 KB

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