Skip to content

Commit

Permalink
fix: 修复侧边栏二级菜单定位不准确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsonliu committed Apr 24, 2023
1 parent f0973d6 commit 8bc6c3b
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/toolbars/MenuBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ function getPosition(targetDom, positionModel = 'absolute') {
}
// 侧边栏按钮做个特殊处理
if (positionModel === 'sidebar') {
const parent = MenuBase.getTargetParentByButton(targetDom);
return {
left: targetDom.parentElement.offsetLeft - 130 + pos.width,
left: parent.offsetLeft - 130 + pos.width,
top: targetDom.offsetTop + pos.height / 2,
width: pos.width,
height: pos.height,
Expand Down Expand Up @@ -328,8 +329,9 @@ export default class MenuBase {
* 获取当前菜单的位置
*/
getMenuPosition() {
const isFromSidebar = /cherry-sidebar/.test(this.dom.parentElement.className);
if (/cherry-bubble/.test(this.dom.parentElement.className)) {
const parent = MenuBase.getTargetParentByButton(this.dom);
const isFromSidebar = /cherry-sidebar/.test(parent.className);
if (/cherry-bubble/.test(parent.className)) {
this.positionModel = 'fixed';
} else if (isFromSidebar) {
this.positionModel = 'sidebar';
Expand All @@ -338,4 +340,17 @@ export default class MenuBase {
}
return getPosition(this.dom, this.positionModel);
}

/**
* 根据按钮获取按钮的父元素,这里父元素要绕过toolbar-(left|right)那一层
* @param {HTMLElement} dom 按钮元素
* @returns {HTMLElement} 父元素
*/
static getTargetParentByButton(dom) {
let parent = dom.parentElement;
if (/toolbar-(left|right)/.test(parent.className)) {
parent = parent.parentElement;
}
return parent;
}
}

0 comments on commit 8bc6c3b

Please sign in to comment.