2012-10-29 29 views
1

我正在嘗試根據一天中的時間更改背景圖片網址。就像在早上的時間太陽升起....太陽落山。每個都有自己的PNG文件和一個網址。我在軌道上使用ruby創建站點,並使用sass-rails作爲css。按時間更改背景圖片網址

所以我的問題,是否有一個聰明的方法來改變從輔助方法或類似的CSS類閱讀bg圖像url?

或者使用Jquery調用服務器端方法並返回圖像url?

任何幫助,非常感謝。

回答

2

嘗試這種方式

<script type="text/javascript"> 

var currentTime = new Date().getHours(); 
if (currentTime> 6 | currentTime<9) { 
if(document.body) { 
document.body.background = "sunrise.jpg"; 
} 
} 
else if (currentTime>=9 &&currentTime < 20) { 
if(document.body) { 
document.body.background = "daytime.jpg"; 
} 
} 
else if (currentTime>20 &&currentTime < 24) { 
if(document.body) { 
document.body.background = "sunset.jpg"; 
} 
} 
else { 
if (document.body) { 
document.body.background = "nighttime.jpg"; 
} 
} 

</script> 

來源:http://www.sitepoint.com/forums/showthread.php?744436-Changing-div-background-image-based-on-time

+0

不要忘了接受的答案,順便說一下:) –

+0

儘管上面的回答是正確的。這是我正在尋找http://stackoverflow.com/questions/8932097/how-to-set-a-variable-from-a-helper-method-for-inclusion-in-a-sass-scss-styleshe – Ducati007