【问题标题】:Constrained random numbers always total the constraint with javascript/jquery受约束的随机数总是与 javascript/jquery 的约束相加
【发布时间】:2013-10-17 00:50:08
【问题描述】:

我是一名 JS 爱好者。我希望随机设置一堆 span 元素的宽度和不透明度来创建动画效果。

目前,每秒使用setInterval 设置和重新设置宽度,这几乎可以正常工作...

$(function () {
  setInterval(function () {
    // Variables for background colour
    var minFloat = 0.3,
        maxFloat = 0.9,
        randFloat = Math.floor(Math.random() * (maxFloat - minFloat + 1)) + minFloat;

    // Set random width
    $('.footerbars span').css('width', Math.random() * 10 + '%');

    // Set random alpha
    $('.footerbars span').css('background-color', 'rgba(58,130,255,' + randFloat + ')');
  }, 1000);
});

我需要的是:

  • 每个跨度的宽度都是不同的百分比,并且所有这些百分比的总和始终为 100%。
  • 每个跨度的背景 Alpha 通道是随机的

任何帮助都很棒!提前致谢

【问题讨论】:

    标签: javascript jquery random width


    【解决方案1】:

    第一个问题是所有的宽度和背景都将设置为相同,因为随机数只生成一次。你需要这样的东西:

    $('.footerbars span').each(function(i, e) {
        $(e).css('width', (Math.random() * 10) + '%')
         .css('background-color', 'rgba('58,130,255,' + ((Math.random() * 0.6) + 0.3) +')');
    });
    

    这样做的问题是宽度加起来可能不是 100%。为了解决这个问题,我们需要首先生成一组随机数,然后对它们进行缩放,使它们相加为 100,然后将它们应用于跨度。

    var numSpans = $('.footerbars span').length;
    var widths = [];
    var total = 0;
    for(var i = 0; i < numSpans; i++) {
        widths[i] = Math.random()+1; // generate a new random width for this span - and make it definitely not zero
        total += widths[i]; // and update the total width so far;
    }
    
    // Now we know what the total random number is (something between numSpans and 2*numSpans)
    // we can scale these so the sum actually is 100
    for(var i = 0; i < numSpans; i++)
        widths[i] = Math.floor(widths[i] * (100 / total));
    

    现在widths[i] 包含.footerbars 中第i 个跨度的% 宽度,因此将代码第一位的第二行修改为:

    $(e).css('width', widths[i])
    

    完整代码:

    var numSpans = $('.footerbars span').length;
    var widths = [];
    var total = 0;
    for(var i = 0; i < numSpans; i++) {
        widths[i] = Math.random()+1; // generate a new random width for this span - and make it definitely not zero
        total += widths[i]; // and update the total width so far;
    }
    
    // Now we know what the total random number is (something between numSpans and 2*numSpans)
    // we can scale these so the sum actually is 100
    for(var i = 0; i < numSpans; i++)
        widths[i] = Math.floor(widths[i] * (100 / total));
    
    $('.footerbars span').each(function(i, e) {
        $(e).css('width', widths[i])
         .css('background-color', 'rgba('58,130,255,' + ((Math.random() * 0.6) + 0.3) +')');
    });
    

    【讨论】:

    • 您的代码出现了一些错误,这样更好吗? $('.footerbars span').each( function(i, e) { e.css('width', (Math.random() * 10) + '%') .css('background-color', 'rgba(58,130,255,' + ((Math.random() * 0.6) + 0.3) + ')'); });
    • 能否添加最终代码作为参考。我在每个间隔都不断收到错误。然而,这些条出现并且以不同的宽度和颜色出现,这是甜蜜的
    猜你喜欢
    • 2013-08-29
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 2016-08-25
    • 1970-01-01
    相关资源
    最近更新 更多