在父 DIV 之外打开自定义上下文菜单
Open Custom Context Menu outside of parent DIV
class Menu {
constructor() {
this.menu = document.querySelector('.menu');
this.ellipsis = document.querySelector('.fa.fa-ellipsis-v');
this.callEvent();
}
callEvent() {
this.ellipsis.addEventListener('click', () => {
$('.menu').toggleClass('show');
});
}
}
const menu = new Menu();
#parent-div{
border: 2px solid green;
}
.menu {
display: none;
}
.show {
display: flex;
border: 2px solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div id="parent-div">
<table>
<tbody>
<i class="fa fa-ellipsis-v"></i>
<div class="menu">
<div id="context-menu">
<a href="#" type="button">Item 1</a>
<a href="#" type="button">Item 2</a>
<a href="#" type="button">Item 3</a>
<a href="#" type="button">Item 4</a>
</div>
</div>
<tbody>
</table>
</div>
我创建了一个自定义上下文菜单,并从菜单项部分合并了 Bootstraps 下拉菜单 类。现在我的上下文菜单在单击时正确打开和关闭,问题是上下文菜单正在更改它所在的父级 div 的大小(可以理解)。是否可以在父级 div 之外打开上下文菜单 div,最好稍微偏右一点?
HTML
<div>
<table id="my-table">
<tbody>
<tr>
<td class="column-1"><h4></h4>
<h5>
<a href="">Link 1</a>
</h5>
<p>September 17, 2020 - September 24, 2020</p>
</td>
<td class="column-2">
<i class="fa fa-ellipsis-v pull-right" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
</i>
<div class="menu dropdown show">
<div class="btn-group-vertical dropdown-menu" role="group" aria-labelledby="dropdownMenu2" style="">
<a href="#" type="button"
class="btn dropdown-item">Item 1</a>
<a href="#" type="button"
class="btn dropdown-item">Item 2</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
从上方提取上下文菜单 html。这是我想在父菜单之外显示的菜单 div:
<div class="menu dropdown show">
<div class="btn-group-vertical dropdown-menu"role="group" aria-labelledby="dropdownMenu2">
<a href="#" type="button" class="btn dropdown-item">Item 1</a>
<a href="#" type="button" class="btn dropdown-item">Item 2</a>
</div>
</div>
Javascript
class Menu {
constructor() {
this.menu = document.querySelector('.menu');
this.ellipsis = document.querySelector('.fa.fa-ellipsis-v');
this.callEvent();
}
callEvent() {
this.ellipsis.addEventListener('click',this.showMenu.bind(this));
}
showMenu(e){
e.preventDefault();
$('.menu').toggleClass('show');
}
}
var menu = new Menu();
CSS
.menu {
display: none;
position: relative;
}
.show {
display: block;
}
您只需使用 position: absolute
定位您的菜单,并将父项的位置设置为 position: relative
。这将从文档流中删除菜单,并允许您使用 top
、bottom
、left
和 right
css 属性相对于父项定位它。
我通过添加以下内容编辑了您的代码段 CSS
#parent-div{
border: 2px solid green;
position: relative; /* accommodates absolutely positioned children */
}
.show {
display: flex;
border: 2px solid;
width: 80px; /* set menu width */
height: 60px; /* set menu height */
position: absolute; /* position absolute relative to parent */
bottom: -90px; /* position the menu relative to parents bottom edge*/
}
const button = document.getElementById('toggle-absolute');
button.addEventListener('click', () => {
const menu = document.getElementById('menu');
menu.classList.toggle('absolute');
});
.container {
background: aquamarine;
position: relative;
height: 50vh;
width: 50vw;
}
#menu {
padding: 8px;
background-color: tomato;
width: 80px;
height: 60px;
}
.absolute {
position: absolute;
top: 50%;
right: 10%;
}
<div class="container">
<div id="menu">
<p>I'm a menu</p>
</div>
<button type='button' id='toggle-absolute'>Toggle Absolute</button>
</div>
class Menu {
constructor() {
this.menu = document.querySelector('.menu');
this.ellipsis = document.querySelector('.fa.fa-ellipsis-v');
this.callEvent();
}
callEvent() {
this.ellipsis.addEventListener('click', () => {
$('.menu').toggleClass('show');
});
}
}
const menu = new Menu();
#parent-div{
border: 2px solid green;
position: relative;
}
.menu {
display: none;
}
.show {
display: flex;
border: 2px solid;
width: 80px;
height: 60px;
position: absolute;
bottom: -90px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div id="parent-div">
<table>
<tbody>
<i class="fa fa-ellipsis-v"></i>
<div class="menu">
<div id="context-menu">
<a href="#" type="button">Item 1</a>
<a href="#" type="button">Item 2</a>
<a href="#" type="button">Item 3</a>
<a href="#" type="button">Item 4</a>
</div>
</div>
<tbody>
</table>
</div>
class Menu {
constructor() {
this.menu = document.querySelector('.menu');
this.ellipsis = document.querySelector('.fa.fa-ellipsis-v');
this.callEvent();
}
callEvent() {
this.ellipsis.addEventListener('click', () => {
$('.menu').toggleClass('show');
});
}
}
const menu = new Menu();
#parent-div{
border: 2px solid green;
}
.menu {
display: none;
}
.show {
display: flex;
border: 2px solid;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div id="parent-div">
<table>
<tbody>
<i class="fa fa-ellipsis-v"></i>
<div class="menu">
<div id="context-menu">
<a href="#" type="button">Item 1</a>
<a href="#" type="button">Item 2</a>
<a href="#" type="button">Item 3</a>
<a href="#" type="button">Item 4</a>
</div>
</div>
<tbody>
</table>
</div>
我创建了一个自定义上下文菜单,并从菜单项部分合并了 Bootstraps 下拉菜单 类。现在我的上下文菜单在单击时正确打开和关闭,问题是上下文菜单正在更改它所在的父级 div 的大小(可以理解)。是否可以在父级 div 之外打开上下文菜单 div,最好稍微偏右一点?
HTML
<div>
<table id="my-table">
<tbody>
<tr>
<td class="column-1"><h4></h4>
<h5>
<a href="">Link 1</a>
</h5>
<p>September 17, 2020 - September 24, 2020</p>
</td>
<td class="column-2">
<i class="fa fa-ellipsis-v pull-right" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
</i>
<div class="menu dropdown show">
<div class="btn-group-vertical dropdown-menu" role="group" aria-labelledby="dropdownMenu2" style="">
<a href="#" type="button"
class="btn dropdown-item">Item 1</a>
<a href="#" type="button"
class="btn dropdown-item">Item 2</a>
</div>
</div>
</td>
</tr>
</tbody>
</table>
</div>
从上方提取上下文菜单 html。这是我想在父菜单之外显示的菜单 div:
<div class="menu dropdown show">
<div class="btn-group-vertical dropdown-menu"role="group" aria-labelledby="dropdownMenu2">
<a href="#" type="button" class="btn dropdown-item">Item 1</a>
<a href="#" type="button" class="btn dropdown-item">Item 2</a>
</div>
</div>
Javascript
class Menu {
constructor() {
this.menu = document.querySelector('.menu');
this.ellipsis = document.querySelector('.fa.fa-ellipsis-v');
this.callEvent();
}
callEvent() {
this.ellipsis.addEventListener('click',this.showMenu.bind(this));
}
showMenu(e){
e.preventDefault();
$('.menu').toggleClass('show');
}
}
var menu = new Menu();
CSS
.menu {
display: none;
position: relative;
}
.show {
display: block;
}
您只需使用 position: absolute
定位您的菜单,并将父项的位置设置为 position: relative
。这将从文档流中删除菜单,并允许您使用 top
、bottom
、left
和 right
css 属性相对于父项定位它。
我通过添加以下内容编辑了您的代码段 CSS
#parent-div{
border: 2px solid green;
position: relative; /* accommodates absolutely positioned children */
}
.show {
display: flex;
border: 2px solid;
width: 80px; /* set menu width */
height: 60px; /* set menu height */
position: absolute; /* position absolute relative to parent */
bottom: -90px; /* position the menu relative to parents bottom edge*/
}
const button = document.getElementById('toggle-absolute');
button.addEventListener('click', () => {
const menu = document.getElementById('menu');
menu.classList.toggle('absolute');
});
.container {
background: aquamarine;
position: relative;
height: 50vh;
width: 50vw;
}
#menu {
padding: 8px;
background-color: tomato;
width: 80px;
height: 60px;
}
.absolute {
position: absolute;
top: 50%;
right: 10%;
}
<div class="container">
<div id="menu">
<p>I'm a menu</p>
</div>
<button type='button' id='toggle-absolute'>Toggle Absolute</button>
</div>
class Menu {
constructor() {
this.menu = document.querySelector('.menu');
this.ellipsis = document.querySelector('.fa.fa-ellipsis-v');
this.callEvent();
}
callEvent() {
this.ellipsis.addEventListener('click', () => {
$('.menu').toggleClass('show');
});
}
}
const menu = new Menu();
#parent-div{
border: 2px solid green;
position: relative;
}
.menu {
display: none;
}
.show {
display: flex;
border: 2px solid;
width: 80px;
height: 60px;
position: absolute;
bottom: -90px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div id="parent-div">
<table>
<tbody>
<i class="fa fa-ellipsis-v"></i>
<div class="menu">
<div id="context-menu">
<a href="#" type="button">Item 1</a>
<a href="#" type="button">Item 2</a>
<a href="#" type="button">Item 3</a>
<a href="#" type="button">Item 4</a>
</div>
</div>
<tbody>
</table>
</div>