我想为我的段落制作动画,点击按钮后内容将被替换。所以按钮#start_animation
应该启动文本in
动画,按钮#out_animation
应该引起out
动画。然后我点击#replaced_word
替换段落内容的第三个按钮。
重要提示:我总是点击同一行:
#start_animation
#out_animation
#replaced_word
所以这个词应该由一个动画()进来,fadeIn
然后由另一个动画()出去,fadeOut
然后被替换,然后我再次点击第一个按钮#start_animation
,它一遍又一遍地重复。问题是它只在第一次工作。我的意思是直到我#start_animation
第二次点击。我之前用代码写过这个问题,但它太长了,所以我以片段的形式做了。有任何想法吗?
$('#start_animation').click(function() {
$('#sample_text').textillate('in');
$('#paragraph').textillate('start');
$('#paragraph').addClass('show');
});
/*-----------------------------------------------*/
$("#out_animation").click(function() {
$('#sample_text').textillate('out');
$('#paragraph').textillate('out');
setTimeout(function() {
$('#paragraph').removeClass('show');
}, 1000);
});
var words = ["peter", "michael", "john", "derek", "chris"];
function replacing() {
document.getElementById('paragraph').innerHTML = getRandomItem(words);
}
var btn2 = document.getElementById("replaced_word");
btn2.addEventListener("click", replacing);
function getRandomItem(array) {
return array.splice(Math.floor(Math.random() * array.length), 1)[0];
}
/*-----------------------------------------------*/
$(function() {
$('#paragraph').textillate({
loop: false,
autoStart: false,
in: {
effect: 'fadeIn',
shuffle: false,
delay: 80
},
out: {
effect: 'fadeOut',
shuffle: false,
delay: 40,
reverse: true
}
});
})
.show {
opacity: 1!important;
}
#paragraph {
opacity: 0;
}
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script src="https://cdn.rawgit.com/jschr/textillate/3ca7326c/jquery.textillate.js"></script>
<script src="https://cdn.rawgit.com/davatron5000/FitText.js/0b4183af/jquery.fittext.js"></script>
<script src="https://cdn.rawgit.com/davatron5000/Lettering.js/d06bb733/jquery.lettering.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
<button id="start_animation">start animation</button>
<button id="out_animation">out animation</button>
<button id="replaced_word">replace word</button>
<p id="paragraph">TEST</p>