php随机调用百度背景图片,用JavaScript实现随机提取指定的图片作为网页背景

博客内容讲述了如何使用JavaScript动态设置网页背景图,通过从预设的images/background目录下的六张图片中随机选取一张作为背景,并讨论了初始代码存在的问题及改进方案,包括将JS代码置于HTML底部以确保DOM加载完成,以及对不同浏览器兼容性的考虑。
摘要由CSDN通过智能技术生成

其实这个功能几个月前都实现了,但是写法有些问题,打算修改下,换个写法。

思路

提取网站下的images里的background文件夹里6张图里的随机取一张图,作为网页背景。

js直接放在了header.php尾部。

用在自己网站的代码

bg_img= ['<?php $this->options->themeUrl('images/background/1.jpg'); ?>',

'<?php $this->options->themeUrl('images/background/2.jpg'); ?>',

'<?php $this->options->themeUrl('images/background/3.jpg'); ?>',

'<?php $this->options->themeUrl('images/background/4.jpg'); ?>',

'<?php $this->options->themeUrl('images/background/5.jpg'); ?>',

'<?php $this->options->themeUrl('images/background/6.jpg'); ?>']; //调用图片路径(我网站模板是用的themeUrl函数获取当前模板路径,根据实际情况决定怎么写)

document.getElementsByTagName("body")[0].style.background = "url("+bg_img[Math.floor(Math.random()*(bg_img.length))]+")";//随机背景图

document.getElementsByTagName("body")[0].style.backgroundPosition = "center";

document.getElementsByTagName("body")[0].style.backgroundRepeat = "no-repeat";

document.getElementsByTagName("body")[0].style.backgroundAttachment = "fixed";

不用php也可以这样写

bg_img = ["http://xxxx/1.jpg",

"http://xxx/2.jpg",

"http://xxx/3.jpg",

"http://xxx/4.jpg",

"http://xxx/5.jpg",

"http://xxx/6.jpg"]; //调用图片地址/路径(改为自己的)

document.getElementsByTagName("body")[0].style.background = "url("+bg_img[Math.floor(Math.random()*(bg_img.length))]+")";//随机背景图

document.getElementsByTagName("body")[0].style.backgroundPosition = "center";

document.getElementsByTagName("body")[0].style.backgroundRepeat = "no-repeat";

document.getElementsByTagName("body")[0].style.backgroundAttachment = "fixed";

写的时候遇到的问题

开始错用了document.body ,发现这个除了IE,其他浏览器都不认。

改为了document.getElementsByTagName。

开始为了图方便 把JS文件放到头部了。导致执行这个js的时候,body还没有出现的情况,一般js要放在html末尾(

后)

关于getElementsByTagName()

getElementsByTagName() 方法可返回带有指定标签名的对象的集合。

语法document.getElementsByTagName(tagname)

返回值

类型:NodeList 对象,描述:指定标签名的元素集合

浏览器支持

所有主要浏览器都支持 getElementsByTagName() 方法

好的,以下是使用JavaScript实现按时间随机变化的网页图片背景的完整HTML代码: ``` <!DOCTYPE html> <html> <head> <title>按时间随机变化的网页图片背景</title> <style> body { background-size: cover; background-position: center; background-repeat: no-repeat; background-image: url("default.jpg"); /* 初始背景图片 */ } </style> </head> <body> <script> function changeBackground() { var date = new Date(); // 获取当前时间 var hours = date.getHours(); // 获取当前小时数 var body = document.getElementsByTagName("body")[0]; // 获取body元素 if (hours >= 6 && hours < 12) { // 6:00-12:00 body.style.backgroundImage = "url('morning.jpg')"; // 早上的图片 } else if (hours >= 12 && hours < 18) { // 12:00-18:00 body.style.backgroundImage = "url('afternoon.jpg')"; // 下午的图片 } else if (hours >= 18 && hours < 24) { // 18:00-24:00 body.style.backgroundImage = "url('evening.jpg')"; // 晚上的图片 } else { // 0:00-6:00 body.style.backgroundImage = "url('night.jpg')"; // 夜晚的图片 } } changeBackground(); // 初始调用一次 setInterval(changeBackground, 1000); // 每隔1秒调用一次 </script> </body> </html> ``` 此代码会根据当前时间随机变换网页背景图片,每隔1秒调用一次`changeBackground()`函数进行更新。具体实现方式是根据当前小时数判断当前时间段,并设置不同的背景图片。在此代码中,分别设置了早上、下午、晚上、夜晚四张图片,您可以根据需要自行修改。注意,图片路径必须正确,否则无法显示图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值