vue右鍵菜單的簡單封裝
更新時間:2022年04月08日 14:22:41 作者:橡皮擦不掉的
這篇文章主要為大家詳細介紹了vue右鍵菜單的簡單封裝,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)右鍵菜單封裝的具體代碼,供大家參考,具體內(nèi)容如下
封裝一個簡單的右鍵菜單,要求右鍵處出現(xiàn)菜單,點擊除了菜單部分可以關(guān)閉菜單。
組件
<template> ? <div class="ContextMenu" @click="close" v-show="show"> ? ? <ul class="menuMain" ref="menuMain" :style="{ top: y, left: x }"> ? ? ? <slot></slot> ? ? </ul> ? </div> </template> <script> export default { ? name: "ContextMenu", ? mounted() { ? ? document.addEventListener("contextmenu", this.contextClick); ? }, ? data() { ? ? return { ? ? ? x: "0px", ? ? ? y: "0px", ? ? ? show: false ? ? }; ? }, ? methods: { ? ? //右鍵事件 ? ? contextClick(e) { ? ? ? //阻止默認事件 ? ? ? e.preventDefault(); ? ? ? this.show = true; ? ? ? this.x = e.clientX + "px"; ? ? ? this.y = e.clientY + "px"; ? ? }, ? ? close(e) { ? ? ? //判斷點擊區(qū)域是否是menuMain的子元素 如果不是則關(guān)閉菜單 ? ? ? if (!this.$refs.menuMain.contains(e.target)) { ? ? ? ? this.show = false; ? ? ? } ? ? } ? } }; </script> <style lang="less" scoped> .ContextMenu { ? position: fixed; ? width: 100vw; ? height: 100vh; ? top: 0; ? left: 0; ? .menuMain { ? ? position: fixed; ? ? z-index: 100000; ? ? list-style: none; ? ? border-radius: 10px; ? ? padding: 0; ? ? margin: 0; ? ? background-color: #f5f5f5; ? ? overflow: hidden; ? ? li{ ? ? ? padding: 20px; ? ? ? cursor: pointer; ? ? ? &:hover{ ? ? ? ? background-color: #bdbdbd; ? ? ? } ? ? } ? } } </style>
使用
<context-menu> ? ? <li>hello</li> ? ? <li>hello</li> </context-menu>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
antd upload上傳組件如何獲取服務(wù)端返回數(shù)據(jù)
這篇文章主要介紹了antd upload上傳組件如何獲取服務(wù)端返回數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02vue-cli5.0?webpack?采用?copy-webpack-plugin?打包復(fù)制文件的方法
今天就好好說說vue-cli5.0種使用copy-webpack-plugin插件該如何配置的問題。這里我們安裝的 copy-webpack-plugin 的版本是 ^11.0.0,感興趣的朋友一起看看吧2022-06-06Vue3 響應(yīng)式 API 及 reactive 和 ref&
響應(yīng)式是一種允許以聲明式的方式去適應(yīng)變化的編程范例,這篇文章主要介紹了關(guān)于Vue3響應(yīng)式API及reactive和ref的用法,需要的朋友可以參考下2023-06-06