用于彩色动画的jQuery Math - 特定颜色范围

mar*_*ark 4 math jquery animation colors

我在悬停时使用这个数学作为bg颜色动画:

var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
Run Code Online (Sandbox Code Playgroud)

它产生随机的rgb颜色.非常好,但我寻找不同的东西.

有没有人知道一个好的数学,我可以使用只有一定颜色范围的颜色范围,如红色范围或绿色?

任何帮助表示赞赏.

@Avinash,这是我现在使用它的完整代码.我如何包含您的解决方案?

$(document).ready(function () {
    //bg color animation
    original = $('.item,.main-menu-button').css('background-color');
    $('.item,.main-menu-button').hover(function () { //mouseover
        var col = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')'; //random hover color
        $(this).stop().animate({
            'backgroundColor': col
        }, 1000);
    }, function () { //mouseout
        $(this).stop().animate({
            'backgroundColor': '#111'
        }, 500); //original color as in css
    });
});
Run Code Online (Sandbox Code Playgroud)



它不起作用.我最好把它留下来.感谢大家的帮助.

小智 6

要生成范围内的随机数,我们需要做一些事情

minValue + random Number * (maxValue - minValue)
Run Code Online (Sandbox Code Playgroud)

即,如果你想创建一个介于100200之间的随机数,我们应该这样做

var rand = 100 + Math.floor(Math.random() * (200 - 100));
Run Code Online (Sandbox Code Playgroud)

它给出了100到200范围内的随机数

使用这个基本规则,我们可以从给定范围生成随机颜色

var range = [{0 : 150,1 : 200}, {0 : 200,1 : 230},{0 : 10,1 : 20}];
function rgb() {
  var color  ='rgb(';
  for(var i = 0; i< 3; i++) {
    color += rand(range[i]['0'], range[i]['1']) + ',';
  }
  return color.replace(/\,$/,')')
}

function rand(min, max) {
    return min + Math.floor(Math.random() * (max - min));
}

alert(rgb());
Run Code Online (Sandbox Code Playgroud)

试试这个代码http://jsbin.com/tuday

编辑:

$(function() {
    var cache = $('#hover').css('background-color');
    $('#hover').hover(function() {
        $(this).css({'background-color' : rgb() });
    },function() {
        $(this).css({'background-color' : cache });
    });
});
Run Code Online (Sandbox Code Playgroud)

示例:http://jsbin.com/iwovew