使用 jQuery 使横幅像文本一样淡入淡出
我有一个标题文本,我想将其淡入框中。 然后,字幕就会在其下方淡入。 一旦两者都可见,它们应该淡出,下一组应该以相同的方式淡入。
这是我创建的,但是,我怀疑它的运行情况,也就是说,它是否占用浏览器的 CPU 资源。我关心的问题之一也是 Javascript 的递归深度。
我有以下代码(我也将其与 CSS 和 HTML 一起包含在 jsfiddle 中 http://jsfiddle.jsfiddle.html 中) net/ukewY/1/)
var no = 3;
function fadeText(id) {
// Fade in the text
$("#text" + id).animate({
opacity: 1,
left: '+=50'
}, 5000, function() {
// Upion completion, fade in subtext
$("#subtext" + id).animate({
opacity: 1,
}, 5000, function() {
// Upon completion, fade the text out and move it back
$("#subtext" + id).animate({
opacity: 0,
}, 1000, function() {
$("#text" + id).animate({
opacity: 0,
left: '+=200'
}, 3000, function() {
// Yet again upon completion, move the text back
$("#text" + id).css("left", "19%");
$("#subtext" + id).css("left", "10%")
fadeText((id % no) + 1);
});
});
});
});
}
$(document).ready(function() {
fadeText(1);
});
我的问题是这是否是正确的方法;或者是否有更好的(也许是非递归的)方法来做到这一点?
附言。由于这是一个网站的横幅,我不担心 id
太大,因为人们可能已经离开了。
I have a title text, which I want to fade into a box.
Afterwards, a subtitle should fade in beneath it.
Once both are visible, they should fade out, and the next set should fade in, in the same fashion.
This I have created, however, I have my doubts on how well it runs, that is, if it takes to much CPU for the browser. One of my concerns are also the recursion depth of Javascript.
I have the following code (which I have also included in a jsfiddle, along with CSS and HTML http://jsfiddle.net/ukewY/1/)
var no = 3;
function fadeText(id) {
// Fade in the text
$("#text" + id).animate({
opacity: 1,
left: '+=50'
}, 5000, function() {
// Upion completion, fade in subtext
$("#subtext" + id).animate({
opacity: 1,
}, 5000, function() {
// Upon completion, fade the text out and move it back
$("#subtext" + id).animate({
opacity: 0,
}, 1000, function() {
$("#text" + id).animate({
opacity: 0,
left: '+=200'
}, 3000, function() {
// Yet again upon completion, move the text back
$("#text" + id).css("left", "19%");
$("#subtext" + id).css("left", "10%")
fadeText((id % no) + 1);
});
});
});
});
}
$(document).ready(function() {
fadeText(1);
});
My question is if this is the right way to do it; or if there is a better, maybe none-recursive, way of doing this?
PS. As this is for a banner for a website, I do not worry about id
being to big, as the people have probably moved on since.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
递归对我来说似乎很好,但我还发现了一些其他事情:
我接受了这些,并随意将代码调整为以下内容:
The recursion seems fine to me, but there are a few other things I spotted:
I took car of these and took the liberty of tweaking your code to the following: