Html+Css+js实现带有雪花飘落的许愿墙效果

功能要点介绍


1雪花飘落的代码是js控制的,可自行修改

2许愿墙的贴纸是for循环控制的,可自行修改

3文字是datas数组控制的,可以自行修改

代码部分


雪花逻辑部分(index.js)


class Snowflake {

constructor() {

this.x = 0;

this.y = 0;

this.vx = 0;

this.vy = 0;

this.radius = 0;

this.alpha = 0;

this.reset();

}

reset() {

this.x = this.randBetween(0, window.innerWidth);

this.y = this.randBetween(0, -window.innerHeight + 672);

this.vx = this.randBetween(-3, 3);

this.vy = this.randBetween(2, 5);

this.radius = this.randBetween(1, 4);

this.alpha = this.randBetween(0.1, 0.9);

}

randBetween(min, max) {

return min + Math.random() * (max - min);

}

update() {

this.x += this.vx;

this.y += this.vy;

if (this.y + this.radius > window.innerHeight) {

this.reset();

}

}

}

class Snow {

constructor() {

this.canvas = document.createElement(‘canvas’);

this.ctx = this.canvas.getContext(‘2d’);

document.body.appendChild(this.canvas);

window.addEventListener(‘resize’, () => this.onResize());

this.onResize();

this.updateBound = this.update.bind(this);

requestAnimationFrame(this.updateBound);

this.createSnowflakes();

}

onResize() {

console.log(this.width, ‘width’);

console.log(this.height, ‘height’);

this.width = window.innerWidth;

this.height = window.innerHeight;

this.canvas.width = this.width;

this.canvas.height = this.height + 672;

}

createSnowflakes() {

const flakes = window.innerWidth / 4;

this.snowflakes = [];

for (let s = 0; s < flakes; s++) {

this.snowflakes.push(new Snowflake());

}

}

update() {

this.ctx.clearRect(0, 0, this.width, this.height);

for (let flake of this.snowflakes) {

flake.update();

this.ctx.save();

this.ctx.fillStyle = ‘#FFF’;

this.ctx.beginPath();

this.ctx.arc(flake.x, flake.y, flake.radius, 0, Math.PI * 2);

this.ctx.closePath();

this.ctx.globalAlpha = flake.alpha;

this.ctx.fill();

this.ctx.restore();

}

requestAnimationFrame(this.updateBound);

}

}

new Snow();

逻辑文件 (script.js)


//转换时间格式

function formatDated(date) {

var date = new Date(date);

var YY = date.getFullYear() + ‘-’;

var MM = (date.getMonth() + 1 < 10 ? ‘0’ +

(date.getMonth() + 1) : date.getMonth() + 1) + ‘-’;

var DD = (date.getDate() < 10 ? ‘0’ +

(date.getDate()) :

date.getDate());

var hh = (date.getHours() < 10 ? ‘0’ +

date.getHours() :

date.getHours()) + ‘:’;

var mm = (date.getMinutes() < 10 ? ‘0’ +

date.getMinutes() :

date.getMinutes()) + ‘:’;

var ss = (date.getSeconds() < 10 ? ‘0’ +

date.getSeconds() : date.getSeconds());

return YY + MM + DD +" "+hh + mm + ss;

}

function timeChange(){

var datas =[]

var idList=0;

var timer= setInterval(() => {

//控制文字的说明

for( i=0;i<2;i++){

datas.push({

id:idList,

name:“歌谣”,

content:" 在过往的岁月中,我遇到了形形色色的人和事情。有的人坚持,有的人放弃。有的人逆袭,有的人失败。最好的种树是十年前其次是现在。很高兴遇到你,愿你的人生多姿多彩,幸福绵绵,好事连连。歌谣很棒谢谢你的一键三连",

time:formatDated(new Date())

})

}

idList++

console.log(idList,“idList”)

if(idList==5){

clearInterval(timer)

}

var content = document.getElementById(“content”);

var zIndex = 1;

for (var i = 0; i < datas.length; i++) {

var data = datas[i];

var div = document.createElement(“div”);

div.className = “tip1”;

div.id = “cc” + data.id;

content.appendChild(div);

//设置随机数

let num=parseInt(Math.random()*10000)

div.innerHTML = ‘

  • 第’+num+‘条’+ data.time + ‘

  • ×

  • ‘’

  • data.content

  • ‘’

  • ‘’

  • ’ + data.name + ‘

  • ‘’;

// 控制贴纸的位置

var x = parseInt(Math.random() * 1500)+200;

var y = parseInt(Math.random() * 700);

div.style.left = x + “px”;

div.style.top = y + “px”;

div.onclick = function () {

zIndex++;

this.style.zIndex = zIndex;

};

var closeDiv = div.children[0];

closeDiv.ondblclick = function () {

this.parentNode.style.display = “none”;

};

var x = closeDiv.children[1];

x.onclick = function () {

this.parentNode.parentNode.style.display = “none”;

};

}

},2000)

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:前端)
img

最后

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。


开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。

  • 21
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值