【发布时间】:2014-06-10 18:16:44
【问题描述】:
下面的代码可以进一步优化吗?我觉得它现在写的它使用了太多的资源(我的JS测试知识几乎没有......)。 我欢迎提示和重写。
jQuery(document).ready(function($) {
$(document).ready(function() {setInterval(function () {spectrum()},5000); });
function spectrum(){
var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
$('.front-panel-heading').animate( { backgroundColor: hue },7000);
$('#content h2 > span').animate( {color:hue}, 7000);
$('#colophon > div > span').animate( {color:hue}, 7000);
};
});
注意事项: 该脚本在我的个人网站 (apleasantview.com) 上发布 我已经使用了一段时间,今天我对其进行了一些清理,并按照此处第三个答案中的建议在我的 css 中添加了“过渡”: How to apply transparency to a background that changes color smoothly
提前谢谢-克里斯。
//代码更新1跟随答案https://stackoverflow.com/a/24079418/2947983
jQuery(document).ready(function($) {
setInterval(function () {spectrum()},5000);
function spectrum(){
var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
$('div#front-panel-bkgr').animate( {backgroundColor:hue}, 7000);
$('span#clr-swtch').animate( {color:hue}, 7000);
};
});
//代码更新2跟随答案https://stackoverflow.com/a/24121309/2947983
jQuery(document).ready(function ($) {
setInterval(function () {
spectrum()
}, 7000);
var frontPanel = $('div#front-panel-bkgr'),
clrSwitch = $('span#clr-swtch'),
timer = 7000;
spectrum();
function spectrum() {
var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
frontPanel.animate({backgroundColor: hue}, timer);
clrSwitch.animate({color: hue}, timer);
};
});
【问题讨论】:
-
两个元素的颜色必须始终相同,或者每个元素的颜色都可以不同?
-
对不起@LGVentura 我现在才看到你的评论。两个元素的颜色必须始终相同,是的。我同时更新了我的问题中的代码,你还有什么要补充的吗?
标签: jquery css optimization jquery-animate