【问题标题】:How to fix CSS "transition" un-hover an element?如何修复 CSS“过渡”取消悬停元素?
【发布时间】:2019-07-08 12:11:00
【问题描述】:

我在页面的左角有一个导航栏。导航栏在悬停时已经设置在margin-left: -120px;,它转换为105px,所以我希望在取消悬停元素时平滑返回。

我尝试在 ul、ul li、ul li a 上使用transition,将它们放在不同的类中。没有什么真正适合我...

/* ----- NAVBAR ----- */

ul {
    position: fixed;
    z-index: 99;
}

.li1{
    animation: move1 2s;
}
.li2{
    animation: move1 3s;
}
.li3{
    animation: move1 4s;
}
.li4{
    animation: move1 5s;
}
.li5{
    animation: move1 6s;
}

ul li {
    text-align: center;
    list-style: none;
    padding-top: 10px;
    margin-left: -120px;
}

ul li a {
    text-align: center;
    display: block;
    text-decoration: none;
    height: 30px;
    line-height: 30px;
    font-size: 12px;
    font-weight: 400;
    letter-spacing: 5px;
    background: #1a2738;
    width: 140px;
    color: #fff;
    text-transform: capitalize;
    border-radius: 15px;
}

ul li a:hover {
    background: #1a2738;
    color: #fff;
    animation: move2 1s forwards;
}

@keyframes move1 {
    0% {
        opacity: 0;
        transform: translate(-50px);
    }
    40% {
        opacity: 100;
        transform: translate(105px);
    }
}

@keyframes move2 {
    100% {
        transform: translate(105px);
    }
}

我希望知道实际问题是什么,请帮助,谢谢!

【问题讨论】:

标签: css


【解决方案1】:

如果您正在寻找一个简单的滑出和滑回效果,一种方法是在您的 ul 元素上简单地使用 transform: transitionX(-120px),然后在悬停时将值从 -120px 重新调整为 0px的ul,像这样:

ul {
  list-style-type: none;
  padding: 1em;
  background-color: #ececec;
  color: #333;
  display: inline-block;
  transition: transform 600ms;
  transform: translateX(-120px);
}

ul:hover {
  transform: translateX(0px); //you can adjust this measurement unit to whatever value you wish, such as 105px
}

ul li {
    text-align: center;
    list-style: none;
    padding-top: 10px;
}

ul li a {
    text-align: center;
    display: block;
    text-decoration: none;
    height: 30px;
    line-height: 30px;
    font-size: 12px;
    font-weight: 400;
    letter-spacing: 5px;
    background: #1a2738;
    width: 140px;
    color: #fff;
    text-transform: capitalize;
    border-radius: 15px;
}
<ul>
  <li><a href="">Home</a></li>
  <li><a href="">About</a></li>
  <li><a href="">contact</a></li>
</ul>

【讨论】:

    【解决方案2】:

    我认为您使用关键帧使 CSS 过于复杂,您可以使用过渡而不是参见下面的代码:

    /* ----- NAVBAR ----- */
    
    ul {
        position: fixed;
        z-index: 99;
    }
    
    ul li {
        text-align: center;
        list-style: none;
        padding-top: 10px;
        margin-left: -120px;
    }
    
    ul li a {
        text-align: center;
        display: block;
        text-decoration: none;
        height: 30px;
        line-height: 30px;
        font-size: 12px;
        font-weight: 400;
        letter-spacing: 5px;
        background: #1a2738;
        width: 140px;
        color: #fff;
        text-transform: capitalize;
        border-radius: 15px;
        transition: transform 1s ease-in-out;
        transform: translateX(-50px);
    }
    
    ul li a:hover {
        background: #1a2738;
        color: #fff;
        transform: translateX(105px);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 2013-07-12
      • 1970-01-01
      • 2018-01-22
      • 2021-05-17
      相关资源
      最近更新 更多