0

我有一个带过渡的 div。我只是通过一些放松来扩展它。悬停时会改变它background-color,当我们将鼠标移出它时会background-color再次改变它!但我希望背景颜色在过渡完成后恢复。我怎样才能做到这一点?

HTML:

<div class="userWidget">
    <div class="user">
        <img src="img/user.png" width="50" height="50" />   <span class="name">Danish</span>
        <br/>   <span class="skills">Coder, Programmer</span>

    </div>
</div>
<div class="userWidget">
    <div class="user">
        <img src="img/user.png" width="50" height="50" />   <span class="name">Danish</span>
        <br/>   <span class="skills">Coder, Programmer</span>

    </div>
</div>
<div class="userWidget">
    <div class="user">
        <img src="img/user.png" width="50" height="50" />   <span class="name">Danish</span>
        <br/>   <span class="skills">Coder, Programmer</span>

    </div>
</div>
<div class="userWidget">
    <div class="user">
        <img src="img/user.png" width="50" height="50" />   <span class="name">Danish</span>
        <br/>   <span class="skills">Coder, Programmer</span>

    </div>
</div>

CSS:

.userWidget {
    position: relative;
    display: inline-block;
    width: 250px;
    height: 50px;
    overflow: visible;
    z-index: 1;
}
.userWidget:hover {
    z-index: 2;
}
.user {
    position: absolute; 
    display: inline-block;
    width: 200px;
    height: 50px;
    margin-bottom: 5px;
    background: #fff;
    transition: width 0.3s, height 0.3s;
}
.user:hover {
    width: 350px;
    height: 200px;
    transition: width 0.3s ease 0.5s, height 0.3s ease 0.5s, background-color 2.3s;
    background: #eee;
}
.user img {
    float: left;
}
.user .name, .skills {
    margin-left: 5px;
}
.user .name {
    font-size: 21px;
    font-weight: bold;
}

这是小提琴

正如您在小提琴中看到的那样,返回过渡只会让它变得很奇怪!过渡完成后如何恢复background-color#fff

4

1 回答 1

1

使用关键帧动画而不是过渡(jsfiddle 演示)可能会更好吗?

.user:hover {
    width: 350px;
    height: 200px;
    transition: width 0.3s ease 0.5s, height 0.3s ease 0.5s;
    animation: flash 2.5s ease .8s forwards;
}

@keyframes flash {
    0% { background: #fff; }
    80% { background: #eee; }
    100% { background: #fff; }
}
于 2013-07-03T20:17:49.653 回答