【问题标题】:How do i make snow effect only display when its Christmas?我如何使雪效果仅在圣诞节时显示?
【发布时间】:2015-02-19 08:22:14
【问题描述】:

如何让这个雪景效果只在圣诞节时显示? 我试图将它添加到我的网站,但问题是,我不希望它一直显示,是否有倒计时可以添加到脚本或什么?

var snowsrc="snow.gif"
var no = 10;

var dx, xp, yp;
var am, stx, sty;
var i, doc_width = 800, doc_height = 600;

if (typeof window.innerWidth != 'undefined')
{
   doc_width = window.innerWidth;
   doc_height = window.innerHeight;
}
else
if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
{
   doc_width = document.documentElement.clientWidth;
   doc_height = document.documentElement.clientHeight;
}
else
{
   doc_width = document.getElementsByTagName('body')[0].clientWidth;
   doc_height = document.getElementsByTagName('body')[0].clientHeight;
}

dx = new Array();
xp = new Array();
yp = new Array();
am = new Array();
stx = new Array();
sty = new Array();

for (i = 0; i < no; ++ i)
{
   dx[i] = 0;
   xp[i] = Math.random()*(doc_width-50);
   yp[i] = Math.random()*doc_height;
   am[i] = Math.random()*20;
   stx[i] = 0.02 + Math.random()/10;
   sty[i] = 0.7 + Math.random();

   if (i == 0)
   {
      document.write("<div id=\"dot"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"; VISIBILITY: visible; TOP: 15px; LEFT: 15px;\"><a href=\"/\"><img src='"+snowsrc+"' border=\"0\"><\/a><\/div>");
   }
   else
   {
      document.write("<div id=\"dot"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"; VISIBILITY: visible; TOP: 15px; LEFT: 15px;\"><img src='"+snowsrc+"' border=\"0\"><\/div>");
   }
}

function DoSnow()
{
   for (i = 0; i < no; ++ i)
   {
      yp[i] += sty[i];
      if (yp[i] > doc_height-50)
      {
         xp[i] = Math.random()*(doc_width-am[i]-30);
         yp[i] = 0;
         stx[i] = 0.02 + Math.random()/10;
         sty[i] = 0.7 + Math.random();
      }
      dx[i] += stx[i];
      document.getElementById("dot"+i).style.top=yp[i]+"px";
      document.getElementById("dot"+i).style.left=xp[i] + am[i]*Math.sin(dx[i])+"px";
   }
   snowtimer=setTimeout("DoSnow()", 10);
}

setTimeout("DoSnow()", 500);

【问题讨论】:

  • 查看月份是否为 12 月,日期是否为 25 日?

标签: javascript


【解决方案1】:

This 是一个很好的帖子,可以准确地解释您在寻找什么。该帖子的摘录:

function scheduleMessage() {
var today=new Date()

//compute the date you wish to show the message
var christmas=new Date(today.getFullYear(), 11, 25)
if (today.getMonth()==11 && today.getDate()>25)
    christmas.setFullYear(christmas.getFullYear()+1)

var timeout = christmas.getTime()-today.getTime();
if( timeout > 2147483647 ){
    window.setTimeout( scheduleMessage(), 2147483647 )
} else {
    window.setTimeout(function() {alert('Ho Ho Ho!'); scheduleMessage()}, timeout)
}

【讨论】:

  • 我还是个JS的菜鸟,我应该把这段代码放在雪地效果里
猜你喜欢
  • 2021-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-28
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
相关资源
最近更新 更多