作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想为我的段落做一个动画,点击按钮后内容被替换。所以按钮 #start_animation
应该启动 textillate 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>
最佳答案
问题是,每次替换段落中的文本时都需要“重置”插件。那么,您还需要替换元素 DOM 本身,否则插件将不会执行任何操作,因为它已经附加了一个 DOM 元素。
$('#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() {
$('#paragraph').replaceWith('<p id="paragraph">' + getRandomItem(words) + '</p>');
init();
}
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 init() {
console.log('init');
$('#paragraph').textillate({
loop: false,
autoStart: false,
in: {
effect: 'fadeIn',
shuffle: false,
delay: 80
},
out: {
effect: 'fadeOut',
shuffle: false,
delay: 40,
reverse: true
}
});
}
/*-----------------------------------------------*/
$(function() {
init();
});
.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>
顺便说一句,为什么您需要手动完成所有这些工作人员?该插件似乎具有手动替换文本的功能带有过渡。
关于javascript - 召回我的 textille 动画的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49718928/
我想为我的段落做一个动画,点击按钮后内容被替换。所以按钮 #start_animation 应该启动 textillate in 动画,按钮 #out_animation 应该导致 out 动画。然后
我是一名优秀的程序员,十分优秀!