马上跨年了,如何用代码写一个“跨年倒计时”呢? 您所在的位置:网站首页 跨年倒计时视频无水印 马上跨年了,如何用代码写一个“跨年倒计时”呢?

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

#马上跨年了,如何用代码写一个“跨年倒计时”呢?| 来源: 网络整理| 查看: 265

前言

大家好,我是陈橘又青,再过两周就是新的一年了,作为一名有仪式感的程序员,今天我们就来制作一个简单的跨年倒计时小网页,祝看到的所有人新年快乐!(附上完整源码,需要的小伙伴自取即可)

文章目录 前言效果展示代码展示倒计时讲解背景音乐

效果展示

在这里插入图片描述

代码展示 doctype html> 新年快乐 body{ overflow: hidden; margin: 0; } h1{ position: fixed; top: 30%; left: 0; width: 100%; text-align: center; transform:translateY(-50%); font-family: 'Love Ya Like A Sister', cursive; font-size: 60px; color: ff64d9; padding: 0 20px; } h1 span{ position: fixed; left: 0; width: 100%; text-align: center; margin-top:30px; font-size:40px; } var canvas = document.querySelector("canvas"), ctx = canvas.getContext("2d"); var ww,wh; function onResize(){ ww = canvas.width = window.innerWidth; wh = canvas.height = window.innerHeight; } ctx.strokeStyle = "red"; ctx.shadowBlur = 25; ctx.shadowColor = "hsla(0, 100%, 60%,0.5)"; var precision = 100; var hearts = []; var mouseMoved = false; function onMove(e){ mouseMoved = true; if(e.type === "touchmove"){ hearts.push(new Heart(e.touches[0].clientX, e.touches[0].clientY)); hearts.push(new Heart(e.touches[0].clientX, e.touches[0].clientY)); } else{ hearts.push(new Heart(e.clientX, e.clientY)); hearts.push(new Heart(e.clientX, e.clientY)); } } var Heart = function(x,y){ this.x = x || Math.random()*ww; this.y = y || Math.random()*wh; this.size = Math.random()*2 + 1; this.shadowBlur = Math.random() * 10; this.speedX = (Math.random()+0.2-0.6) * 8; this.speedY = (Math.random()+0.2-0.6) * 8; this.speedSize = Math.random()*0.05 + 0.01; this.opacity = 1; this.vertices = []; for (var i = 0; i x : (15 * Math.pow(Math.sin(step), 3)), y : -(13 * Math.cos(step) - 5 * Math.cos(2 * step) - 2 * Math.cos(3 * step) - Math.cos(4 * step)) } this.vertices.push(vector); } } Heart.prototype.draw = function(){ this.size -= this.speedSize; this.x += this.speedX; this.y += this.speedY; ctx.save(); ctx.translate(-1000,this.y); ctx.scale(this.size, this.size); ctx.beginPath(); for (var i = 0; i requestAnimationFrame(render); hearts.push(new Heart()) ctx.clearRect(0,0,ww,wh); for (var i = 0; i hearts.splice(i,1); i--; } } } onResize(); window.addEventListener("mousemove", onMove); window.addEventListener("touchmove", onMove); window.addEventListener("resize", onResize); requestAnimationFrame(render); window.onload=function starttime(){ time(h1,'2023,01,01'); // 2023年元旦时间 ptimer = setTimeout(starttime,1000); // 添加计时器 } function time(obj,futimg){ var nowtime = new Date().getTime(); // 现在时间转换为时间戳 var futruetime = new Date(futimg).getTime(); // 未来时间转换为时间戳 var msec = futruetime-nowtime; // 毫秒 未来时间-现在时间 var time = (msec/1000); // 毫秒/1000 var day = parseInt(time/86400); // 天 24*60*60*1000 var hour = parseInt(time/3600)-24*day; // 小时 60*60 总小时数-过去的小时数=现在的小时数 var minute = parseInt(time%3600/60); // 分 -(day*24) 以60秒为一整份 取余 剩下秒数 秒数/60 就是分钟数 var second = parseInt(time%60); // 以60秒为一整份 取余 剩下秒数 obj.innerHTML="距离2023年还有:"+day+"天"+hour+"小时"+minute+"分"+second+"秒"+"平安喜乐万事顺遂" return true; } 倒计时讲解

新年倒计时才是本页面的灵魂,很多小伙伴可能会好奇,如何设置倒计时呢?

其实只需要用当前window的事件来设置一个开始的事件。【倒计时时间=过年时间-当前时间】

设置time为2023年1月1日,添加计时器从当前时间开始, 设置作用时间,将时间进行一个转换为当前的时间,并设置未来的一个时间戳随后定义一个变量来获取倒计时时间是多少。【未来时间-现在的时间】

再将获取到的时间毫秒为单位/1000来计算,小时数用总小时数-过去的小时数为现在的小时数,再以60秒为一整份,取余计算得到的就是分钟数在用当前的时间%60就是秒数,最后我们插入想要的信息将上述设置的变量插入进去即可!

背景音乐

src给一个背景的音乐链接(非本地音频需联网),使用下方代码即可。

document.addEventListener('DOMContentLoaded', function () { function audioAutoPlay() { var audio = document.getElementById('bgmusic'); audio.play(); document.addEventListener("WeixinJSBridgeReady", function () { audio.play(); }, false); } audioAutoPlay(); });

在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有