[英]Change background-image of a class dynamically on a date using javascript/Jquery?
我正在嘗試根據季節更改主頁的背景圖像,例如從 spring.jpg 更改為 summer.jpg。 到目前為止我所寫的,
var currentDate=="12/01/2020";
if (currentDate==True){
document.body.background = "spring.jpg";
}else{
document.body.background = "summer.jpg";
}
只是想知道我離我有多遠,如果這是可能的
請像下面提到的那樣使用它,它對你有用。
var currentDate == "12/01/2020";
if (currentDate == True) {
document.body.style.backgroundImage = "url('spring.jpg')";
}else {
document.body.style.backgroundImage = "url('summer.jpg')";
}
是的,這是可能的。
我認為
定義季節持續時間
const SEASONS = { SPRING: {fromDate - toDate}, SUMMER: {fromDate - toDate}, ... }
檢查today
是什么季節(開關 - 案例),然后通過以下方式應用圖像:
document.body.style.backgroundImage = "url('season.png')";
參考 這個鏈接
定義季節的方法有很多種,但對於簡單的以 3 個月定義季節的方法,我們可以使用 @omgaz 在Javascript 編碼中給出的簡單公式: 輸入特定日期,Output 季節
const getSeason = d => Math.floor((d.getMonth() / 12 * 4)) % 4 let IamInTheNorthernHemisphere = true; //change to false if you are in the Southern hemisphere let seasons = IamInTheNorthernHemisphere? ['Winter', 'Spring', 'Summer', 'Autumn']: ['Summer', 'Autumn', 'Winter', 'Spring']; document.body.style.backgroundImage = "url(" + seasons[getSeason(new Date())] + ".jpg)"; alert(document.body.style.backgroundImage);
<body> </body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.