vue點擊右鍵出現(xiàn)自定義操作菜單實現(xiàn)代碼
一、前言


實現(xiàn)像微信一樣的點擊右鍵后出現(xiàn)操作菜單,對選中的數(shù)據(jù)項進行相應的操作,接下來介紹使用原生vue實現(xiàn)右鍵菜單的方法。
二、頁面代碼
<div v-if="isShow" class="warn_box">
<div
v-for="(item, index) in warnList"
:key="index"
:class="{ 'list': true, 'isTop': item.isTop, 'isSelected': activeIndex === index }"
@click="itemClick(index, Number(item.code))"
@contextmenu.prevent.stop="showMenu($event, index, item.configId)"
>
<!-- {{ item.name }} -->
<div
class="all"
>
<div class="left">
<div class="left_top">
<EllipsisPop :content="item.name" :row-num="1" width="180px"></EllipsisPop>
</div>
<div class="left_bottom">{{ item.code }}</div>
</div>
<div class="right">
<div class="right_top">{{ filterTime(item.maxCreateTime) }}</div>
<div
v-if="item.untreatedTotal"
class="right_bottom"
>
<el-badge
:value="item.untreatedTotal"
:max="999"
class="badge"
>
</el-badge>
</div>
</div>
</div>
</div>
<div v-if="isShowMenu" class="menu_box" :style="{'left': menuLeft + 'px', 'top': menuTop + 'px'}">
<div class="menu">
<div v-if="!isNowTop" class="menu_item item_text" @click.stop="stick(true)">置頂</div>
<div v-else class="menu_item item_text" @click.stop="stick(false)">取消置頂</div>
<!-- <div class="menu_item item_text" @click.stop="deleteList">刪除</div> -->
<el-popover v-model="showDelPop" placement="top" width="160" trigger="click">
<!-- <p>請確定是否刪除?</p> -->
<div style="text-align: center; margin: 0">
<p>請確定是否刪除?</p>
<el-button size="small" @click="showDelPop = false">取消</el-button>
<el-button type="primary" size="small" @click.stop="deleteList">確定</el-button>
</div>
<template #reference>
<div class="menu-item item_text" @click.stop="()=>{}">刪除</div>
</template>
</el-popover>
</div>
</div>
</div>
.menu_box {
position: fixed;
z-index: 1004;
background-color: #fff;
// border-radius: 5px;
.menu{
width: 100px;
text-align: left;
// padding: 5px;
box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
.menu_item{
height: 24px;
line-height: 22px;
// margin-top: 5px;
}
.item_text{
color: #171A1D;
cursor: pointer;
padding: 4px 20px;
// border-radius: 3px;
transition: all .2s ease-in;
}
.item_text:hover {
background-color: #E9EAEC;
}
}
}- @contextmenu.prevent.stop 為阻止瀏覽器的右鍵點擊菜單事件
- isShowMenu: 來控制菜單的顯示
三、頁面邏輯
同時我們要為其出現(xiàn)的地方進行調(diào)整 menuTop,menuLeft,在展示 menu 的時候我們將 event 的頁面位置屬性 e.pageX 和 e.pageX 拿來賦值
我們需要在頁面創(chuàng)建的時候增加 click 和 mousedonw 的監(jiān)聽,這樣就可以在我們點擊別的地方的時候?qū)⒉藛坞[藏
// 右鍵菜單
const isShowMenu = ref(false) // 控制是否顯示右鍵菜單
const menuLeft = ref(0)
const menuTop = ref(0)
const openMenuConfigId = ref(0) // 打開右鍵菜單的那一行的configId
const isNowTop = ref(false) // 是否已置頂
const showMenu = async(e:any, index:number, configId:number):Promise<void> => {
// console.log(e, index, configId)
// eslint-disable-next-line
const data = await window.app.windowGetData(store.userBaseInfo.username + '.data.warn')
data.forEach((item:any) => {
if (item.configId === configId) {
if (item.isTop) {
isNowTop.value = true
} else {
isNowTop.value = false
}
}
})
openMenuConfigId.value = configId
isShowMenu.value = true
menuLeft.value = e.pageX
menuTop.value = e.pageY
}// 置頂
const stick = async(status:boolean):Promise<void> => {
// eslint-disable-next-line
let data = await window.app.windowGetData(store.userBaseInfo.username + '.data.warn')
data.forEach((item:any) => {
if (item.configId === openMenuConfigId.value) {
item.isTop = status
}
})
const topList:any[] = []
data.forEach((item:any) => {
if (item.isTop) {
topList.push(item)
}
})
topList.sort((a:any, b:any) => (b.maxCreateTime - a.maxCreateTime))
data = data.filter((item:any) => (item.isTop === false))
data.sort((a:any, b:any) => (b.maxCreateTime - a.maxCreateTime))
data = topList.concat(data)
warnList = JSON.parse(JSON.stringify(data))
$forceUpdate()
// eslint-disable-next-line
window.app.windowStoreData(store.userBaseInfo.username + '.data.warn', data)
clockMenu()
}// 刪除
const showDelPop = ref(false)
const deleteList = async():Promise<void> => {
// eslint-disable-next-line
let data = await window.app.windowGetData(store.userBaseInfo.username + '.data.warn')
data = data.filter((item:any) => (item.configId !== openMenuConfigId.value))
warnList = JSON.parse(JSON.stringify(data))
$forceUpdate()
// eslint-disable-next-line
window.app.windowStoreData(store.userBaseInfo.username + '.data.warn', data)
clockMenu()
}
// 關閉菜單
const clockMenu = ():void => {
isShowMenu.value = false
}// 失去焦點時關閉右擊菜單
mounted() {
//失去焦點時關閉右擊菜單
document.addEventListener("click", (e) => {
if (!this.$refs.rightMenu.contains(e.target))
this.rightMenuVisible = false;
});
}四、總結(jié)
到此這篇關于vue點擊右鍵出現(xiàn)自定義操作菜單實現(xiàn)的文章就介紹到這了,更多相關vue點擊右鍵自定義操作菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue 路由守衛(wèi)(導航守衛(wèi))及其具體使用
這篇文章主要介紹了vue 路由守衛(wèi)(導航守衛(wèi))及其具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02
用v-html解決Vue.js渲染中html標簽不被解析的問題
這篇文章主要給大家介紹了如何利用v-html解決Vue.js渲染過程中html標簽不能被解析,html標簽顯示為字符串的問題,文中通過圖文介紹的很詳細,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
Vue3 Element Plus el-form表單組件示例詳解
這篇文章主要介紹了Vue3 Element Plus el-form表單組件,Element Plus 是 ElementUI 的升級版,提供了更多的表單控件和功能,同時還改進了一些細節(jié)和樣式,本文結(jié)合示例代碼給大家詳細講解,需要的朋友可以參考下2023-04-04

