vue點(diǎn)擊右鍵出現(xiàn)自定義操作菜單實(shí)現(xiàn)代碼
一、前言
實(shí)現(xiàn)像微信一樣的點(diǎn)擊右鍵后出現(xiàn)操作菜單,對(duì)選中的數(shù)據(jù)項(xiàng)進(jìn)行相應(yīng)的操作,接下來(lái)介紹使用原生vue實(shí)現(xiàn)右鍵菜單的方法。
二、頁(yè)面代碼
<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>請(qǐng)確定是否刪除?</p> --> <div style="text-align: center; margin: 0"> <p>請(qǐng)確定是否刪除?</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 為阻止瀏覽器的右鍵點(diǎn)擊菜單事件
- isShowMenu: 來(lái)控制菜單的顯示
三、頁(yè)面邏輯
同時(shí)我們要為其出現(xiàn)的地方進(jìn)行調(diào)整 menuTop,menuLeft,在展示 menu 的時(shí)候我們將 event 的頁(yè)面位置屬性 e.pageX 和 e.pageX 拿來(lái)賦值
我們需要在頁(yè)面創(chuàng)建的時(shí)候增加 click 和 mousedonw 的監(jiān)聽(tīng),這樣就可以在我們點(diǎn)擊別的地方的時(shí)候?qū)⒉藛坞[藏
// 右鍵菜單 const isShowMenu = ref(false) // 控制是否顯示右鍵菜單 const menuLeft = ref(0) const menuTop = ref(0) const openMenuConfigId = ref(0) // 打開(kāi)右鍵菜單的那一行的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() } // 關(guān)閉菜單 const clockMenu = ():void => { isShowMenu.value = false }
// 失去焦點(diǎn)時(shí)關(guān)閉右擊菜單
mounted() { //失去焦點(diǎn)時(shí)關(guān)閉右擊菜單 document.addEventListener("click", (e) => { if (!this.$refs.rightMenu.contains(e.target)) this.rightMenuVisible = false; }); }
四、總結(jié)
到此這篇關(guān)于vue點(diǎn)擊右鍵出現(xiàn)自定義操作菜單實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)vue點(diǎn)擊右鍵自定義操作菜單內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue 路由守衛(wèi)(導(dǎo)航守衛(wèi))及其具體使用
這篇文章主要介紹了vue 路由守衛(wèi)(導(dǎo)航守衛(wèi))及其具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02用v-html解決Vue.js渲染中html標(biāo)簽不被解析的問(wèn)題
這篇文章主要給大家介紹了如何利用v-html解決Vue.js渲染過(guò)程中html標(biāo)簽不能被解析,html標(biāo)簽顯示為字符串的問(wèn)題,文中通過(guò)圖文介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來(lái)一起看看吧。2016-12-12使用vue-router為每個(gè)路由配置各自的title
這篇文章主要介紹了如何使用vue-router為每個(gè)路由配置各自的title,及使用vue router的方法,需要的朋友可以參考下2018-07-07前端token中4個(gè)存儲(chǔ)位置的優(yōu)缺點(diǎn)說(shuō)明
這篇文章主要介紹了前端token中4個(gè)存儲(chǔ)位置的優(yōu)缺點(diǎn)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10Vue3 Element Plus el-form表單組件示例詳解
這篇文章主要介紹了Vue3 Element Plus el-form表單組件,Element Plus 是 ElementUI 的升級(jí)版,提供了更多的表單控件和功能,同時(shí)還改進(jìn)了一些細(xì)節(jié)和樣式,本文結(jié)合示例代碼給大家詳細(xì)講解,需要的朋友可以參考下2023-04-04基于Vue實(shí)例對(duì)象的數(shù)據(jù)選項(xiàng)
下面小編就為大家?guī)?lái)一篇基于Vue實(shí)例對(duì)象的數(shù)據(jù)選項(xiàng)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Vue中對(duì)數(shù)組和對(duì)象進(jìn)行遍歷和修改方式
這篇文章主要介紹了Vue中對(duì)數(shù)組和對(duì)象進(jìn)行遍歷和修改方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08