【发布时间】:2012-05-30 17:10:09
【问题描述】:
由于在不同浏览器中让背景延伸到整个页面的问题,我不得不使用带有图像的 div 来覆盖页面。
我想要做的是让这张图片根据一天中的时间而改变。当我在 CSS 中使用更传统的 background-image 属性时,它工作得很好,但是我在翻译它以更改图像的 src 属性时遇到了问题。
图片的id是#bg。我可能会犯语法错误,但任何帮助都会很棒,谢谢!
$('document').ready(function(){
var hours = getDayTime(new Date().getHours());
if (hours > 20 || hours < 5) {
$('#page-background > img').prop('src', 'backgrounds/night.jpg')
}
else if (hours > 17) {
$('#page-background > img').prop('src', 'backgrounds/dusk.jpg')
}
else if (hours > 8) {
$('#page-background > img').prop('src', 'backgrounds/day.jpg')
}
else {
$('#page-background > img').prop('src', 'backgrounds/dawn.jpg')
}
});
<div id="page-background"><img src="backgrounds/day.jpg" width="100%" height="100%" alt="" id="bg" /></div>
【问题讨论】:
-
小时变量在哪里定义?你能把你的整个页面都放上去吗?
-
我现在也添加了相关的 HTML,谢谢。
-
@EmilyTwist
hours定义在哪里?有什么错误吗? -
@ManseUK 我得到了它:
code$('document').ready(function(){ var hours = getDayTime(new Date().getHours()); if ( hours > 20 || hours img').prop('src', 'backgrounds/night.jpg') }code是吗?
标签: jquery function time image attr