如何定期更改整个站点的背景图像?

发布于 2024-07-25 09:45:50 字数 1623 浏览 4 评论 0原文

我正在尝试设置一个随机交替背景,该背景在整个网站上定期变化。 例如,在任何给定点,一段时间内每个页面上的随机背景图像都是相同的。

目前,我的背景图像使用此 javascript 每 30 分钟更改一次:(来自此处:http://www.blogfodder.co.uk/post/2008/01/JavaScript-CSS-Random-Background-Image.aspx)

然而问题是每个链接随机化每个新页面上的背景。

function ChangeCSSBgImg() {
    if (!document.getElementById) return false;

    var MyElement = "bgimg" 
    var ImgPath = "../i/" 

    if (!document.getElementById(MyElement)) return false;

    var random_images = new Array ();
    random_images[0] = "bg1.jpg"; // these are the background images
    random_images[1] = "bg2.jpg"; 
    random_images[2] = "bg3.jpg"; 
    random_images[3] = "bg4.jpg"; 
    random_images[4] = "bg5.jpg"; 

    var $header = document.getElementById(MyElement);
    var $backgroundurl = $header.style.backgroundImage;
    var ImgURL = "url(" + ImgPath + random_images[rand(random_images.length)] + ")";

    if ($backgroundurl != ImgURL) {
        $header.style.backgroundImage = ImgURL; 
    }

    movement = setTimeout("ChangeCSSBgImg()",108000000); 
}


/* random number generator */
function rand(n) {
    return ( Math.floor ( Math.random ( ) * n ) );
}

/* Custom onload function */
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}
/* trigger onload */
addLoadEvent(ChangeCSSBgImg);

I am trying to set up an random alternating background that changes periodically across the whole site. E.g. at any given point the random background image would be the same on every page for a period of time.

Currently I have background images that change every 30 mins with this javascript: (from here: http://www.blogfodder.co.uk/post/2008/01/JavaScript-CSS-Random-Background-Image.aspx)

The problem however is that each link randomizes the background on each new page.

function ChangeCSSBgImg() {
    if (!document.getElementById) return false;

    var MyElement = "bgimg" 
    var ImgPath = "../i/" 

    if (!document.getElementById(MyElement)) return false;

    var random_images = new Array ();
    random_images[0] = "bg1.jpg"; // these are the background images
    random_images[1] = "bg2.jpg"; 
    random_images[2] = "bg3.jpg"; 
    random_images[3] = "bg4.jpg"; 
    random_images[4] = "bg5.jpg"; 

    var $header = document.getElementById(MyElement);
    var $backgroundurl = $header.style.backgroundImage;
    var ImgURL = "url(" + ImgPath + random_images[rand(random_images.length)] + ")";

    if ($backgroundurl != ImgURL) {
        $header.style.backgroundImage = ImgURL; 
    }

    movement = setTimeout("ChangeCSSBgImg()",108000000); 
}


/* random number generator */
function rand(n) {
    return ( Math.floor ( Math.random ( ) * n ) );
}

/* Custom onload function */
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}
/* trigger onload */
addLoadEvent(ChangeCSSBgImg);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无悔心 2024-08-01 09:45:50

你能散列当前时间来选择图像吗? 例如,一个简单的方法是,有六个图像,然后根据当前时间倒数第二个数字选择图像,例如

  • 10:08 - 选择图像 #0
  • 10:15 - 选择图像 #1
  • 10:20 -选择图像 #2
  • 10:51 - 选择图像 #5

Can you hash the current time to pick an image? e.g. a simplistic one would be, have six images, then pick the image based on the 2nd-to-last digit in the current time, e.g.

  • 10:08 - pick image #0
  • 10:15 - pick image #1
  • 10:20 - pick image #2
  • 10:51 - pick image #5
奶茶白久 2024-08-01 09:45:50

我建议您不要为此存储任何信息 - 它应该根据先验信息工作,例如 Jeffrey Kemp 建议的系统时间。 这将使其更加稳定并可以跨更多用户代理工作。

例如,获取 UNIX 时间戳将允许您每秒更改背景。

请记住,某些用户的连接速度较慢,可能无法立即加载背景图像。

一种简单的方法是使用 setInterval 每 30 分钟迭代一次图像数组,并在选择最后一个图像后重置。

I recommend you don't store any info for this - it should work from a priori info, such as the system time as suggested by Jeffrey Kemp. This will make it much more stable and work across more user agents.

Getting a unix time stamp for instance, will allow you to change the background on a per second basis.

Remember that some users have slower connections, and may not load the background image immediately.

A simple way would be to use a setInterval that iterates through an array of images every 30 minutes, and resets after the last image is chosen.

金橙橙 2024-08-01 09:45:50

杰弗里的想法似乎是一个很好且简单的解决方案,除非所有访问者的背景需要完全随机且相同(这需要服务器端解决方案):

使用 javascript 根据时间(小时、分钟或任何)。
然后使用 CSS 设置图像: .background-0 { background-image: ... }

示例:

  • < /code> (00-09 分钟)

  • (10-19 分钟)< /em>

  • (20-29 分钟)

  • (30-39 分钟)

  • (40-49 分钟)

  • (50-59 分钟) >

Jeffrey's idea seems like a good and simple solution for this purpose unless the backgrounds needs to be totally random and identical among all visitors (which would require a server side solution):

Use javascript to set the proper class based on time (hour, minute or whatever).
Then set the image with CSS: .background-0 { background-image: ... }

Example:

  • <div class="background-0"></div> (minutes 00-09)
  • <div class="background-1"></div> (minutes 10-19)
  • <div class="background-2"></div> (minutes 20-29)
  • <div class="background-3"></div> (minutes 30-39)
  • <div class="background-4"></div> (minutes 40-49)
  • <div class="background-5"></div> (minutes 50-59)
大姐,你呐 2024-08-01 09:45:50

我要做的就是在 Web 服务器上以编程方式生成 CSS(即带有 CSS 输出的脚本),或者在生成网页时动态更改样式表链接。

What I would do is either have programmatically generated CSS on the web server (i.e. a script with CSS output) or dynamically change the stylesheet link when generating the web pages.

梦太阳 2024-08-01 09:45:50

您可以将背景图像信息存储在用户会话中并定期在服务器端更新。 这是假设您使用的是在服务器上解析的语言。

You could store the background image info in the user's session and update it server-side periodically. This is assuming you're using a language that is parsed on the server though.

毁梦 2024-08-01 09:45:50

使用 Javascript,你可以做这样的事情。

function changeBg()
    document.body.style.backgroundImage = 'url(bg/'+ new Date().getHours() +'.gif)';
}
    window.setInterval(changeBg, 1000);

将其封装在 Javascript 文件中并将其链接到站点的每个页面。

该脚本将根据时间(即0-23小时)更改网站的背景。

这里,图像存储在bg目录中,名称范围为0代码> 到 <代码>23。 不涉及随机性,因为图像将根据一天中的时间进行选择。 您可以根据需要稍微更改脚本。

Using Javascript, you can do something like this.

function changeBg()
    document.body.style.backgroundImage = 'url(bg/'+ new Date().getHours() +'.gif)';
}
    window.setInterval(changeBg, 1000);

Wrap this in a Javascript file and link it to every page of your site.

This script will change the background of the website, based on the hour of time (ie 0-23 hours)

Here, the images are stored in the bg directory with names ranging from 0 to 23. There is no randomness involved, since the images will be selected based on the hour of the day. You can slightly change the script for your need.

原谅我要高飞 2024-08-01 09:45:50
< script type = "text/javascript" > var now = new Date () ;
var hours = now. getHours() ;
//Keep in code - Written by Computerhope.com
//Place this script in your HTML heading section
document. write( 'It' s now: ', hours, ' < br >< br > ');
document.bgColor="#CC9900";
//18-19 night

if (hours > 17 && hours < 20){
    document.write (' < body style = "background-color: orange" > ');
}
//20-21 night
else if (hours > 19 && hours < 22){
    document.write (' < body style = "background-color: orangered" > ');
}
//22-4 night
else if (hours > 21 || hours < 5){
    document.write (' < body style = "background-color: #C0C0C0;" > ');
}
//9-17 day
else if (hours > 8 && hours < 18){
    document.write (' < body style = "background-color: #616D7E" > ');
}
//7-8 day
else if (hours > 6 && hours < 9){
    document.write (' < body style = "background-color: skyblue" > ');
}
//5-6 day
else if (hours > 4 && hours < 7){
    document.write (' < body style = "background-color: steelblue" > ');
}
else {
    document.write (' < body style = "background-color: white" > ');
}
</script>
< script type = "text/javascript" > var now = new Date () ;
var hours = now. getHours() ;
//Keep in code - Written by Computerhope.com
//Place this script in your HTML heading section
document. write( 'It' s now: ', hours, ' < br >< br > ');
document.bgColor="#CC9900";
//18-19 night

if (hours > 17 && hours < 20){
    document.write (' < body style = "background-color: orange" > ');
}
//20-21 night
else if (hours > 19 && hours < 22){
    document.write (' < body style = "background-color: orangered" > ');
}
//22-4 night
else if (hours > 21 || hours < 5){
    document.write (' < body style = "background-color: #C0C0C0;" > ');
}
//9-17 day
else if (hours > 8 && hours < 18){
    document.write (' < body style = "background-color: #616D7E" > ');
}
//7-8 day
else if (hours > 6 && hours < 9){
    document.write (' < body style = "background-color: skyblue" > ');
}
//5-6 day
else if (hours > 4 && hours < 7){
    document.write (' < body style = "background-color: steelblue" > ');
}
else {
    document.write (' < body style = "background-color: white" > ');
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文