代码之家  ›  专栏  ›  技术社区  ›  JLF

CSS关键帧动画完成后消失

  •  1
  • JLF  · 技术社区  · 10 年前

    我的动画在它完成动画后一直消失。我已经遵循了其他问题的答案,但仍然无济于事。我的CSS如下:

    .upvote-object {
        width: 50px;
        height: 62.5px;
        background: url('/icons/upvote.png') left center no-repeat;
        background-size: 50px 62.5px;
    
        &.upvoted {
            background: url('/icons/upvoted.png') left center no-repeat;
            background-size: 50px 62.5px !important;
        }
    }
    .ani-upvote {
        background: url('/icons/upvote-sprite.png') left center no-repeat;
        background-size: 1700px 62.5px;
    
        animation: play 3s steps(34);
        animation-fill-mode: forwards;
        -webkit-animation-fill-mode: forwards;
        opacity: 1;
    }
    
    @keyframes play {
        100% {
            background-position: -1700px;
            opacity: 1;
        }
    }
    @-webkit-keyframes play {
        100% {
            background-position: -1700px;
            opacity: 1;
        }
    }
    

    我在HTML中这样调用它:

    <div class="upvote-object ani-upvote"></div>
    

    问题是: JSFiddle

    更新: 还请注意,动画也必须停留在最后一帧!(绿色)

    1 回复  |  直到 10 年前
        1
  •  1
  •   Antoni    4 年前

    我成功了。但我不是CSS专家。所以我很抱歉不能解释为什么它会起作用。也许你知道原因:)

        .upvote-object {
            width: 50px;
            height: 62.5px;
            background: url('http://sstaging-parts.herokuapp.com/icons/upvote.png') left center no-repeat;
            background-size: 50px 62.5px;
            animation-fill-mode: forwards;
            -webkit-animation-fill-mode: forwards;
            opacity: 1;
        }
        .ani-upvote {
            background: url('http://staging-parts.herokuapp.com/icons/upvote-sprite.png') left center no-repeat;
            background-size: 1700px 62.5px;
    
            animation: play 2s steps(33); // changed the steps to 33
            animation-fill-mode: forwards;
            -webkit-animation-fill-mode: forwards;
            opacity: 1;
        }
    
        @keyframes play {
            100% { background-position: -1650px; opacity: 1; }
        }
        @-webkit-keyframes play {
            100% { background-position: -1650px; opacity: 1; } // Changed the position to -1650
        }
    

    更新 我成功了。我认为这与精灵和它停止动画的位置有关…如果这有意义的话。