仅当日期在 12 月 15 日至明年 1 月 8 日之间时,我才会运行 .js 脚本,因此该脚本将运行 25 天。
我的代码是:
<script type="text/javascript">
var now = new Date();
var day = now.getDate(); // 0-31 Days
var month = now.getMonth()+1; // 0-11 Months
var startDate = new Date();
startDate.setDate(15);
startDate.setMonth(11);
var endDate = new Date();
endDate.setDate(8);
endDate.setMonth(0);
if (startDate >= now && endDate <= now) {
snowFall.snow(document.body);
}
.js 文件位于 <script src="js/snowfall.js"></script>
代码有什么问题吗?
我的html/javascript代码水平很差,我是新手
感谢您的帮助!!!
请您参考如下方法:
只需更改您的条件:
if (startDate >= now && endDate <= now)
至
if (startDate <= now || endDate >= now)
这是有效的,因为日期始终是同一年,所以 startDate <= now
工作范围 15.11.2017 - 31.12.2017
和endDate >= now
将在 1.1.2018 - 31.1.2018
之间工作