vue實(shí)現(xiàn)右鍵點(diǎn)擊彈框信息功能
vue右鍵點(diǎn)擊彈框信息功能
當(dāng)某個(gè)地方需要右鍵彈出信息,已供快速選擇時(shí),例如一些用到慣用語(yǔ)的地方。

輸入框部分代碼
<el-row style="margin-top:50px">
<el-col :span="24">
<el-form-item label="意見(jiàn)"
prop="note1">
<div v-click-outside>
<div @contextmenu.prevent="rightShow">
<el-input type="textarea" :autosize="{ minRows: 4}" v-model="ruleForm.note1" @input="forceInput()" placeholder="請(qǐng)輸入整改驗(yàn)收意見(jiàn)" ></el-input>
<ul id="menu" ref="msgRightMenu" v-show="isPersoncontextMenus">
<li @click.stop="selectPhrase(item)"
v-for="item in sceneList"
:key="item.id">{{ item.content}}</li>
</ul>
</div>
</div>
</el-form-item>
</el-col>
</el-row>
showRightMenu: false, isPersoncontextMenus:false, sceneList:[]
// 位置在 export default {} 里,點(diǎn)擊輸入框外的地方會(huì)關(guān)閉彈窗
directives: {
clickOutside: {//自定義指令:clickOutside
bind (el, bindings, vnode) {//自定義指令生命周期:bind ;參數(shù):el, bindings, vnode
let handler = (e) => {
if (el.contains(e.target)) {
if (!vnode.context.isPersoncontextMenus) {
vnode.context.focus()
}
} else {
if (vnode.context.isPersoncontextMenus) {
vnode.context.blur()
}
}
}
el.handler = handler
document.addEventListener('click', handler)
},
unbind (el) {//自定義指令生命周期:unbind
document.removeEventListener('click', el.handler)
}
}
},
// methods
rightShow() {
let menu = this.$refs.msgRightMenu
this.isPersoncontextMenus = true
this.$post('/test/getList', {}, res => {
if (res.success === true) {
this.sceneList = res.data;
} else {
this.isCommiting = false
this.isPersoncontextMenus = false
}
})
var evt = event || window.event;
var clientWidth = document.documentElement.clientWidth || document.body.clientWidth ;
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
var clientHeight = document.documentElement.clientHeight || document.body.clientHeight ;
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop ;
//給left和top分別賦值為鼠標(biāo)的位置;
menu.style.left = evt.pageX+"px";
menu.style.top = evt.pageY+"px";
//如果鼠標(biāo)右邊放不下菜單,就把left的值的改了
if(evt.pageX+100>clientWidth+scrollLeft){//菜單應(yīng)該在鼠標(biāo)左邊;
var left1 = evt.pageX-100;
menu.style.left = left1+"px";
}
//如果鼠標(biāo)下邊放不下菜單,就把top的值的改了
if(evt.pageY+100>clientHeight+scrollTop){
var top1 = (evt.pageY-100);
menu.style.top = top1+"px";
}
menu.style.display = "block";
},
// 選擇要用的常用語(yǔ)
selectPhrase(item) {
// if (item.content === '暫無(wú)慣用語(yǔ)') return
this.ruleForm.note1 = item.content
this.isPersoncontextMenus = false
},
// showNo(){
// console.log('showNo')
// console.log('showNossssssssssssssss')
// let menu = this.$refs.msgRightMenu
// menu.style.display = "none";
// },
focus () {
this.isPersoncontextMenus = true
let menu = this.$refs.msgRightMenu
menu.style.display = "block";
},
blur () {
this.isPersoncontextMenus = false
let menu = this.$refs.msgRightMenu
menu.style.display = "none";
},
如果加完右鍵功能,發(fā)現(xiàn)該輸入框或者的內(nèi)容無(wú)法修改,也無(wú)法輸入。
出現(xiàn)該問(wèn)題的原因是
vue頁(yè)面進(jìn)行數(shù)據(jù)渲染時(shí),層次嵌套或者多重?cái)?shù)據(jù)綁定導(dǎo)致該組件信息框數(shù)據(jù)不能被Vue實(shí)時(shí)監(jiān)聽(tīng)到,以此出現(xiàn)了數(shù)據(jù)發(fā)生改變但頁(yè)面上更新或刪除對(duì)應(yīng)信息框的數(shù)據(jù)毫無(wú)反應(yīng)的現(xiàn)象,此時(shí)需要強(qiáng)制更新,重新渲染。
可以在輸入框添加@input=“forceInput()”,然后在methods中添加方法:
forceInput() {
this.$forceUpdate();
},
forceUpdate用來(lái)強(qiáng)制渲染,避免data中對(duì)象層次太深,Vue框架不自動(dòng)渲染的情況。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue?CLI項(xiàng)目遷移eslint報(bào)錯(cuò)解決辦法及最佳實(shí)踐
ESLint是一個(gè)ECMAScript/JavaScript語(yǔ)法規(guī)則和代碼風(fēng)格的檢查工具,它的目標(biāo)是保證代碼的一致性和避免錯(cuò)誤,這篇文章主要介紹了Vue?CLI項(xiàng)目遷移eslint報(bào)錯(cuò)解決辦法及最佳實(shí)踐的相關(guān)資料,需要的朋友可以參考下2025-09-09
Vue Echarts實(shí)現(xiàn)柱形圖從右向左滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Vue如何利用Echarts實(shí)現(xiàn)柱形圖從右向左滾動(dòng)的效果,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05
詳解Vue-cli 創(chuàng)建的項(xiàng)目如何跨域請(qǐng)求
本篇文章主要介紹了詳解Vue-cli 創(chuàng)建的項(xiàng)目如何跨域請(qǐng)求 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
vue實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
Vue在echarts?tooltip中添加點(diǎn)擊事件案例詳解
本文主要介紹了Vue項(xiàng)目中在echarts?tooltip添加點(diǎn)擊事件的案例詳解,代碼具有一定的價(jià)值,感興趣的小伙伴可以來(lái)學(xué)習(xí)一下2021-11-11
Vue3+Swiper.js實(shí)現(xiàn)無(wú)縫輪播圖組件的最佳實(shí)踐
vue是一款流行的前端框架,它提供了一系列的工具和組件,使得開(kāi)發(fā)者可以更加便捷地創(chuàng)建交互式的Web應(yīng)用程序,輪播圖是Web應(yīng)用程序中常見(jiàn)的一種交互式組件,這篇文章主要介紹了Vue3+Swiper.js實(shí)現(xiàn)無(wú)縫輪播圖組件的最佳實(shí)踐,需要的朋友可以參考下2026-04-04

