当前位置:   article > 正文

马上跨年了,如何用代码写一个“跨年倒计时”呢?_跨年倒计时代码

跨年倒计时代码

参考 okeyl.com

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>新年快乐</title>
  6. <style>
  7. body{
  8. overflow: hidden;
  9. margin: 0;
  10. }
  11. h1{
  12. position: fixed;
  13. top: 30%;
  14. left: 0;
  15. width: 100%;
  16. text-align: center;
  17. transform:translateY(-50%);
  18. font-family: 'Love Ya Like A Sister', cursive;
  19. font-size: 60px;
  20. color: ff64d9;
  21. padding: 0 20px;
  22. }
  23. h1 span{
  24. position: fixed;
  25. left: 0;
  26. width: 100%;
  27. text-align: center;
  28. margin-top:30px;
  29. font-size:40px;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <h1 id="h1"></h1>
  35. <canvas></canvas>
  36. <script>
  37. var canvas = document.querySelector("canvas"),
  38. ctx = canvas.getContext("2d");
  39. var ww,wh;
  40. function onResize(){
  41. ww = canvas.width = window.innerWidth;
  42. wh = canvas.height = window.innerHeight;
  43. }
  44. ctx.strokeStyle = "red";
  45. ctx.shadowBlur = 25;
  46. ctx.shadowColor = "hsla(0, 100%, 60%,0.5)";
  47. var precision = 100;
  48. var hearts = [];
  49. var mouseMoved = false;
  50. function onMove(e){
  51. mouseMoved = true;
  52. if(e.type === "touchmove"){
  53. hearts.push(new Heart(e.touches[0].clientX, e.touches[0].clientY));
  54. hearts.push(new Heart(e.touches[0].clientX, e.touches[0].clientY));
  55. }
  56. else{
  57. hearts.push(new Heart(e.clientX, e.clientY));
  58. hearts.push(new Heart(e.clientX, e.clientY));
  59. }
  60. }
  61. var Heart = function(x,y){
  62. this.x = x || Math.random()*ww;
  63. this.y = y || Math.random()*wh;
  64. this.size = Math.random()*2 + 1;
  65. this.shadowBlur = Math.random() * 10;
  66. this.speedX = (Math.random()+0.2-0.6) * 8;
  67. this.speedY = (Math.random()+0.2-0.6) * 8;
  68. this.speedSize = Math.random()*0.05 + 0.01;
  69. this.opacity = 1;
  70. this.vertices = [];
  71. for (var i = 0; i < precision; i++) {
  72. var step = (i / precision - 0.5) * (Math.PI * 2);
  73. var vector = {
  74. x : (15 * Math.pow(Math.sin(step), 3)),
  75. y : -(13 * Math.cos(step) - 5 * Math.cos(2 * step) - 2 * Math.cos(3 * step) - Math.cos(4 * step))
  76. }
  77. this.vertices.push(vector);
  78. }
  79. }
  80. Heart.prototype.draw = function(){
  81. this.size -= this.speedSize;
  82. this.x += this.speedX;
  83. this.y += this.speedY;
  84. ctx.save();
  85. ctx.translate(-1000,this.y);
  86. ctx.scale(this.size, this.size);
  87. ctx.beginPath();
  88. for (var i = 0; i < precision; i++) {
  89. var vector = this.vertices[i];
  90. ctx.lineTo(vector.x, vector.y);
  91. }
  92. ctx.globalAlpha = this.size;
  93. ctx.shadowBlur = Math.round((3 - this.size) * 10);
  94. ctx.shadowColor = "hsla(0, 100%, 60%,0.5)";
  95. ctx.shadowOffsetX = this.x + 1000;
  96. ctx.globalCompositeOperation = "screen"
  97. ctx.closePath();
  98. ctx.fill()
  99. ctx.restore();
  100. };
  101. function render(a){
  102. requestAnimationFrame(render);
  103. hearts.push(new Heart())
  104. ctx.clearRect(0,0,ww,wh);
  105. for (var i = 0; i < hearts.length; i++) {
  106. hearts[i].draw();
  107. if(hearts[i].size <= 0){
  108. hearts.splice(i,1);
  109. i--;
  110. }
  111. }
  112. }
  113. onResize();
  114. window.addEventListener("mousemove", onMove);
  115. window.addEventListener("touchmove", onMove);
  116. window.addEventListener("resize", onResize);
  117. requestAnimationFrame(render);
  118. window.onload=function starttime(){
  119. time(h1,'2023,01,01'); // 2023年元旦时间
  120. ptimer = setTimeout(starttime,1000); // 添加计时器
  121. }
  122. function time(obj,futimg){
  123. var nowtime = new Date().getTime(); // 现在时间转换为时间戳
  124. var futruetime = new Date(futimg).getTime(); // 未来时间转换为时间戳
  125. var msec = futruetime-nowtime; // 毫秒 未来时间-现在时间
  126. var time = (msec/1000); // 毫秒/1000
  127. var day = parseInt(time/86400); // 天 24*60*60*1000
  128. var hour = parseInt(time/3600)-24*day; // 小时 60*60 总小时数-过去的小时数=现在的小时数
  129. var minute = parseInt(time%3600/60); // 分 -(day*24) 以60秒为一整份 取余 剩下秒数 秒数/60 就是分钟数
  130. var second = parseInt(time%60); // 以60秒为一整份 取余 剩下秒数
  131. obj.innerHTML="<br>距离2023年还有:<br>"+day+"天"+hour+"小时"+minute+"分"+second+"秒"+"<br><span>平安喜乐<br>万事顺遂</span>"
  132. return true;
  133. }
  134. </script>
  135. </body>
  136. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/85125
推荐阅读
相关标签
  

闽ICP备14008679号