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