需要为JavaScript图像随机化脚本设置样式规则
我试图将背景图像设置为固定,并拉伸以适合屏幕。我正在使用的js在每次页面加载时切换背景图像 头需要为JavaScript图像随机化脚本设置样式规则,javascript,css,Javascript,Css,我试图将背景图像设置为固定,并拉伸以适合屏幕。我正在使用的js在每次页面加载时切换背景图像 头 身体 inum需要设置为1以外的值。可以将其设置为阵列中的图像数 此外,数组以索引0开头,因此为了更安全,应该相应地为数组编制索引 <script language="JavaScript"> // setup your image array var images = new Array; images[0] = "firstImage.jpg"; ... images[
身体
inum
需要设置为1以外的值。可以将其设置为阵列中的图像数
此外,数组以索引0开头,因此为了更安全,应该相应地为数组编制索引
<script language="JavaScript">
// setup your image array
var images = new Array;
images[0] = "firstImage.jpg";
...
images[4] = "lastImage.jpg";
// alternative image array setup
var images = [ 'firstImage.jpg', 'secondImage.jpg', ... ,'lastImage.jpg' ];
// check to see if the image list is empty (if it's getting built somewhere else)
if (images.length) {
// set inum
var inum = images.length;
var randnum = Math.random();
var randIndex = Math.floor(randnum * inum);
// Ensure you have an array item for every image you are using.
var imageFile;
if (randIndex < images.length) {
imageFile = images[randIndex];
}
...
</script>
//设置图像阵列
var图像=新数组;
图像[0]=“firstImage.jpg”;
...
图像[4]=“lastImage.jpg”;
//替代图像阵列设置
var images=['firstImage.jpg','secondImage.jpg',…,'lastImage.jpg'];
//检查图像列表是否为空(如果它是在其他地方生成的)
如果(图像长度){
//安眠药
var inum=images.length;
var randnum=Math.random();
var randIndex=数学地板(randnum*inum);
//确保使用的每个图像都有一个数组项。
var图像文件;
if(随机索引
体内
<script type="text/javascript">
window.onload = function() {
if (imageFile) {
var body = document.getElementsByTagName("body")[0];
if (body) { body.setAttribute('style',"background:url('"+imageFile+"'); text: white;"); }
}
}
</script>
window.onload=函数(){
如果(图像文件){
var body=document.getElementsByTagName(“body”)[0];
if(body){body.setAttribute('style',“background:url('“+imageFile+”);text:white;”);}
}
}
简单,将两个脚本更改为:
<script type="text/javascript">
var images = [ 'http://tinyurl.com/qhhyb8k'
, 'http://tinyurl.com/nqw2t9b'
, 'http://tinyurl.com/nkogvoq'
// , 'url 4'
]
, image = images[ (Math.random() * images.length)|0 ]
; //end vars
window.onload=function(){
document.body.style.background=
"url('"+image+"') no-repeat center center fixed";
document.body.style.backgroundSize=
"cover"; // width% height% | cover | contain
};
</script>
var images=['http://tinyurl.com/qhhyb8k'
, 'http://tinyurl.com/nqw2t9b'
, 'http://tinyurl.com/nkogvoq'
//“url 4”
]
,image=images[(Math.random()*images.length)|0]
;//结束变量
window.onload=function(){
document.body.style.background=
“url(“+image+”)无重复中心已修复”;
document.body.style.backgroundSize=
“封面”;//宽度%高度%|封面|包含
};
无需更多配置,只需添加/删除/更改URL即可
例如
希望这有帮助。我会检查这两个问题:我尝试了2个链接,但我可以通过seam设置图像的id。这会破坏脚本。我故意设置数字,因为我只添加了1个图像。我需要帮助设置图像属性,以便图像延伸到屏幕并得到修复。我已经尝试了背景图像css样式ng.你的脚本比我的脚本简单得多,所以我可能会切换到它哎哟,完全错过了。我相应地更新了我的答案。在我的辩护中..狡猾的javascript比你本质上简单的问题“如何居中背景图像”吸引了更多的注意力。-1你测试过这个吗?randnum=undefined
,undefined*(inum-1)=NaN
,Math.round(NaN)=NaN
,NaN+1=NaN
,因此rand1=NaN
。因此images[NaN]
是undefined
。看到问题了吗?还有..您的随机数逻辑是错误的,例如:如果数组长度是1..那么您就执行random*(1-1)=>0
,添加1后您将请求数组[1] 应该是数组[0]。。。。
<script type="text/javascript">
window.onload = function() {
if (imageFile) {
var body = document.getElementsByTagName("body")[0];
if (body) { body.setAttribute('style',"background:url('"+imageFile+"'); text: white;"); }
}
}
</script>
<script type="text/javascript">
var images = [ 'http://tinyurl.com/qhhyb8k'
, 'http://tinyurl.com/nqw2t9b'
, 'http://tinyurl.com/nkogvoq'
// , 'url 4'
]
, image = images[ (Math.random() * images.length)|0 ]
; //end vars
window.onload=function(){
document.body.style.background=
"url('"+image+"') no-repeat center center fixed";
document.body.style.backgroundSize=
"cover"; // width% height% | cover | contain
};
</script>