【发布时间】:2014-11-22 13:06:21
【问题描述】:
最近我一直在学习一些 JavaScript 来改进我的网页。
我尝试做一个简单的练习,其中我每 5 秒更改一个元素,但是当我尝试使用 css 添加淡入淡出动画并且仅适用于第一次时,此时我真的不知道我的 javascript没有以正确的方式编写或与我的 css 有关。
有更多知识和经验的人能给我一些建议吗?
好的,所以基本上问题是我不能让动画像句子一样重复,你可以在这里查看:
//Calling the element.
var $slogan = document.getElementById("slogan");
// Setting an array with several strings.
var sloganArray = ["This sentence will change every 5 seconds!", "See? I'm changing!", "Knock Knock.", "Who's there?", "Heck I don't know!"];
//Setting variable to control the index.
var sloganIndex = 0;
/* This function (only when called) replaces the text of the element called before with text contained on the strings of the array, each time incrementing one and going through every array position. */
function changeSlogan() {
$slogan.innerHTML = sloganArray[sloganIndex];
++sloganIndex;
if (sloganIndex >= sloganArray.length) {
sloganIndex = 0;
}
}
//Calling the function created before every five seconds.
setInterval(changeSlogan,5000);
#slogan {
margin: 0;
font-family: Arial, sans-serif;
font-size: 20px;
color: #000;
opacity: 1;
transition: opacity .3s ease-out;
-webkit-animation:fadeIn ease-out ;
-moz-animation:fadeIn ease-out ;
animation:fadeIn ease-out;
-webkit-animation-fill-mode:forward; /* this makes sure that after animation is done we remain at the last keyframe value (opacity: 1)*/
-moz-animation-fill-mode:forward;
animation-fill-mode:forward;
-webkit-animation-duration: 1.5s;
-moz-animation-duration: 1.5s;
animation-duration: 1.5s;
-webkit-animation-delay: 4.9s; /* Chrome, Safari, Opera */
animation-delay: 4.9s;
animation-iteration-count: infinite;
}
<h3 id="slogan"><em>This is a dynamic sentence!</em></h3>
感谢您的宝贵时间!
【问题讨论】:
标签: javascript html css loops animation