我想弄清楚如何使用CSS和Javascript更改我的網站背景季節。它似乎應該很容易,但我只是沒有得到它的任何地方。下面是我嘗試,它不工作(當然,對吧?):使用CSS和Javascript按季節更改背景
<script>
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var total = month;
// Summer
if (total >= 6 && total <= 8)
{
var season = "summer.jpg";
}
// Autumn
else if (total >= 9 && total <= 11)
{
var season = "fall.jpg";
}
// Winter
else if (total == 12 || total == 1 || total == 2)
{
var season = "winter.jpg";
}
// Spring
else if (total >= 2 && total <= 6)
{
var season = "spring.jpg";
}
else
{
var season = "summer.jpg";
}
</script>
<style type="text/css">
#maincontent{
position: fixed;
left: 200px; /*Set left value to WidthOfLeftFrameDiv*/
top: 0px; /*Set top value to HeightOfTopFrameDiv*/
right: 0;
bottom: 0;
overflow: auto;
background-image: url('season'); /*Note: I tried putting season in <scirpt> tags too with no success */
</style>
那麼,無論「季節」如何,圖像是不是顯示出來?或者它顯示出來了,但不會隨季節變化? – user1289451
您無法使用javascript更改background-image屬性。我建議你爲每個賽季創建4個不同的課程,並根據季節改變你的邏輯以添加正確的課程。 – Eddi
是的,你可以.... – felix91