javascript - 如何设计具有下拉效果的下拉菜单?
问题描述
我正在尝试使用动画制作下拉菜单。
到目前为止,我完成了一个常见的下拉菜单,如下例所示。
input {
display: none;
}
label {
cursor: pointer;
display: inline-block;
padding: 10px 20px;
border: 1px solid;
border-radius: 4px;
background: transparent;
color: #000;
z-index: 100;
}
.menu {
-webkit-transition: all .3s ease;
height: 0;
overflow: hidden;
width: 200px;
background: red;
height: 100px;
z-index: -1;
/* transform: translateY(-100%); */
/* I want it drawn down behind the button */
}
input:checked+.menu {
transform: translateY(50%);
}
<label for="check">Click me</label>
<input id="check" type="checkbox">
<div class="menu">
<p>I am dropdown menu</p>
</div>
然而,我的目的是设计一个可以在后面滑下(下拉)的菜单
按钮。我已将 z-index 设置为这些元素但不起作用。
此外,我无法将背景颜色设置为切换按钮,我需要保持透明。
有什么建议么?
解决方案
检查以下css更改使菜单元素绝对和顶部填充
input {
display: none;
}
label {
cursor: pointer;
display: inline-block;
padding: 10px 20px;
border: 1px solid;
border-radius: 4px;
background: transparent;
color: #000;
z-index: 100;
}
.menu {
-webkit-transition: all .3s ease;
height: 0;
overflow: hidden;
width: 200px;
background: #ffffff;
height: 100px;
z-index: -1;
position:absolute;
padding-top:35px;
border:1px solid #000000;
border-radius: 4px;
}
input:checked+.menu {
transform: translateY(-40px);
}
<label for="check">Click me</label>
<input id="check" type="checkbox">
<div class="menu">
<p>I am dropdown menu</p>
</div>
推荐阅读
- ffmpeg - FFmpeg,从 MP3 转换为 Opus (OGG) 时保留专辑封面
- python - 使用日期时间设置列时未获得默认值
- vim - 如果文件标题为“test.cpp”,则使 vim 在关闭文件后删除文件
- postgresql - “创建用户密码”与“使用密码创建用户”
- flutter - 使用 Positioned 小部件时,Argon 按钮仅在底部有效
- html - 使元素填充但不扩展
- karate - 空手道:场景大纲中的注释不显示在 HTML 报告中
- node.js - NodeMailer:Fastcomet 邮件中的 getaddrinfo EAI_FAIL 错误
- java - 解析类似于 JSON 的数据格式
- swift - Swift Combine:订阅时只获得第一个价值?