简体   繁体   English

动画结束后使用js添加文本

[英]Add text with js after animation ends

I'm trying to add a text (that is a countdown ) made with JS that works, but only after another text (made with JS) has finished it's animation, but I'm struggling with it. 我正在尝试添加一个可以使用JS制作的文本(这是一个倒计时),但是只有在另一个文本(用JS制作)完成动画之后,我才开始努力。 I've tried some methods but nothing worked for me, I'm new but I'm a quick learner. 我尝试了一些方法,但对我没有任何帮助,我是新手,但我是一个快速学习者。

Can someone help me out? 有人可以帮我吗?

Here is a part of my code: 这是我的代码的一部分:

HTML 的HTML

 // Set the date we're counting down to var countDownDate = new Date("Feb 1, 2019 11:59:20").getTime(); // Update the count down every 1 second var x = setInterval(function() { // Get todays date and time var now = new Date().getTime(); // Find the distance between now and the count down date var distance = countDownDate - now; // Time calculations for days, hours, minutes and seconds var days = Math.floor(distance / (1000 * 60 * 60 * 24)); var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); // Display the result in the element with id="countdown" document.getElementById("countdown").innerHTML = days + " days left " + hours + "h " + minutes + "m " + seconds + "s "; // Add text to <h1 id="fade"> var comingSoon = document.getElementById("fade"); comingSoon.innerHTML = 'COMING SOON'; // If the count down is finished, write some text if (distance < 0) { clearInterval(x); //document.getElementById("countdown").innerHTML = "Well.. what are you waiting for?"; // If countdown finished, remove the <p id="countdown"> var parent = document.getElementById("comingSoonContainer"); var child = document.getElementById("countdown"); parent.removeChild(child); // Create new <p> with text var para = document.createElement("p"); var node = document.createTextNode("New Paragraph works fine."); para.appendChild(node); var element = document.getElementById("comingSoonContainer"); element.appendChild(para); // Replace <h1> with text para = document.createElement("h1"); node = document.createTextNode("Enjoooooy !!"); para.appendChild(node); parent = document.getElementById("comingSoonContainer"); child = document.getElementById("fade"); parent.replaceChild(para, child); //document.getElementById("fade").innerHTML = "Enjoy !!"; } }, 1000); 
 #countdown { font-family: 'Raleway', sans-serif; font-size: /*1.3em;*/ 3em; color: #ffffff; /*including fade animation*/ -webkit-animation-name: fade; -webkit-animation-duration: 12s; animation-name: fade; animation-duration: 12s; } /* Fade animation */ #fade { -webkit-animation-name: fade; -webkit-animation-duration: 8s; animation-name: fade; animation-duration: 8s; } @-webkit-keyframes fade { from {opacity: .0} to {opacity: 1} } @keyframes fade { from {opacity: .0} to {opacity: 1} } 
  <div id="comingSoonContainer"> <h1 id="fade"></h1> <p id="countdown"></p> </div> 

Thanks 谢谢

There is animation callbacks: 有动画回调:

 function callFunction(){ } var element=document.getElementById('fade'); element.addEventListener("webkitAnimationEnd", callfunction,false); element.addEventListener("animationend", callfunction,false); element.addEventListener("onaimationend", callfunction,false); 

UPDATE: First, the problem is with your CSS styles for the countdown paragraph . 更新:首先,问题在于您的CSS样式的countdown段落。 You set the paragraph color to white and since your body is white as well, that's why you never see the countdown. 您将段落颜色设置为白色,并且由于您的身体也是白色的,所以这就是您从未看到倒数的原因。 But if you change the color to black for instance, you'll see your countdown. 但是,例如,如果您将颜色更改为黑色,则会看到倒数计时。

Second, I added an animationend event to your fade element and put the setInterval inside it. 其次,我向您的衰落元素添加了animationend事件,并将setInterval放入其中。

Here is the CSS you need to change: 这是您需要更改的CSS:

#countdown {
    font-family: 'Raleway', sans-serif;
    font-size: /*1.3em;*/ 3em;
    color: #000;                      // Color was #ffffff
    /*including fade animation*/
    -webkit-animation-name: fade;
      -webkit-animation-duration: 12s;
      animation-name: fade;
      animation-duration: 12s;
}

And here is the javascript: 这是javascript:

// Set the date we're counting down to
var countDownDate = new Date("Feb 1, 2019 11:59:20").getTime();

// Add text to <h1 id="fade">
var comingSoon = document.getElementById("fade");
comingSoon.innerHTML = 'COMING SOON';

comingSoon.addEventListener('animationend', ()=>{


// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

 // Display the result in the element with id="countdown"
  document.getElementById("countdown").innerHTML = days + " days left " + hours     + "h "
  + minutes + "m " + seconds + "s ";

});

 });

I removed the part where you check for distance just because it had nothing to do with what you asked for help. 我删除了您检查距离的部分,因为它与您寻求帮助的内容无关。 You can add it though. 您可以添加它。

But here is the working fiddle https://jsfiddle.net/fwxL1sj4/33/ 但是这是工作中的小提琴https://jsfiddle.net/fwxL1sj4/33/

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

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