使用 JavaScript 和 CSS 定位子菜单
Posted
技术标签:
【中文标题】使用 JavaScript 和 CSS 定位子菜单【英文标题】:Positioning submenu with JavaScript and CSS 【发布时间】:2020-12-29 12:43:57 【问题描述】:我怎样才能使这些框具有响应性?例如,最后一个子菜单超出边界。我不知道如何验证盒子在空间上是否可见以及如何正确移动它们。在这种情况下,我想看看如何在不可见的情况下移动第二级,或者在第二级和第三级都可见但第三级不可见时移动它们。
document.addEventListener('click', function(e)
e = e || window.event;
console.log(e);
var target = e.target || e.srcElement;
console.log(target);
if (target.parentElement.className.indexOf('has-submenu') > -1)
e.target.classList.toggle('open');
, false);
#menu
background: #343434;
color: #eee;
height: 35px;
border-bottom: 4px solid #eeeded
#menu ul,
#menu li
margin: 0 0;
padding: 0 0;
list-style: none
#menu ul
height: 35px
#menu li
float: left;
display: inline;
position: relative;
font: bold 12px Arial;
text-shadow: 0 -1px 0 #000;
border-right: 1px solid #444;
border-left: 1px solid #111;
text-transform: uppercase
#menu li:first-child
border-left: none
#menu a
display: block;
line-height: 35px;
padding: 0 14px;
text-decoration: none;
color: #eee;
#menu li:hover > a,
#menu li a:hover
background: #111
#menu input
display: none;
margin: 0 0;
padding: 0 0;
width: 80px;
height: 35px;
opacity: 0;
cursor: pointer
#menu label
font: bold 30px Arial;
display: none;
width: 35px;
height: 36px;
line-height: 36px;
text-align: center
#menu label span
font-size: 12px;
position: absolute;
left: 35px
#menu ul.menus
height: auto;
width: 180px;
background: #111;
position: absolute;
z-index: 99;
display: none;
border: 0;
#menu ul.menus li
display: block;
width: 100%;
font: 12px Arial;
text-transform: none;
#menu li:hover ul.menus
display: block
#menu a.home
background: #c00;
#menu a.prett
padding: 0 27px 0 14px
#menu a.prett::after
content: "";
width: 0;
height: 0;
border-width: 6px 5px;
border-style: solid;
border-color: #eee transparent transparent transparent;
position: absolute;
top: 15px;
right: 9px
#menu a.prett.open::after
content: "";
width: 0;
height: 0;
border-width: 6px 5px;
border-style: solid;
border-color: transparent transparent #eee transparent;
position: absolute;
top: 9px;
right: 9px
#menu ul.menus a:hover
background: #333;
#menu ul.menus .submenu
display: none;
position: absolute;
left: 180px;
background: #111;
top: 0;
width: 180px;
#menu ul.menus .submenu li
background: #111;
#menu ul.menus .has-submenu a.open ~ .submenu
display: block;
<nav>
<ul id='menu'>
<li><a class='home' href='/'>Home</a></li>
<li><a class='prett' href='#' title='Menu'>Menu</a>
<ul class='menus'>
<li class='has-submenu'><a class='prett' title='Dropdown 1'>Dropdown 1 + Sub Menu</a>
<ul class='submenu'>
<li><a href="#" title="Sub Menu">Sub Menu</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 2</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 3</a></li>
</ul>
</li>
<li><a href='#' title='Dropdown 2'>Dropdown 2</a></li>
<li><a href='#' title='Dropdown 3'>Dropdown 3</a></li>
</ul>
</li>
<li><a class='prett' href='#' title='Menu'>Menu</a>
<ul class='menus'>
<li class='has-submenu'><a class='prett' title='Dropdown 1'>Dropdown 1 + Sub Menu</a>
<ul class='submenu'>
<li><a href="#" title="Sub Menu">Sub Menu</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 2</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 3</a></li>
</ul>
</li>
<li><a href='#' title='Dropdown 2'>Dropdown 2</a></li>
<li><a href='#' title='Dropdown 3'>Dropdown 3</a></li>
</ul>
</li>
<li><a class='prett' href='#' title='Menu'>Menu</a>
<ul class='menus'>
<li class='has-submenu'><a class='prett' title='Dropdown 1'>Dropdown 1 + Sub Menu</a>
<ul class='submenu'>
<li><a href="#" title="Sub Menu">Sub Menu</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 2</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 3</a></li>
</ul>
</li>
<li><a href='#' title='Dropdown 2'>Dropdown 2</a></li>
<li><a href='#' title='Dropdown 3'>Dropdown 3</a></li>
</ul>
</li>
<li><a class='prett' href='#' title='Menu'>Menu</a>
<ul class='menus'>
<li class='has-submenu'><a class='prett' title='Dropdown 1'>Dropdown 1 + Sub Menu</a>
<ul class='submenu'>
<li><a href="#" title="Sub Menu">Sub Menu</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 2</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 3</a></li>
</ul>
</li>
<li><a href='#' title='Dropdown 2'>Dropdown 2</a></li>
<li><a href='#' title='Dropdown 3'>Dropdown 3</a></li>
</ul>
</li>
<li><a class='prett' href='#' title='Menu'>Menu</a>
<ul class='menus'>
<li class='has-submenu'><a class='prett' title='Dropdown 1'>Dropdown 1 + Sub Menu</a>
<ul class='submenu'>
<li><a href="#" title="Sub Menu">Sub Menu</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 2</a></li>
<li><a href="#" title="Sub Menu">Sub Menu 3</a></li>
</ul>
</li>
<li><a href='#' title='Dropdown 2'>Dropdown 2</a></li>
<li><a href='#' title='Dropdown 3'>Dropdown 3</a></li>
</ul>
</li>
</ul>
</nav>
例子
https://jsfiddle.net/mwb2caht/
image of submenu before fix and after
【问题讨论】:
【参考方案1】:使用这里的信息,它是关于 @media 查询的。 你可以给特定的屏幕尺寸指定属性
https://www.w3schools.com/css/css3_mediaqueries_ex.asp
编辑:您可以在浏览器窗口(在 Chrome 上)中按 F12,然后按 ctrl + shift + M 以获得响应式“测试器”
【讨论】:
实际上这不会正确工作,因为框的宽度可能不同,我正在寻找的是用 js 来确定第 2 层和第 3 层是否超出边界并将它们移动到左侧如果需要。我用屏幕截图编辑我的问题 你有解决办法了吗? 你是怎么解决的? 这是另一个同样问题的问题,有解决方案***.com/questions/11512032/… 感谢链接,它真的帮助了我以上是关于使用 JavaScript 和 CSS 定位子菜单的主要内容,如果未能解决你的问题,请参考以下文章