我正在尝试创建这样的旋转文字效果: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>
答案 0 :(得分:1)
这是一种方法,在这个小提琴中测试它: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);
}
})()