jQuery:按时间间隔更改文本颜色
我希望有人能帮我解决 jQuery 问题。我有一个网站,我希望文本块在页面加载时在定义的时间间隔内改变颜色。例如,在下面的代码中;
<div id="fade">
<div class="fade1">text block 1</div>
<div class="fade2">text block 2</div>
<div class="fade3">text block 3</div>
</div>
我希望 .fade1 从颜色更改为:#000,字体粗细:正常;颜色:#F00,字体粗细:加粗五秒然后恢复正常;接下来是 .fade2,然后是 .fade3 等。我希望这些效果在页面加载时发生,而不是由鼠标单击或悬停触发。
我对这种类型的编程相当陌生,并尝试使用 jQuery.Color() 和 .animate() 方法,但我似乎无法达到我想要的效果。 非常感谢任何帮助 - 谢谢。
**这是我第一次写这篇文章以来使用的代码:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<div id="fade1">Text Block 1</div>
<div id="fade2">Text Block 2</div>
<div id="fade3">Text Block 3</div>
<script type="text/javascript">
var index = 0;
setInterval(highlightText, 3000);
function highlightText() {
index = (index % 3) + 1;
$('#fade' + index).css('color', '#e7008a').css('font-size', '110%');
setTimeout(function() {
$('#fade' + index).css('color', '#000').css('font-size', '100%');
}, 2900);
}
</script>
I was hoping someone could help me out with a jQuery question. I have a site where I want blocks of text to change colour within defined time intervals when the page loads. As an example, in the following code;
<div id="fade">
<div class="fade1">text block 1</div>
<div class="fade2">text block 2</div>
<div class="fade3">text block 3</div>
</div>
I would like .fade1 to change from color: #000, font-weight: normal; to color: #F00, font-weight: bold for five seconds then return to normal; followed by .fade2 and then .fade3 etc. I want these effects to happen on page load and not triggered by mouse click or hover.
I’m fairly new to this type of programming and have tried to play around with the jQuery.Color() and .animate() methods but I can’t seem to achieve the effect I want.
Any help much appreciated – thank you.
**This is code I've used since first writing this post:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<div id="fade1">Text Block 1</div>
<div id="fade2">Text Block 2</div>
<div id="fade3">Text Block 3</div>
<script type="text/javascript">
var index = 0;
setInterval(highlightText, 3000);
function highlightText() {
index = (index % 3) + 1;
$('#fade' + index).css('color', '#e7008a').css('font-size', '110%');
setTimeout(function() {
$('#fade' + index).css('color', '#000').css('font-size', '100%');
}, 2900);
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用文档的 Ready 功能,您可以在此处阅读更多相关信息。
在ready函数中使用setTimeout函数。您可以在此处了解更多相关信息。
Use the ready function of your document, you can read more about that here.
Use the setTimeout function inside the ready function. You can read more about it here.