gpt4 book ai didi

javascript - 根据屏幕大小,将菜单项合并到下拉列表中。没有jquery

转载 作者:太空宇宙 更新时间:2023-11-04 01:44:58 24 4
gpt4 key购买 nike

例如我有包含元素的菜单:

1) enter image description here

当我更改浏览器的大小时,最后两个元素合并了两个 1 下拉列表

2) enter image description here

3) enter image description here

我需要这样的东西。我有Contacts_France | Contacts_Germany 并且当尺寸减小时,它们应该合并到一个名称为 Contacts

的下拉列表中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MAx-nax</title>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
</head>
<body>


<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Project name</a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact_france">Contact_France</a></li>
<li><a href="#contact_germany">Contact_Germany</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>

</body>
</html>

最佳答案

<罢工>A JSfiddle易于调整大小的能力。现在,在调整大小事件期间,更多菜单中有一些疯狂的切换。我不知道为什么会这样......

编辑:可以用 @Santiago Rebella's 解决解决方案在这里。

JSfiddle

var tele = document.getElementById('teleporter'),
rec = document.getElementById('receiver');

window.onresize = resize;
resize();

function resize() {
const rChildren = rec.children;
let numW = 0;

[...rChildren].forEach(item => {
item.outHTML = '';
tele.appendChild(item);
})

const teleW = tele.offsetWidth,
tChildren = tele.children;

[...tChildren].forEach(item => {
numW += item.offsetWidth;

if (numW > teleW) {
item.outHTML = '';
rec.appendChild(item);
}
});
}
#teleporter {
height: 20px;
overflow: hidden;
box-sizing: border-box;
padding: 0;
}

li {
float: left;
padding: 0 10px;
box-sizing: border-box;
list-style: none;
}
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div id="navbar" class="collapse navbar-collapse">
<ul id="teleporter">
<li>List item 0</li>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
<li>List item 5</li>
<li>List item 6</li>
<li>List item 7</li>
<li>List item 8</li>
<li>List item 9</li>
<li>List item 10</li>
<li>List item 11</li>
<li>List item 12</li>
<li>List item 13</li>
</ul>
<div>More:
<ul id="receiver"></ul>
</div>
</div>
</div>
</nav>

关于javascript - 根据屏幕大小,将菜单项合并到下拉列表中。没有jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44741560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com