gpt4 book ai didi

javascript - 使用 JS 使导航栏折叠

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

目前我的导航栏随着页面大小缩小,使用@media screen。这很好用,但是当页面很小时,我希望导航栏折叠成一个垂直下拉菜单,需要单击才能打开。

由于我的情况,我不能使用 bootstrap,只能使用 html/css 和 js。

关于 jsfiddle 的例子

最佳答案

首先使用@media 隐藏导航栏元素并将元素更改为 ListView

@media screen and (max-width: 850px){
//replace max width with your width

ul li {
display: block;
}
ul {
display: none;
}


}

Showmenu 函数切换导航栏元素的可见性

function showmenu()
{
var x = document.getElementById('myUL');
if (x.style.display == 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}

同时添加一个按钮来触发功能

<!DOCTYPE html>
<html lang="en">

<head>
<button onclick = 'showmenu();'>click me to see menu</button>
<ul id='myUL'>
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
</head>


</html>

希望对你有帮助

关于javascript - 使用 JS 使导航栏折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45998699/

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