javascript - 每次单击每个链接或 url 时如何更改我网站的背景图片?

标签 javascript html css background-image

好吧,我在网上看到了很多关于这个的问题,但似乎其中很多都是使用 PHP、ASP、JQUERY 和任何其他语言。对我来说,我对 CSS 和 Java Script 只熟悉一点点,那是我只知道的语言。那么有人可以向我展示一些 CSS 和 Java 脚本代码来解决这个问题吗?

无论如何,我也对每次刷新都会改变的网站背景感兴趣!

提前致谢!

嘿,我找到了示例代码,它运行良好!

感谢本论坛:http://www.daniweb.com/web-development/web-design-html-and-css/threads/228324/load-different-background-image-when-user-refresh

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function changeImg(imgNumber)   {
var myImages = ["Background/Background.jpg", "Background/Background2.jpg", "Background/Background3.jpg", "Background/Background4.jpg", "Background/Background5", "Background/Background6", "Background/Background7"];
var imgShown = document.body.style.backgroundImage;
var newImgNumber =Math.floor(Math.random()*myImages.length);
document.body.style.backgroundImage = 'url('+myImages[newImgNumber]+')';
}
window.onload=changeImg;
</script>
<style type="text/css">
.bg {background-attachment:fixed; background-repeat: no-repeat; background-position:top right;}
</style>
</head>
<body class="bg">
<p>Some text</p>
<!-- put a lot text lines here to see that the background stays fixed. -->
<p>Some text</p>
</body>
</html>

最佳答案

大量的解决方案:

将所有文件存储在一个文件夹中。

  1. 使用索引号为数据库表中的图像编制索引。生成一个特定范围的随机数,并显示随机数对应的图片

  2. 将文件夹中的所有图像重命名为 (1 - n)。生成随机数并附加/。

一旦你得到了随机图像,你就可以很容易地用 javascript 覆盖背景或任何其他 dom 组件

document.getElementByTag('body').setAttribute('style','background-image:url('+<your image>+'));

您也可以使用 jQuery 简化它。

关于javascript - 每次单击每个链接或 url 时如何更改我网站的背景图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13209798/

相关文章:

执行 appendChild 时的 Javascript 额外标记

javascript - Pagespeed 评级低 - 我错过了什么?

c# - session 超时

Python BeautifulSoup 城市抓取并发症

html - CSS 下拉导航导致 html 内容移动

css - 在 python flask 元素中管理 bootstrap 的最佳方法

javascript - 检查 jQuery 页面加载事件是否已经触发

javascript - 正则表达式查找模式并仅替换其中的一部分

javascript - 使用 JQuery 获取日期中最后一天的有效值

javascript - jQM ui-content 100%高度问题