如何修复CSS“转换”取消悬停元素?

问题描述 投票:1回答:3

我在页面的左角有一个导航栏。导航栏已经在悬停时设置在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 css3
3个回答
1
投票

我认为你使用关键帧使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);
}

0
投票

如果你正在寻找一个简单的滑出并向后滑动,一种方法是在你的transform: transitionX(-120px)元素上简单地使用ul,并在ul的悬停时重新调整从-120px到0px的值,如下所示:

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

首先你的位置是固定的,我认为这是一个问题,然后是负边距 - 左边仍然感觉正确使用translateX和负值最后你需要一个关键帧悬停状态浏览器将通过从完成播放您的关键帧处理相反的条件开始#cheers

© www.soinside.com 2019 - 2024. All rights reserved.