// hide word-rotate spans
$('.word-rotate').hide();
// Set variables
var words = [],
index = 0,
$span = $('.text-rotate');
// Get words within 'word-rotate' spans and push in array
$('.word-rotate').each(function() {
words.push($(this).text());
});
// Rotate function
function rotateFunction() {
$span.text(words[index]).fadeIn(0);
if ( index == words.length -1 ) {
$span.css('color', '#F42156');
$span.delay(50000).fadeOut(0);
index = 0;
} else {
$span.css('color', '#00FFEE');
$span.delay(500).fadeOut(0);
index++;
}
}
setInterval(rotateFunction, 500);
html {
background: #313449;
color: #ffffff;
display: flex;
height: 100%;
justify-content: center;
align-items: center;
font-family: 'Open Sans', sans-serif;
text-align: center;
}
.text-rotate {
color: #00FFEE;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div>
<h1>Make it more
<br />
<span class="text-rotate"></span>
<span class="word-rotate">attractive</span>
<span class="word-rotate">fun</span>
<span class="word-rotate">colorful</span>
<span class="word-rotate">fancy</span>
<span class="word-rotate">intelligent.</span>
</h1>
</div>
我正在试图找出如何创建一个更长的时间,在我的名单上的最后一个字改变的系统。
它是为旋转的部分工作,但我不知道如何暂停最后一个字。我将.delay更改为5000,但不像预期的那样工作。
我想让每个词出现在500毫秒和最后一个5s。
为什么这个.delay()不能工作?
以下是代码:http://codepen.io/mouuuton/pen/ZBzqGL/
发布于 2016-11-03 09:35:25
// hide word-rotate spans
$('.word-rotate').hide();
// Set variables
var words = [],
index = 0,
$span = $('.text-rotate');
// Get words within 'word-rotate' spans and push in array
$('.word-rotate').each(function() {
words.push($(this).text());
});
// Rotate function
function rotateFunction() {
$span.text(words[index]).fadeIn(0);
if ( index == words.length -1 ) {
$span.css('color', '#F42156');
$span.delay(50000).fadeOut(0);
index = 0;
setTimeout(rotateFunction, 5000);
} else {
$span.css('color', '#00FFEE');
$span.delay(500).fadeOut(0);
index++;
setTimeout(rotateFunction, 500);
}
}
setTimeout(rotateFunction, 500);
html {
background: #313449;
color: #ffffff;
display: flex;
height: 100%;
justify-content: center;
align-items: center;
font-family: 'Open Sans', sans-serif;
text-align: center;
}
.text-rotate {
color: #00FFEE;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<div>
<h1>Make it more
<br />
<span class="text-rotate"></span>
<span class="word-rotate">attractive</span>
<span class="word-rotate">fun</span>
<span class="word-rotate">colorful</span>
<span class="word-rotate">fancy</span>
<span class="word-rotate">intelligent.</span>
</h1>
</div>
发布于 2016-11-03 09:38:18
我不会使用delay
和setInterval
,而是这样做的:
function rotateFunction() {
$span.text(words[index]).fadeIn(0);
if ( index == words.length -1 ) {
setTimeout(function() {
$span.css('color', '#F42156');
$span.delay(50000).fadeOut(0);
index = 0;
rotateFunction();
}, 5000);
} else {
setTimeout(function() {
$span.css('color', '#00FFEE');
$span.delay(500).fadeOut(0);
index++;
rotateFunction();
}, 500);
}
}
rotateFunction();
https://stackoverflow.com/questions/40397822
复制相似问题