This website 's nav bar has a nice, smooth rollover effect that I like. As far as I can tell, it's being done in JavaScript - can such a thing be easily emulated with CSS, and if so, how would I apply it to my own website's navigation?
I've looked for CSS examples, but nothing I've found quite comes close to the cascade-like effect that this website's menu has, and adapting what I have found to my (Wordpress) site just breaks my navigation.
The slow slide back when the site's sub-menu item loses focus would also be a massive bonus. If the effects can't be emulated to the same degree of smoothness in CSS, I'm happy to be directed to a JavaScript solution/library that I won't have to pay for.
Here's the markup for my site's navigation:
<nav class="main-navigation">
<ul id="primary-menu" class="primary-menu menu">
<li class="menu-item"><a href="http://localhost/wordpress">Home</a></li>
<li class="menu-item"><a href="http://localhost/wordpress/about">About</a>
<ul class="sub-menu">
<li class="menu-item"><a href="http://localhost/wordpress/about/history/">History</a></li>
<li class="menu-item"><a href="http://localhost/wordpress/about/people">People</a></li>
</ul>
</li>
<li class="menu-item"><a href="http://localhost/wordpress/services/">Services</a></li>
<li class="menu-item"><a href="http://localhost/wordpress/what-we-do">What We Do</a></li>
<li class="menu-item"><a href="http://localhost/wordpress/the-library/">The Library</a>
<ul class="sub-menu">
<li class="menu-item"><a href="http://localhost/wordpress/the-library/gallery/">Gallery</a></li>
<li id="menu-item" class="menu-item"><a href="http://localhost/wordpress/audio-and-video/">Audio and Video</a></li>
</ul>
</li>
<li class="menu-item"><a href="http://localhost/wordpress/donate/">Donate</a></li>
<li class="menu-item"><a href="http://localhost/wordpress/contact/">Contact</a></li>
</ul>
</nav>
And here is most of the CSS relating to it:
.primary-menu,
.primary-menu ul {
margin: 0;
padding: 0;
list-style: none;
}
.primary-menu {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
margin-left: calc(var(--menuItemsSpacing) / -2);
margin-right: calc(var(--menuItemsSpacing) / -2);
}
.primary-menu a {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
text-align: initial;
overflow: none;
color: #000;
}
.primary-menu .current-menu-item a {
color:#FFF;
background-color: #09153F;
}
.primary-menu>li {
font-size: 18px;
font-family: 'Open Sans', sans-serif;
font-weight: 400;
position:relative;
margin-bottom: 20px;
}
.primary-menu>li a {
padding: 10px 25px 10px 25px;
-webkit-transition-duration: 0.3s;
transition-duration: 0.3s;
-webkit-transition-property: color, background-color;
transition-property: color, background-color;
}
.primary-menu>li:hover a,
.primary-menu>li:focus a,
.primary-menu>li:active a {
background-color: #09153F;
color: #FFF;
}
.primary-menu .sub-menu {
position: absolute;
top: 100%;
margin-top: var(--dropdownTopOffset);
min-width: 100%;
width:120%;
display:block;
border-radius: 2px;
background: #09153F;
}
.primary-menu .sub-menu li {
font-size: 15px;
font-weight: 400;
text-transform: initial;
line-height: 1.6;
}
.primary-menu .sub-menu li a {
transition: padding 0.2s ease-out;
}
.primary-menu .sub-menu li a:hover {
padding-left:30px;
color: var(--menuHoverColor);
}
Look into CSS transitions.
Here is an example. ul.topLevel li { transition:all .6s ease-out } ul.topLevel li:hover { transition:all .2s ease-out }
Edit:
As per the [CodePen](https://codepen.io/toddsynan/pen/pJuyx) all that is needed is transitions.
.nav2 li ul {
position: absolute;
left: 0;
top: 36px;
z-index: 1;
visibility: hidden;
opacity: 0;
filter: alpha(opacity=0);
-webkit-transition: 200ms ease;
-moz-transition: 200ms ease;
-o-transition: 200ms ease;
transition: 200ms ease;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.