简体   繁体   English

CSS3动画不是以JQuery开头

[英]CSS3 animation not starting with JQuery

First of all sorry for my bad English. 首先,抱歉我的英语不好。 I try my best :) 我尽力了 :)

The idea is to realise an animated countdown (days / hours / minutes / seconds). 这个想法是实现动画倒计时(天/小时/分钟/秒)。

This is the countdown : 这是倒计时:

<div id="sp_days"></div> : <div id="sp_hours"></div> : <div id="sp_minutes"></div> : <div id="sp_seconds"></div>

The animation : 动画:

.pop{
    -webkit-animation-name: pop;
    -webkit-animation-duration: 1s;
    -webkit-animation-timing-function: linear;
    animation-name: pop;
    animation-duration: 1s;
    animation-timing-function: linear;
}
@-webkit-keyframes pop
{
    0% {
        -webkit-transform:scale(1.3);
    }
    100% {
        -webkit-transform:scale(1);
    }
}
@keyframes pop
{
    0% {
        transform:scale(1.3);
    }
    100% {
        transform:scale(1);
    }
}

The code is working for days, hours and minutes, but i can't figure out why not for the seconds... 该代码工作了几天,几小时和几分钟,但我不知道为什么不这样做……

This is basically my jquery loop : 这基本上是我的jquery循环:

var last_minutes;
var last_seconds;

setInterval(function () {
    update();
}, 1000);

function update(){
    minutes = ...; // calculating minutes left
    if(last_minutes !== minutes){
        $("#sp_minutes").addClass("pop");
    } else {
        $("#sp_minutes").removeClass("pop");
    }
    last_minutes = minutes;

    seconds = ...; // calculating seconds left
    if(last_seconds !== seconds){
        $("#sp_seconds").addClass("pop");
    } else {
        $("#sp_seconds").removeClass("pop");
    }
    last_seconds = seconds;
}

So when you load the page, every div (days, hours, minutes, seconds) are "pop-ing" and it's normal, the seconds are the only one which is not working after its first "pop" animation. 因此,当您加载页面时,每个div(天,小时,分钟,秒)都会“弹出”,这是正常现象,秒是唯一一个在其第一个“弹出”动画之后不起作用的秒。

Thank you for your help. 谢谢您的帮助。

NB : I know you can loop an animation using animation-iteration-count, but in this case the animation desyncs from the javascript loop every time you change app (smarphones) or reduce windows or change tab in chrome. 注意:我知道您可以使用animation-iteration-count循环播放动画,但是在这种情况下,每次您更改应用程序(智能手机)或缩小窗口或更改chrome中的制表符时,动画都会与javascript循环不同步。 Also, the loading time of the page can desync the animation too. 同样,页面的加载时间也可以使动画不同步。

I found out how to do it ! 我发现了怎么做!

Removing and adding the same class at the same time is not taking affect. 同时删除和添加同一类不会影响。 So you have to clone the element, and then remove the old one in order to restart the animation. 因此,您必须克隆该元素,然后删除旧元素以重新启动动画。

seconds = ...; // calculating seconds left
if(last_seconds !== seconds){
    var el = $("#sp_seconds");  
    var newone = el.clone(true);
    el.before(newone);
    $("." + el.attr("class") + ":last").remove();
}
last_seconds = seconds;

HTML : HTML:

<div class="pop" id="sp_seconds">

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM