我正在尝试创建这样的旋转文本效果:http://www.massstudies.com/ 。但是,我希望当用户将鼠标悬停在文本上方时旋转速度减慢。我目前已经停止了,我正在使用clearInterval。
非常感谢任何建议。谢谢:)
HTML:
<div id="main">
<p id="text"></p>
<button onclick="pauseRotation();">Stop</button>
</div>
脚本:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
var words = ['Chocolate', 'Sugar','Cocoa Powder', 'Salt'];
var index = 0;
var nIntervId;
function rotate() {
document.getElementById('text').innerHTML =words[(index++)%(words.length)];
}
nIntervId=setInterval(rotate, 100);
function pauseRotation(){
clearInterval(nIntervId);
}
</script>
最佳答案
这里有一个方法,用这个 fiddle 测试一下:http://jsfiddle.net/6ybyM/2/
var words = ['Chocolate', 'Sugar',"Cocoa Powder", 'Salt'];
var index = 0;
(function rotate() {
document.getElementById('text').innerHTML =words[(index++)%(words.length)];
if($("#text").is(":hover")){
setTimeout(rotate, 300);
}
else{
setTimeout(rotate, 100);
}
})()
关于javascript - 减慢旋转/交替文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23033875/