如何使用JavaScript根据季节更改CSS
如果这是一个简单的问题,我很抱歉,但我需要这方面的帮助。我正在尝试创建一个“动态”页面,它会随着每个季节(基于南半球季节)改变页面/站点的外观。我在CSS中设置了颜色、标题、图像和背景,希望在夏天、冬天、秋天和春天时进行更改,并为圣诞节和新年增加了一项功能。我已经包含了我页面中的内嵌JavaScript,以及与CSS相关的脚本。有人能帮忙吗,因为它不工作如何使用JavaScript根据季节更改CSS,javascript,css,html,dynamic,Javascript,Css,Html,Dynamic,如果这是一个简单的问题,我很抱歉,但我需要这方面的帮助。我正在尝试创建一个“动态”页面,它会随着每个季节(基于南半球季节)改变页面/站点的外观。我在CSS中设置了颜色、标题、图像和背景,希望在夏天、冬天、秋天和春天时进行更改,并为圣诞节和新年增加了一项功能。我已经包含了我页面中的内嵌JavaScript,以及与CSS相关的脚本。有人能帮忙吗,因为它不工作 <script> // JavaScript Document $(document).ready
<script>
// JavaScript Document
$(document).ready(function(){
var d = new Date();
var m = d.getMonth();
var n = d.getDate();
if ( m >= 3 && m <= 5 )
(
// If date is between March and May inclusive, apply autumn theme to ‘body’
document.body.className = "autumn";
)
else if ( m >= 6 && m <= 8 )
(
// If date is between June and August inclusive, apply winter theme to ‘body’
document.body.className = "winter";
)
else if ( m >= 9 && m <= 11 )
(
// If date is between September and November inclusive, apply spring theme to ‘body’
document.body.className = "spring";
)
else if( m >= 12 && m <= 2 )
(
// All other times apply the 'Summer' theme to 'body'
document.body.className = "summer";
if ( m = 12 && n >= 13 && n <= 27 )
(
// If date is between 13/12 and 27/12 inclusive, apply Christmas theme to ‘body’
document.body.className = "xmas";
)
else if ( m = 12 && n >= 28 && n <= 31 )
(
// If Date is between 28/12 and 31/12 New Year's theme to ‘body’
document.body.className = "nye";
)
else if ( m = 1 && n >= 1 && n <= 4 )
(
// If Date is between 28/12 and 31/12 New Year's theme to ‘body’
document.body.className = "nye";
)
)
});
</script>
很抱歉,这篇文章太长了。
m>=12&&m您能否尝试从
if ( m >= 3 && m <= 5 )
(
/* your code */
)else if(...)
(
/* your code */
)
在这个月的情况下,我认为你的情况是错误的
else if( m >= 12 && m <= 2 )
else如果(m>=12&&m请注意,月份是零索引的,因此一月是0,二月1,依此类推,十二月是11。非常容易修复,只需将1添加到getMonth返回的月数中即可
代码可以大大简化,因为您实际上不需要else部分。每个if返回一个离散值,因此只需使用ifs序列。此外,可以通过将月数除以3并使用模数运算符来确定季节:
// Return the southern hemisphere season for a date
// If no date provided, uses current system date
function getSeason(d) {
d = d || new Date();
var mon = d.getMonth() + 1; // Adjust to be indexed from 1
var day = d.getDate();
var seasons = ['summer','autumn','winter','spring'];
// Do silly seasons here
if (mon == 12) {
if (day >= 13 && day <= 27) {
return 'xmas';
}
if (day >= 28 && day <= 31) {
return 'nye';
}
}
// If wasn't a silly season, do others
mon = Math.floor((mon % 12) / 3);
return seasons[mon];
}
console.log('Currently it is ' + getSeason()); // Currently it is summer
请您详细介绍一下“不工作!!!”:)告诉我们什么是不工作的-您会遇到什么错误,您希望看到什么,等等?月份是零索引的,包括6月到8月都是(m>=5&&m RobG-谢谢,但我有一个问题…它在IE8中不起作用。听起来很奇怪,我需要它在这个浏览器中工作。在IE10和Chrome中工作正常,但不是IE8。有什么建议吗?不确定“它”是什么是的,我已经在IE 6中测试了上述功能,它可以工作。它没有使用任何ES5功能,所以应该可以在IE/NN4之后的每个浏览器中工作。“它”是代码:)不过再次感谢。这是一个很大的帮助!我目前正在学习一门在线Web设计与开发课程,我想让它成为“动态的”随季节变化的页面/站点。季节并不总是从每月的第一天开始?@Kokodoko算法的一部分是OPs,我不一定同意它。:-)
if ( m >= 3 && m <= 5 )
{
/* your code */
}else if(...)
{
/* your code */
}
var m = d.getMonth() + 1;
else if( m >= 12 && m <= 2 )
else if( m >= 12 || m <= 2 )
// Return the southern hemisphere season for a date
// If no date provided, uses current system date
function getSeason(d) {
d = d || new Date();
var mon = d.getMonth() + 1; // Adjust to be indexed from 1
var day = d.getDate();
var seasons = ['summer','autumn','winter','spring'];
// Do silly seasons here
if (mon == 12) {
if (day >= 13 && day <= 27) {
return 'xmas';
}
if (day >= 28 && day <= 31) {
return 'nye';
}
}
// If wasn't a silly season, do others
mon = Math.floor((mon % 12) / 3);
return seasons[mon];
}
console.log('Currently it is ' + getSeason()); // Currently it is summer
$(document).ready(function(){
document.body.className = getSeason();
});