我需要用fadeInDown和fadeOutDown效果在Javascript中每5秒换一个单词。我有一组不同的单词要展示。我在动画中使用了下面的链接,但是当单词发生变化时,它没有动画效果。
动画:http://jschr.github.io/textillate/
二、在动画中,选择‘fadeInDown’+‘sync’iii。对于Out动画,选择‘fadeOutDown’+‘sync
用于更改下列代码中的单词
<script>
$(function () {
var messages = [],
index = 0;
messages.push('awesome');
messages.push('incredible');
messages.push('cool');
messages.push('fantastic');
function cycle() {
$('#some-id').html(messages[index]);
index++;
if (index === messages.length) {
index = 0;
}
setTimeout(cycle, 5000);
}
cycle();
});
</script>
HTML code :
<div>
This is so<span id="some-id">awesome</span>
</div>发布于 2014-11-02 12:25:18
在动画第一个单词之后,Textillate在这里不能正常工作,因为您的cycle javascript函数只是将span的内部html替换为您想要的单词。要使textillate处理单词列表,我们需要创建适当的html标记,然后将其附加到所需的span元素中。
JQUERY
$(function () {
var messages = [],
index = 0;
messages.push('awesome');
messages.push('incredible');
messages.push('cool');
messages.push('fantastic');
//Function for generating appropriate html markup as required by textillate
function generateMarkup() {
var markup = '';
//Wrapping all words in <li> tags as required by textillate
messages.forEach(function(item){
markup += '<li>' + item + '</li>';
});
//Wrapping the li tags in <ul class="texts"> tag as required by textillate
markup = '<ul class="texts">' + markup + '</ul>';
return markup;
}
markup = generateMarkup();
//Appending the html markup we generated to the desired span element
$("#some-id").append(markup);
//Initializing textillate
$('#some-id').textillate({
in: {
effect: 'fadeInDown',
sync: true,
},
out: {
effect: 'fadeOutDown',
sync: true,
},
loop: true,
minDisplayTime: 5000, //Delay between changing words
});
});<div>
This is so <span id="some-id"></span>
</div>这是一个工作的小提琴
https://stackoverflow.com/questions/26698168
复制相似问题