如何在结束时停止css3动画?

时间:2014-12-16 11:15:08

标签: css css3 css-animations

我正在玩css3 translateY而且我不会在最后停止动画。

HTML:

<ul id="nav" class="nav-ctn">
    <li><a href="?type=about">About</a></li>
    <li><a href="?type=projects">Projects</a></li>
    <li><a href="?type=media">Media</a></li>
    <li><a href="?type=schedule">Schedule</a></li>
    <li><a href="?type=contact">Contact</a></li>
</ul>

CSS:

    .tr-up {
        -moz-animation: tr-up 0.5s ease-in-out;
        -o-animation: tr-up 0.5s ease-in-out;
        -webkit-animation: tr-up 0.5s ease-in-out;
        animation: tr-up 0.5s ease-in-out;
        -moz-animation-fill-mode: forwards;
        -o-animation-fill-mode: forwards;
        -webkit-animation-fill-mode: forwards;
        animation-fill-mode: forwards;
    }


    @-moz-keyframes tr-up {
        from {
            -moz-transform: translateY(0);
            -ms-transform: translateY(0);
            -o-transform: translateY(0);
            -webkit-transform: translateY(0);
            transform: translateY(0);
        }

        to {
            -moz-transform: translateY(-3px);
            -ms-transform: translateY(-3px);
            -o-transform: translateY(-3px);
            -webkit-transform: translateY(-3px);
            transform: translateY(-3px);
        }
    }

    @-webkit-keyframes tr-up {
        from {
            -moz-transform: translateY(0);
            -ms-transform: translateY(0);
            -o-transform: translateY(0);
            -webkit-transform: translateY(0);
            transform: translateY(0);
        }

        to {
            -moz-transform: translateY(-3px);
            -ms-transform: translateY(-3px);
            -o-transform: translateY(-3px);
            -webkit-transform: translateY(-3px);
            transform: translateY(-3px);
        }
    }

    @keyframes tr-up {
        from {
            -moz-transform: translateY(0);
            -ms-transform: translateY(0);
            -o-transform: translateY(0);
            -webkit-transform: translateY(0);
            transform: translateY(0);
        }

        to {
            -moz-transform: translateY(-3px);
            -ms-transform: translateY(-3px);
            -o-transform: translateY(-3px);
            -webkit-transform: translateY(-3px);
            transform: translateY(-3px);
        }
    }

到目前为止,我已尝试过以下帖子的解决方案:

Stopping CSS3 KeyFrames Animation

Stop CSS3 animation jumping

Animation stop with css3

Stopping a CSS3 Animation on last frame

但他们没有为我工作。 那么我做错了什么?

HERES THE FIDDLE

注意:tr-up类会动态添加到链接字符中。

2 个答案:

答案 0 :(得分:3)

动画shouldn't work on inline elements因此您需要将<span>代码的默认显示属性更改为span{display:inline-block;}

如果您想在菜单项上保留下划线,还需要将text-decoration:underline;添加到这些范围标记中:

<强> DEMO

答案 1 :(得分:0)

尝试添加迭代次数

animation-iteration-count: 1;

可能