Vue中實現(xiàn)動態(tài)右鍵菜單的示例代碼
在前端開發(fā)中,自定義右鍵菜單是一種常見的交互方式,它能夠為用戶提供更多的功能選項。在本文中,將探討如何在 Vue 中實現(xiàn)一個動態(tài)右鍵菜單,該菜單能夠根據(jù)用戶點擊位置動態(tài)調整其顯示位置,確保菜單始終在瀏覽器窗口的可視區(qū)域內。
實現(xiàn)目標
- 右鍵點擊頁面時顯示自定義菜單。
- 菜單根據(jù)點擊位置動態(tài)定位。
- 確保菜單不會超出瀏覽器窗口的可視區(qū)域。(超出窗口頂部或者底部優(yōu)化)
實現(xiàn)步驟
1. 創(chuàng)建 Vue 組件模板
首先,編寫 Vue 組件的模板部分:
<template>
<div v-if="visible">
<a-menu class="contextmenu" :style="style" @click="handleClick">
<a-menu-item v-for="item in list" :key="item.key">
<span>{{ item.text }}</span>
</a-menu-item>
</a-menu>
</div>
</template>
在這個模板中,使用 v-if 指令控制菜單的顯示與隱藏,并使用 :style 綁定菜單的動態(tài)樣式。
2. 編寫組件腳本
接下來是組件的腳本部分:
<script>
export default {
name: "ContextMenu",
props: {
visible: {
type: Boolean,
default: false,
},
list: {
type: Array,
required: true,
default: () => [],
},
},
data() {
return {
left: 0,
top: 0,
target: null,
};
},
computed: {
style() {
return {
left: this.left + "px",
top: this.top + "px",
};
},
},
created() {
const clickHandler = () => this.closeMenu();
const contextMenuHandler = (e) => {
e.preventDefault();
this.setPosition(e);
};
window.addEventListener("click", clickHandler);
window.addEventListener("contextmenu", contextMenuHandler);
this.$emit("hook:beforeDestroy", () => {
window.removeEventListener("click", clickHandler);
window.removeEventListener("contextmenu", contextMenuHandler);
});
},
methods: {
closeMenu() {
this.$emit("update:visible", false);
},
setPosition(e) {
// 獲取菜單的寬高
const menu = document.querySelector(".contextmenu");
const menuHeight = menu.offsetHeight;
// 獲取窗口的可視高度
const windowHeight = window.innerHeight;
this.left = e.clientX;
// 計算菜單的上邊位置
if (e.clientY + menuHeight > windowHeight) {
const top = e.clientY - menuHeight;
if (top < 0) {
this.top = 0; // 確保菜單不會超出頂部
} else {
// 如果菜單的底部超出了窗口的底部
this.top = top;
}
} else {
// 如果菜單的底部沒有超出窗口的底部
this.top = e.clientY;
}
this.target = e.target;
},
handleClick({ key }) {
const _component = this.list.filter((item) => item.key === key)[0]
.component;
if (_component) {
this.$emit("contextMenuClick", _component, key);
}
this.closeMenu();
},
},
};
</script>
詳細解釋
setPosition 方法
setPosition 方法用于根據(jù)用戶點擊的位置動態(tài)調整菜單的位置,確保菜單始終在可視區(qū)域內。
setPosition(e) {
// 獲取菜單的寬高
const menu = document.querySelector(".contextmenu");
const menuHeight = menu.offsetHeight;
// 獲取窗口的可視高度
const windowHeight = window.innerHeight;
this.left = e.clientX;
// 計算菜單的上邊位置
if (e.clientY + menuHeight > windowHeight) {
const top = e.clientY - menuHeight;
if (top < 0) {
this.top = 0; // 確保菜單不會超出頂部
} else {
// 如果菜單的底部超出了窗口的底部
this.top = top;
}
} else {
// 如果菜單的底部沒有超出窗口的底部
this.top = e.clientY;
}
this.target = e.target;
}
- 相加:通過
e.clientY + menuHeight計算菜單底部的位置,如果大于windowHeight,則表示菜單超出了窗口底部,需要調整位置。 - 相減:通過
e.clientY - menuHeight將菜單位置調整到鼠標點擊位置的上方,確保菜單不會超出窗口底部。 - clientY:鼠標點擊位置的垂直坐標,相對于視口。
- offsetHeight:菜單元素的高度,包括內容的高度、內邊距和邊框。
事件處理
在 created 生命周期鉤子中,添加了 click 和 contextmenu 事件監(jiān)聽器。
created() {
const clickHandler = () => this.closeMenu();
const contextMenuHandler = (e) => {
this.setPosition(e);
};
window.addEventListener("click", clickHandler);
window.addEventListener("contextmenu", contextMenuHandler);
this.$emit("hook:beforeDestroy", () => {
window.removeEventListener("click", clickHandler);
window.removeEventListener("contextmenu", contextMenuHandler);
});
}
- clickHandler:點擊頁面時,關閉菜單。
- contextMenuHandler:右鍵點擊時,阻止默認的右鍵菜單行為,并根據(jù)點擊位置設置菜單的位置。
樣式部分
<style lang="scss" scoped>
.contextmenu {
position: fixed;
z-index: 1000;
border-radius: 4px;
border: 1px lightgrey solid;
box-shadow: 4px 4px 10px lightgrey !important;
}
</style>
總結
通過以上代碼,實現(xiàn)了一個動態(tài)右鍵菜單。這個菜單能夠根據(jù)用戶的點擊位置動態(tài)調整其顯示位置,確保菜單始終在瀏覽器窗口的可視區(qū)域內。這樣的實現(xiàn)可以提升用戶體驗,使應用更加友好和易用。
到此這篇關于Vue中實現(xiàn)動態(tài)右鍵菜單的示例代碼的文章就介紹到這了,更多相關Vue 動態(tài)右鍵菜單內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue過渡效果之CSS過渡詳解(結合transition,animation,animate.css)
Vue 在插入、更新或者移除 DOM 時,提供多種不同方式的應用過渡效果。本文將從CSS過渡transition、CSS動畫animation及配合使用第三方CSS動畫庫(如animate.css)這三方面來詳細介紹Vue過渡效果之CSS過渡2020-02-02
el-select選擇器組件下拉框增加自定義按鈕的實現(xiàn)
本文主要介紹了el-select選擇器組件下拉框增加自定義按鈕的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-07-07
VueX瀏覽器刷新如何實現(xiàn)保存數(shù)據(jù)
這篇文章主要介紹了VueX瀏覽器刷新如何實現(xiàn)保存數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
Vue+webpack+Element 兼容問題總結(小結)
這篇文章主要介紹了Vue+webpack+Element 兼容問題總結(小結),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08

