javascript - 如何在下拉容器中使用过渡,使其缓慢显示,直到达到我想要的大小?
问题描述
我试图在我的下拉列表中使用过渡,所以当我点击它时,它会出现增加高度 2 秒,直到它达到我想要的高度(以同样的方式消失它),但是当我在我的dropdown-container 它不起作用,它立即向我显示全高度的下拉列表。有人可以帮助我吗?
var x=0;
/* 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 */
document.getElementById("btn-show-products").addEventListener("click", clickcont);
function clickcont(){
if(x==0){
openNav();
x=1;
}
else{
closeNav();
x=0;
}
}
function openNav() {
document.getElementById("contenedor").style.height = "auto";
}
function closeNav() {
document.getElementById("contenedor").style.height = "0";
}
body {
font-family: "Lato", sans-serif;
}
/* Fixed sidenav, full height */
.sidenav {
height: 100%;
width: 200px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
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 {
background-color: #262626;
padding-left: 8px;
height:0px;
overflow-y:hidden;
overflow-x:hidden;
transition: 2s;
}
/* 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;}
}
<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" id="btn-show-products">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-container" id="contenedor">
<a href="#">Link 1</a>
<a href="#">Link 2</a>
<a href="#">Link 3</a>
</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>
解决方案
它不起作用,因为转换到 auto 高度时转换不起作用。尝试用某种类型的单位指定高度,你应该会很好。例如,这将起作用:
function openNav() {
document.getElementById("contenedor").style.height = "100px";
}
祝你好运!
推荐阅读
- c# - Xamarin 显示警报未显示
- c# - 报告设计 acuamtica
- excel - 在条形图excel中分组数据
- google-cloud-platform - istio 的其他 api 可用吗?
- c# - 如何从 Azure Blob 存储 C# 中的文件 URL 获取容器名称
- javascript - 将数组值与对象进行比较
- react-native - 纱线还是 npm?用于在 react-native 中安装依赖项
- angular - 防止来自嵌入中重复组件的双重 api 调用(仅显示其中一个)
- docker - 如何在 elk filebeat 中限制 *-json.log
- php - fopen 无法在没有错误的服务器上运行