Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何随机设置背景_Javascript_Jquery_Css - Fatal编程技术网

Javascript 如何随机设置背景

Javascript 如何随机设置背景,javascript,jquery,css,Javascript,Jquery,Css,我正在使用来自的天线模板,并试图从一组图像中随机选择滚动背景。我将如何做到这一点 当前,背景设置在如下代码块中: #bg { background: #348cb2 url("images/bg.jpg") bottom left; background-repeat: repeat-x; } 我发现这个StackOverflow问题可能就是我想要的,但我不知道如何实现它 这是一个开始:vararr=['bg1.jpg','bg2.jpg']$css({backgrou

我正在使用来自的天线模板,并试图从一组图像中随机选择滚动背景。我将如何做到这一点

当前,背景设置在如下代码块中:

#bg {
    background: #348cb2 url("images/bg.jpg") bottom left;
    background-repeat: repeat-x;
    }

我发现这个StackOverflow问题可能就是我想要的,但我不知道如何实现它

这是一个开始:
vararr=['bg1.jpg','bg2.jpg']$css({backgroundImage:arr[(Math.random()*arr.length).toFixed(0)])@RienNeVaPlu͢s通过将您的一些代码与另一个StackOverflow答案相结合,我让它开始工作了!谢谢
$(document).ready(function(){
var images=['https://unsplash.imgix.net/photo-1422405153578-4bd676b19036?q=75&fm=jpg&s=5ecc4c704ea97d85ea550f84a1499228', 'https://unsplash.imgix.net/photo-1423439793616-f2aa4356b37e?q=75&fm=jpg&s=3b42f9c018b2712544debf4d6a4998ff',];
var randomNumber = Math.floor(Math.random() * images.length);
var bgImg = 'url(' + images[randomNumber] + ')';

$('#bg').css({'background':bgImg});