vue中實(shí)現(xiàn)點(diǎn)擊空白區(qū)域關(guān)閉彈窗的兩種方法
首頁在外層容器里面取一個(gè)名字為main,即ref='main',當(dāng)bankSwitch為true的時(shí)候,彈窗出現(xiàn)
<div ref='main'><div v-if='bankSwitch == true'>你好我是彈窗里面的內(nèi)容部分 </div></div>
所觸發(fā)的事件如下:
首頁,先在全局創(chuàng)建一個(gè)點(diǎn)擊事件:bodyCloseMenus
事件作用:當(dāng)點(diǎn)擊main容器的時(shí)候(this.refs.main && !this.refs.main.contains(e.target)),并且彈窗出現(xiàn)的時(shí)候(self.bankSwitch == true),點(diǎn)擊空白區(qū)域?qū)棿瓣P(guān)閉(self.bankSwitch = false)
最后,在頁面注銷前,將點(diǎn)擊事件給移除
mounted() { document.addEventListener('click', this.bodyCloseMenus); }, methods:{ bodyCloseMenus(e) { let self = this; if (this.$refs.main && !this.$refs.main.contains(e.target)) { if (self.bankSwitch == true){ self.bankSwitch = false; } } },beforeDestroy() { document.removeEventListener('click', this.bodyCloseMenus); },2.第二種做法
首頁在外層容器里面定義一個(gè)阻止冒泡事件,即@click.stop,當(dāng)bankSwitch為true的時(shí)候,彈窗出現(xiàn)
<div @click.stop><div v-if='bankSwitch == true'>你好我是彈窗里面的內(nèi)容部分 </div></div>
所觸發(fā)的事件如下:
首頁,先在全局創(chuàng)建一個(gè)點(diǎn)擊事件:bodyCloseMenus
事件作用:當(dāng)彈窗出現(xiàn)的時(shí)候(self.bankSwitch == true),點(diǎn)擊空白區(qū)域?qū)棿瓣P(guān)閉(self.bankSwitch = false)
最后,在頁面注銷前,將點(diǎn)擊事件給移除
mounted() { document.addEventListener('click', this.bodyCloseMenus); }, methods:{ bodyCloseMenus(e) { let self = this; if (self.bankSwitch == true){ self.bankSwitch = false; } },beforeDestroy() { document.removeEventListener('click', this.bodyCloseMenus); },
以上就是vue中實(shí)現(xiàn)點(diǎn)擊空白區(qū)域關(guān)閉彈窗的兩種方法的詳細(xì)內(nèi)容,更多關(guān)于vue 點(diǎn)擊空白區(qū)域關(guān)閉彈窗的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. .NET中l(wèi)ambda表達(dá)式合并問題及解決方法2. JSP數(shù)據(jù)交互實(shí)現(xiàn)過程解析3. 淺談python出錯(cuò)時(shí)traceback的解讀4. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法5. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼6. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解7. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無效問題8. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向9. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解10. Nginx+php配置文件及原理解析
