vue右鍵菜單的簡(jiǎn)單封裝
本文實(shí)例為大家分享了vue實(shí)現(xiàn)右鍵菜單封裝的具體代碼,供大家參考,具體內(nèi)容如下
封裝一個(gè)簡(jiǎn)單的右鍵菜單,要求右鍵處出現(xiàn)菜單,點(diǎ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) { ? ? ? //阻止默認(rèn)事件 ? ? ? e.preventDefault(); ? ? ? this.show = true; ? ? ? this.x = e.clientX + "px"; ? ? ? this.y = e.clientY + "px"; ? ? }, ? ? close(e) { ? ? ? //判斷點(diǎn)擊區(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
antd upload上傳組件如何獲取服務(wù)端返回?cái)?shù)據(jù)
這篇文章主要介紹了antd upload上傳組件如何獲取服務(wù)端返回?cái)?shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02vue-cli5.0?webpack?采用?copy-webpack-plugin?打包復(fù)制文件的方法
今天就好好說說vue-cli5.0種使用copy-webpack-plugin插件該如何配置的問題。這里我們安裝的 copy-webpack-plugin 的版本是 ^11.0.0,感興趣的朋友一起看看吧2022-06-06Vue.js 實(shí)現(xiàn)tab切換并變色操作講解
這篇文章主要介紹了Vue.js 實(shí)現(xiàn)tab切換并變色操作講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09Vue3 響應(yīng)式 API 及 reactive 和 ref&
響應(yīng)式是一種允許以聲明式的方式去適應(yīng)變化的編程范例,這篇文章主要介紹了關(guān)于Vue3響應(yīng)式API及reactive和ref的用法,需要的朋友可以參考下2023-06-06Vue.js計(jì)算屬性computed與watch(5)
這篇文章主要為大家詳細(xì)介紹了Vue.js計(jì)算屬性computed與watch,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12