【问题标题】:Optimizing JQuery color changes script优化 JQuery 颜色更改脚本
【发布时间】: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


【解决方案1】:

减少 DOM 查找次数

每次运行此代码时,您都在查找相同的 dom 元素。您可以将 jQuery 对象存储在函数外部的变量中以提高 cpu-sage。

jQuery(document).ready(function ($) {

    setInterval(function () {
        spectrum()
    }, 5000);

    var frontPanel = $('div#front-panel-bkgr'),
        clrSwitch = $('span#clr-swtch');

    function spectrum() {
        var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
        frontPanel.animate({backgroundColor: hue}, 7000);
        clrSwitch.animate({color: hue}, 7000);
    };
});

时间

您的动画比setInterval 调用该函数的时间长了两秒钟。对于第一个或两个循环,您可能没问题,但最终您将尝试将元素同时更改为两种或三种颜色。删除 setInterval 并将函数作为回调添加到动画函数之一。此外,如果您希望两个动画的运行时间相同,请将7000 替换为变量。这将允许您在开发期间仅在一个地方更改值并同时运行两个规则:

jQuery(document).ready(function ($) {

    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, spectrum());
    };
});

[编辑]

因为回调是自引用的,所以会有一个无限循环。我认为浏览器(或者可能是 jQuery?)已经解决了这个问题并抛出了范围错误。为了解决这个问题,将回调包装在一个匿名函数中:

        clrSwitch.animate({color: hue}, timer, function() {spectrum();});

【讨论】:

  • 添加频谱()函数作为回调(参见计时)在控制台中给了我一个 RangeError。我确实开始在您的第一个示例中添加计时器作为变量。保持 setInterval 也会使颜色按预期循环。用代码更新问题。如果您有任何意见或其他信息,请拍-cheers
  • 回调是自引用的,所以浏览器会抛出异常。将回调包装在匿名函数中以解决此问题。我已经更新了我的答案。
【解决方案2】:

1) 如果#content h2#colophon > div 中只有一个跨度,则向它们添加 id

2) 你正在使用双$(document).ready(),只使用第一个,并将函数谱从$(document).ready移开

3) .front-panel-heading - 不能用 id 代替吗?

【讨论】:

  • 从您的回复中我更新了我的 JS 代码。我注意到当我离开网站时,我的笔记本电脑风扇并没有发疯,如果这可以被视为改进的迹象的话。但是,您能否向我澄清以下内容:在第 1 点和第 3 点中,您是否建议我应该尝试以 ID 而不是类为目标,以获得更好的性能作为一般规则?第2点:我不完全确定你的意思。您能否验证上述问题中的更新代码?
  • @cristovaov 是的,如果可能,对单个元素使用 ID 而不是类。更新的代码删除了双 $(document).ready。最后 - 您的笔记本电脑风扇并不是网站 js 使用情况的指标。我不认为您的网页中有这么多元素和循环,会占用大量 CPU/GPU。
  • 感谢您的所有意见,它让我走上了正轨。至于风扇,称我为迷信者:在此之前,chrome 似乎确实超载了,让我的风扇变得更加努力......
猜你喜欢
  • 1970-01-01
  • 2016-11-07
  • 2012-12-02
  • 1970-01-01
  • 2011-02-25
  • 2020-07-30
  • 1970-01-01
  • 2014-04-29
  • 2011-11-12
相关资源
最近更新 更多