如何重现这种翻转效果

时间:2019-07-28 17:58:17

标签: javascript html css wordpress

This website的导航栏具有我喜欢的很好的平滑滚动效果。据我所知,它是用JavaScript完成的-可以用CSS轻松地模拟这样的事情,如果可以,我如何将其应用于我自己网站的导航中?

我一直在寻找CSS示例,但是我发现没有什么东西可以接近该网站菜单所具有的类似层叠效果,并且将我发现的内容改编到我的(Wordpress)网站上只会破坏我的导航。 / p>

当站点的子菜单项失去焦点时的缓慢回退也将是一笔巨大的收获。如果无法在CSS中将效果模拟到同样的平滑度,那么我很乐意针对无需付费的JavaScript解决方案/库。

以下是我网站导航的标记:

<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>

以下是与之相关的大多数CSS:

.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);
}

1 个答案:

答案 0 :(得分:-1)

查看CSS过渡。

这里是一个例子。 ul.topLevel li {  过渡:所有.6s缓解 } ul.topLevel li:hover {  过渡:所有.2s缓解 }

编辑:

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;
}