我有一个关于加载基于一天中的时间或日期更改CSS的JavaScript的问题。根据日期或时间的日期或时间更改CSS
问题:如何缓存图像以避免页面间的加载?该脚本工作正常,并根据一天中的时间将该类添加到正文和标题中。但是,当用户点击网站的下一页时,它将重新加载相同的CSS类,并使该网站在加载CSS之前闪烁为白色。
我已将脚本放置在页面底部和标题中。但它仍然在加载脚本的每个页面上闪烁。每当用户从页面转到页面时,是否可以防止脚本加载?
这里是Js代码。
function TimeOfDaySiteChange() {
var d = new Date();
var n = d.getHours();
if (n < 5) {
// midnight
night();
} else if (n > 16 && n < 20) {
// If time is between 5PM – 8PM sunset theme to 'body'
dusk();
} else if (n > 19) {
// If time is 8PM
night();
} else if (n > 8) {
// If time is 9AM
daytime();
} else {
// Else use 'dawn' theme
dawn();
}
}
function dawn() {
jQuery('body').addClass('sunrise_bg');
jQuery('#header_masthead').addClass('sunrise_masthead_bg');
}
function daytime() {
jQuery('body').addClass('day_bg');
jQuery('#header_masthead').addClass('day_masthead_bg');
}
function dusk() {
jQuery('body').addClass('sunset_bg');
jQuery('#header_masthead').addClass('sunset_masthead_bg');
}
function night() {
jQuery('body').addClass('night_bg');
jQuery('#header_masthead').addClass('night_masthead_bg');
}
function init() {
TimeOfDaySiteChange();
}
window.onload = init;
我也尝试过不window.onload
+0
你有没有试过把你的代码放在文档就绪函数中? –
2013-04-29 15:00:09
+1
您不需要向#header_masthead添加类,只需执行body.sunrise_bg #header_masthead {} –
2013-04-29 15:01:41
+0
关于flash;在JS加载之前,运行并最终添加一个类,浏览器将显示默认页面。我会在服务器端添加类。 –
2013-04-29 15:02:55