jQuery从字符串数组中选择一个随机值

Ale*_*lex 3 javascript arrays random jquery

我知道在PHP中这是怎么做的,但在javascript数组中很奇怪.

所以我有一组图像过渡,其效果使用缓动方程.对于某些效果,我想从多个值的数组中选取一个随机值:

就像是:

easing: randomFrom(array('easeOutElastic', 'easeOutBounce', 'easeOutSince')),

Dog*_*ert 10

function randomFrom(array) {
  return array[Math.floor(Math.random() * array.length)];
}

// in your code
easing: randomFrom(['easeOutElastic', 'easeOutBounce', 'easeOutSince']),
Run Code Online (Sandbox Code Playgroud)


Nea*_*eal 5

试试这个:

function randomFrom(arr){
    var randomIndex = Math.floor(Math.random() * arr.length);
    return arr[randomIndex];
}
Run Code Online (Sandbox Code Playgroud)