javascript - 如何使下拉菜单扩展到右侧而不是向下?
问题描述
对于这个带有下拉菜单的侧边栏示例:
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sidenav_dropdown
在我的情况下,下拉菜单对用户不友好,因为有很多项目,所以我认为向下扩展下拉菜单不是一个好主意。我想像这样扩展并指向右边:
我该怎么做?提前致谢!
解决方案
没什么,只是添加一个列表并使其固定,然后调整边距
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {
font-family: "Lato", sans-serif;
}
/* Fixed sidenav, full height */
.sidenav {
height: 100%;
width: 200px;
position: fixed;
z-index: 2221;
top: 0;
left: 0;
background-color: #111;
padding-top: 20px;
}
/* Style the sidenav links and the dropdown button */
.sidenav a, .dropdown-btn {
padding: 6px 8px 6px 16px;
text-decoration: none;
font-size: 20px;
color: #818181;
display: block;
border: none;
background: none;
width: 100%;
text-align: left;
cursor: pointer;
outline: none;
}
/* On mouse-over */
.sidenav a:hover, .dropdown-btn:hover {
color: #f1f1f1;
}
/* Main content */
.main {
margin-left: 200px; /* Same as the width of the sidenav */
font-size: 20px; /* Increased text to enable scrolling */
padding: 0px 10px;
}
/* Add an active class to the active dropdown button */
.active {
background-color: green;
color: white;
}
/* Dropdown container (hidden by default). Optional: add a lighter background color and some left padding to change the design of the dropdown content */
.dropdown-container {
display: none;
background-color: #262626;
padding-left: 8px;
}
/* Optional: Style the caret down icon */
.fa-caret-down {
float: right;
padding-right: 8px;
}
/* Some media queries for responsiveness */
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
.dropdown-container ul {
position: fixed;
margin-left:192px;
background-color: #111;
list-style: none;
margin-top: -36px;
padding: 0;
}
.dropdown-container ul {
width: 150px;
}
</style>
</head>
<body>
<div class="sidenav">
<a href="#about">About</a>
<a href="#services">Services</a>
<a href="#clients">Clients</a>
<a href="#contact">Contact</a>
<button class="dropdown-btn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-container">
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
</ul>
</div>
<a href="#contact">Search</a>
</div>
<div class="main">
<h2>Sidebar Dropdown</h2>
<p>Click on the dropdown button to open the dropdown menu inside the side navigation.</p>
<p>This sidebar is of full height (100%) and always shown.</p>
<p>Some random text..</p>
</div>
<script>
/* Loop through all dropdown buttons to toggle between hiding and showing its dropdown content - This allows the user to have multiple dropdowns without any conflict */
var dropdown = document.getElementsByClassName("dropdown-btn");
var i;
for (i = 0; i < dropdown.length; i++) {
dropdown[i].addEventListener("click", function() {
this.classList.toggle("active");
var dropdownContent = this.nextElementSibling;
if (dropdownContent.style.display === "block") {
dropdownContent.style.display = "none";
} else {
dropdownContent.style.display = "block";
}
});
}
</script>
</body>
</html>
推荐阅读
- java - 如何使用 MusicKit SDK for Android 播放歌曲
- angular - 形式:手动触发valueChange
- android - 将现有 AWS Amplify 项目集成到 Android 项目中
- java - 将改造原始响应作为 json 对象返回而不转换为任何特定类型的对象?
- typescript - 无法使方法重载与歧视性联合一起工作。怎么了?
- http - HTTP-header 中的 User-Agent 包含三种不同的浏览器,谁是对的?
- android - 如果应用程序被滑动杀死,如何停止所有通知和警报?
- python - math.gcd() 与欧几里得算法
- c++ - 如何使用 C/C++ 获取硬盘的运行时间
- ioredis - redis关闭后一直重连?