| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <template>
- <view>
- <view @longpress.stop="onLongPress($event)" @touchmove="onTouchMove">
- <slot></slot>
- </view>
- <view v-if="isShowMenu" class="pop-menu" @touchstart="onClose()" @contextmenu.prevent=""></view>
- <view v-if="isShowMenu" class="menu" :style="menuStyle">
- <view class="menu-item" v-for="(item) in items" :key="item.key" @click.prevent="onSelectMenu(item)">
- <uni-icons class="menu-icon" :type="item.icon" :style="itemStyle(item)" size="22"></uni-icons>
- <text :style="itemStyle(item)"> {{item.name}}</text>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default {
- name: "pop-menu",
- data() {
- return {
- isShowMenu : false,
- style : ""
- }
- },
- props: {
- items: {
- type: Array
- }
- },
- methods: {
- onLongPress(e){
- uni.getSystemInfo({
- success: (res) => {
- let touches = e.touches[0];
- let style = "";
- /* 因 非H5端不兼容 style 属性绑定 Object ,所以拼接字符 */
- if (touches.clientY > (res.windowHeight / 2)) {
- style = `bottom:${res.windowHeight-touches.clientY}px;`;
- } else {
- style = `top:${touches.clientY}px;`;
- }
- if (touches.clientX > (res.windowWidth / 2)) {
- style += `right:${res.windowWidth-touches.clientX}px;`;
- } else {
- style += `left:${touches.clientX}px;`;
- }
- this.menuStyle = style;
- //
- this.$nextTick(() => {
- this.isShowMenu = true;
- });
- }
- })
- },
- onTouchMove(){
- this.onClose()
- },
- onSelectMenu(item) {
- this.$emit("select", item);
- this.isShowMenu = false;
- },
- onClose() {
- this.isShowMenu = false;
- },
- itemStyle(item){
- if(item.color){
- return `color:${item.color};`
- }
- return `color:#4f76e6;`;
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .pop-menu {
- position: fixed;
- left: 0;
- top: 0;
- right: 0;
- bottom: 0;
- width: 100%;
- height: 100%;
- background-color: #333;
- z-index: 999;
- opacity: 0.5;
- }
-
- .menu {
- position: fixed;
- border: 1px solid #b4b4b4;
- border-radius: 7px;
- overflow: hidden;
- background-color: #f5f6ff;
- z-index: 1000;
- .menu-item {
- height: 25px;
- min-width: 150rpx;
- line-height: 25px;
- font-size: 18px;
- display: flex;
- padding: 10px;
- justify-content: center;
- border-bottom: 1px solid #d0d0d8;
-
- .menu-icon {
- margin-right: 10rpx;
- }
- }
- }
-
- </style>
|