欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS实现520 表白简单代码

程序员文章站 2022-05-26 13:32:07
这两天不知道咋了,迷迷糊糊的,突然知道今天是520的我,急急忙忙赶出个程序(新手,代码有点乱),发出来大家一起研究下(参考百度的)。

这两天不知道咋了,迷迷糊糊的,突然知道今天是520的我,急急忙忙赶出个程序(新手,代码有点乱),发出来大家一起研究下(参考百度的)。

<!doctype html>
<html>
 <head>
  <meta charset="utf-8">
  <title>520</title>
  <style>
  html, body{padding:0px; margin:0px; background:#222; font-family: 'karla', sans-serif; color:#fff; height:100%; overflow:hidden;}
  canvas {width:100%; height:100%;}
  #text,#text_520{font-family:'楷体'; color:rgb(255,255,3); font-size:20px; position:fixed; left:10%; top:10%;}
  #text_520{font-size:100px; top:50%; left:50%;}
  img{position:fixed; top:0; left:0; width:100%;}
  #last{font-size:12px; bottom:10px; left:50%; position:fixed;}
  /*
  @keyframes drop {
   0% { 
    transform: translatey(-100px);
    opacity: 0;
   }
   90% {
    opacity: 1;
    transform:translatey(10px);
   }
   100% {
    transform:translatey(0px;)
   }
  }
  */
  </style>
 </head>
 <body>
  <canvas id="c"></canvas>
  <div id="text"></div>
  <div id="text_520">5 2 0</div>
  <img src="./timg.jpg" class="img" />
  <div id="last">版权所有:李晓珂</div>
  <script type="text/javascript" src="./jquery-1.11.0.min.js"></script>
  <script type="text/javascript">
   function isie(){
    var u = navigator.useragent;
    if((u.indexof("compatible") > -1 && u.indexof("msie") > -1) || (u.indexof("trident") > -1 && u.indexof("rv:11.0") > -1)){
     alert("该浏览器暂不支持,请更换浏览器");
     window.open('','_self'); 
     window.close();
    }
    var audio = document.createelement("audio");
     audio.setattribute("src","./520-love.mp3");
     audio.setattribute("autoplay","autoplay");
   }
   isie();
  </script>
  <script type="text/javascript">
    var textarr = [
     'i love three things in this world,',
     'the sun ,the moon and you.',
     'the sun for the day,',
     'the moon for the night,',
     'and you forever!',
     '',
     'if you were a teardrop,',
     'in my eye,',
     'for fear of losing you,',
     'i would never cry.',
     'and if the golden sun,',
     'should cease to shine its light,',
     'just one smile from you,',
     'would make my whole world bright.'
    ];
    var text_520 = document.getelementbyid('text_520');
    var height = (window.innerheight - text_520.offsetheight) / 2;
    var width = (window.innerwidth - text_520.offsetwidth) / 2;
    text_520.style.top = height + 'px';
    text_520.style.left = width + 'px';
    $('#text_520').hide();
    $('.img').hide();
    var m = 0;
    var n = 0;
    var text = document.getelementbyid('text');
    function typing(){
     if(m <= textarr[n].length) {
      text.innerhtml = text.innerhtml.substring(0,text.innerhtml.length-1) + textarr[n].substr(m++,1) + '_';
      settimeout(typing,250);
     }else {
      if(n < textarr.length-1){
       text.innerhtml = text.innerhtml.substring(0,text.innerhtml.length-1) + "<br />_";
       n++;
       m = 0;
       typing();
      }else {
       text.innerhtml = text.innerhtml.substring(0,text.innerhtml.length-1);
       $('#text').fadeout(5000);
       settimeout(function(){$('#text_520').fadein(5000);},7000);
       settimeout(function(){$('#text_520').fadeout(5000); },7000);
       settimeout(function(){$('.img').fadein(50000);},15000)
      }
     }
    }
    settimeout(typing,5000);
   var ctx = document.queryselector('canvas').getcontext('2d');
    ctx.canvas.width = window.innerwidth;
    ctx.canvas.height = window.innerheight;
   var sparks = [];
   var fireworks = [];
   var walker;
   fireworks.pop();
   var i = 10;
   while(i--) fireworks.push(new firework(math.random()*window.innerwidth, window.innerheight*math.random()));
   // setinterval(render, 1000/50);
   render();
   function render() {
    settimeout(render, 1000/50);
    ctx.fillstyle = 'rgba(0, 0, 0, 0.2)';
    ctx.fillrect(0, 0, ctx.canvas.width, ctx.canvas.height);
    // 上升效果
    for(var firework of fireworks) {
     if(firework.dead) continue;
     firework.move();
     firework.draw();
    }
    // 绽放效果
    for(var spark of sparks) {
     if(spark.dead) continue;
     spark.move();
     spark.draw();
    }
    if(math.random() < 0.1) fireworks.push(new firework());
    //ctx.height = ctx.height;
   }
   function spark(x, y, color) {
    this.x = x;
    this.y = y;
    this.dir = math.random() * (math.pi*2);
    this.dead = false;
    this.color = color;
    this.speed = math.random() * 3 + 3;
    walker = new walker({ radius: 20, speed: 0.25 });
    this.gravity = 0.25;
    this.dur = this.speed / 0.15;
    this.move = function() {
     this.dur--;
     if(this.dur < 0) this.dead = true;
     if(this.speed < 0) return;
     if(this.speed > 0) this.speed -= 0.15;
     walk = walker.step();
     this.x += math.cos(this.dir + walk) * this.speed;
     this.y += math.sin(this.dir + walk) * this.speed;
     this.y += this.gravity;
     this.gravity += 0.05;
    }
    this.draw = function() {
     drawcircle(this.x, this.y, 2, this.color);
    }
   }
   function firework(x, y) {
    this.xmove = math.random()*2 - 1;
    this.x = x || math.random() * ctx.canvas.width;
    this.y = y || ctx.canvas.height;
    this.height = math.random()*ctx.canvas.height/2;
    this.dead = false;
    this.color = randomcolor();
    this.move = function() {
     this.x += this.xmove;
     if(this.y > this.height) this.y -= 4; 
     else this.burst();
    }
    this.draw = function() {
     drawcircle(this.x, this.y, 3, this.color)
    }
    this.burst = function() {
     this.dead = true
     i = 100; while(i--) sparks.push(new spark(this.x, this.y, this.color));
     sparks.pop();
    }
   }
   settimeout(function (){window.open('','_self').close();},175000);
/*
   // 清除两个数组
   function clear(){
    if(sparks!=null || fireworks!=null){
     sparks.pop();
     fireworks.pop();
    }
    var sparks = [];
    var fireworks = [];
   }
   setinterval(clear,100);
    */
   function drawcircle(x, y, radius, color) {
    color = color || '#fff';
    ctx.fillstyle = color;
    ctx.fillrect(x-radius/2, y-radius/2, radius, radius);
   }
   function randomcolor(){
    return ['#6ae5ab','#88e3b2','#36b89b','#7bd7ec','#66cbe1'][math.floor(math.random() * 5)];
   }
   function walker(options){
    this.step = function(){
     this.direction = math.sign(this.target) * this.speed
     this.value += this.direction
     this.target
      ? this.target -= this.direction
      : (this.value)
      ? (this.wander) 
       ? this.target = this.newtarget() 
       : this.target = -this.value
      : this.target = this.newtarget() 
     return this.direction
    }
    this.newtarget = function() {
     return math.round(math.random()*(this.radius*2)-this.radius)
    }
    this.start = 0
    this.value = 0
    this.radius = options.radius
    this.target = this.newtarget()
    this.direction = math.sign(this.target)
    this.wander = options.wander
    this.speed = options.speed || 1
   }
  </script>
 </body>
</html>

总结

以上所述是小编给大家介绍的js实现520 简单表白代码,希望对大家有所帮助