我已经使用 Javascript 和 CSS 的组合制作了一个下拉菜单,效果很好,但我希望在显示和隐藏菜单时有一个过渡效果,最好是在显示菜单时有一个下拉菜单,以及隐藏时的俯卧撑......我如何整合它?谢谢。
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
.dropbtn {
width: 80px;
padding: 16px;
font-size: 16px;
border: none;
cursor: pointer;
opacity: 0.95;
visibility: hidden;
display: block;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
}
.dropbtn:hover,
.dropbtn:focus {
opacity: 1;
}
.dropdown {
position: relative;
display: inline-block;
width: 100%;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 100%;
margin-top: 5px;
overflow: auto;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: center;
font-family: Century Gothic, Calibri, Cambria, sans-serif;
}
.dropdown a:hover {
background-color: #f1f1f1
}
.show {
display: block;
}
<div class="dropdown">
<img onclick="myFunction()" src="../images/logotestme.png" class="dropbtn" </img>
<div id="myDropdown" class="dropdown-content">
<a href="http://www.coopertimewell.com">Home</a>
<a href="http://www.coopertimewell.com/portfolio">Portfolio</a>
<a href="http://www.coopertimewell.com/contact">Contact</a>
<a href="http://www.coopertimewell.com/downloads">Downloads</a>
</div>
</div>
最佳答案
不是通过添加删除显示类来切换显示属性,
您可以根据您想要实现的目标,使用 css transition 为目标元素的高度或顶部或变换属性设置动画
这是一个链接,指向某人制作的示例:
http://codepen.io/shshaw/pen/gsFch
来自该链接的代码示例:
.sub-menu-parent {
position: relative;
}
.sub-menu {
visibility: hidden;
/* hides sub-menu */
opacity: 0;
position: absolute;
top: 100%;
left: 0;
width: 100%;
transform: translateY(-2em);
z-index: -1;
transition: all 0.3s ease-in-out 0s, visibility 0s linear 0.3s, z-index 0s linear 0.01s;
}
.sub-menu-parent:hover .sub-menu {
visibility: visible;
/* shows sub-menu */
opacity: 1;
z-index: 1;
transform: translateY(0%);
transition-delay: 0s, 0s, 0.3s;
/* this removes the transition delay so the menu will be visible while the other styles transition */
}
/* presentational */
body {
padding: 2%;
font: 18px/1.4 sans-serif;
}
nav a {
color: #E00;
display: block;
padding: 0.5em 1em;
text-decoration: none;
}
nav a:hover {
color: #F55;
}
nav ul,
nav ul li {
list-style-type: none;
padding: 0;
margin: 0;
}
nav > ul {
background: #EEE;
text-align: center;
}
nav > ul > li {
display: inline-block;
border-left: solid 1px #aaa;
}
nav > ul > li:first-child {
border-left: none;
}
.sub-menu {
background: #DDD;
}
关于javascript - 如何在 Javascript 中切换 "show"时添加下推/上推效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38070558/