html、css、javascript 实现红包雨
<p><strong>先看看效果吧!</strong></p><p><strong><img src="https://img2018.cnblogs.com/common/1543983/202002/1543983-20200206080357150-1653696339.gif"></strong></p>
<p><strong>HTML代码:</strong></p>
<div class="cnblogs_code">
<pre><div id="wrapper">
<img src="images/hb/petal1.png" >
</div><br><div id="modol">
<div id="hb">
<div id="text"></div>
<div id="btn">继续抢红包</div>
</div>
</div>
</pre>
</div>
<p><strong>CSS代码:</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 0, 1)">* </span>{<span style="color: rgba(255, 0, 0, 1)">
padding</span>:<span style="color: rgba(0, 0, 255, 1)"> 0</span>;<span style="color: rgba(255, 0, 0, 1)">
margin</span>:<span style="color: rgba(0, 0, 255, 1)"> 0</span>;
}<span style="color: rgba(128, 0, 0, 1)">
body </span>{<span style="color: rgba(255, 0, 0, 1)">
background-color</span>:<span style="color: rgba(0, 0, 255, 1)"> #fb9d3b</span>;<span style="color: rgba(255, 0, 0, 1)">
overflow</span>:<span style="color: rgba(0, 0, 255, 1)"> hidden</span>;
}
<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
animation各参数意思
关键帧动画名称
动画执行时间
动画延迟时间
动画执行次数
动画的执行速度函数
动画执行方向alternate间隔的顺序 ( 0 到 100 到 0 到 100 )
reverse 反向运动 (100 到 0 )
reverse-alternate 反向间隔运动
动画执行模式forwards(动画停止在最后一个关键帧的位置)
backwards (动画第一个关键帧立即执行)
both(两者都 即立即作用第一个关键帧也停止在最后一个关键帧)
</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(128, 0, 0, 1)">
#wrapper img </span>{<span style="color: rgba(255, 0, 0, 1)">
position</span>:<span style="color: rgba(0, 0, 255, 1)"> absolute</span>;<span style="color: rgba(255, 0, 0, 1)">
transform</span>:<span style="color: rgba(0, 0, 255, 1)"> translateY(-100%)</span>; <span style="color: rgba(0, 128, 0, 1)">/*下落动画*/</span><span style="color: rgba(255, 0, 0, 1)">
animation</span>:<span style="color: rgba(0, 0, 255, 1)"> dropDowm 3s forwards</span>; <span style="color: rgba(0, 128, 0, 1)">/*旋转动画*/</span>
}<span style="color: rgba(128, 0, 0, 1)">
@keyframes dropDowm</span>{<span style="color: rgba(255, 0, 0, 1)">
0% {
top</span>:<span style="color: rgba(0, 0, 255, 1)"> 0px</span>;<span style="color: rgba(255, 0, 0, 1)">
transform</span>:<span style="color: rgba(0, 0, 255, 1)"> translateY(-100%) rotate(0deg)</span>;
}<span style="color: rgba(128, 0, 0, 1)">
100% </span>{<span style="color: rgba(255, 0, 0, 1)">
top</span>:<span style="color: rgba(0, 0, 255, 1)"> 110%</span>;<span style="color: rgba(255, 0, 0, 1)">
transform</span>:<span style="color: rgba(0, 0, 255, 1)"> translateY(0%) rotate(360deg)</span>;
}<span style="color: rgba(128, 0, 0, 1)">
}
#modol </span>{<span style="color: rgba(255, 0, 0, 1)">
display</span>:<span style="color: rgba(0, 0, 255, 1)"> none</span>;
}<span style="color: rgba(128, 0, 0, 1)">
#modol::before </span>{<span style="color: rgba(255, 0, 0, 1)">
content</span>:<span style="color: rgba(0, 0, 255, 1)"> ''</span>;<span style="color: rgba(255, 0, 0, 1)">
position</span>:<span style="color: rgba(0, 0, 255, 1)"> fixed</span>;<span style="color: rgba(255, 0, 0, 1)">
top</span>:<span style="color: rgba(0, 0, 255, 1)"> 0</span>;<span style="color: rgba(255, 0, 0, 1)">
left</span>:<span style="color: rgba(0, 0, 255, 1)"> 0</span>;<span style="color: rgba(255, 0, 0, 1)">
right</span>:<span style="color: rgba(0, 0, 255, 1)"> 0</span>;<span style="color: rgba(255, 0, 0, 1)">
bottom</span>:<span style="color: rgba(0, 0, 255, 1)"> 0</span>;<span style="color: rgba(255, 0, 0, 1)">
background-color</span>:<span style="color: rgba(0, 0, 255, 1)"> rgba(0,0,0, 0.5)</span>;
}<span style="color: rgba(128, 0, 0, 1)">
#hb </span>{<span style="color: rgba(255, 0, 0, 1)">
width</span>:<span style="color: rgba(0, 0, 255, 1)"> 350px</span>;<span style="color: rgba(255, 0, 0, 1)">
height</span>:<span style="color: rgba(0, 0, 255, 1)"> 450px</span>;<span style="color: rgba(255, 0, 0, 1)">
border-radius</span>:<span style="color: rgba(0, 0, 255, 1)"> 20px</span>;<span style="color: rgba(255, 0, 0, 1)">
background-color</span>:<span style="color: rgba(0, 0, 255, 1)"> #e7223e</span>;<span style="color: rgba(255, 0, 0, 1)">
color</span>:<span style="color: rgba(0, 0, 255, 1)"> #fad755</span>;<span style="color: rgba(255, 0, 0, 1)">
position</span>:<span style="color: rgba(0, 0, 255, 1)"> fixed</span>;<span style="color: rgba(255, 0, 0, 1)">
left</span>:<span style="color: rgba(0, 0, 255, 1)"> 50%</span>;<span style="color: rgba(255, 0, 0, 1)">
top</span>:<span style="color: rgba(0, 0, 255, 1)"> 50%</span>;<span style="color: rgba(255, 0, 0, 1)">
margin-top</span>:<span style="color: rgba(0, 0, 255, 1)"> -225px</span>;<span style="color: rgba(255, 0, 0, 1)">
margin-left</span>:<span style="color: rgba(0, 0, 255, 1)"> -175px</span>;<span style="color: rgba(255, 0, 0, 1)">
font-size</span>:<span style="color: rgba(0, 0, 255, 1)"> 30px</span>;<span style="color: rgba(255, 0, 0, 1)">
font-weight</span>:<span style="color: rgba(0, 0, 255, 1)"> 900</span>;<span style="color: rgba(255, 0, 0, 1)">
display</span>:<span style="color: rgba(0, 0, 255, 1)"> flex</span>;<span style="color: rgba(255, 0, 0, 1)">
flex-direction</span>:<span style="color: rgba(0, 0, 255, 1)"> column</span>;<span style="color: rgba(255, 0, 0, 1)">
justify-content</span>:<span style="color: rgba(0, 0, 255, 1)"> center</span>;<span style="color: rgba(255, 0, 0, 1)">
align-items</span>:<span style="color: rgba(0, 0, 255, 1)"> center</span>;
}<span style="color: rgba(128, 0, 0, 1)">
#btn </span>{<span style="color: rgba(255, 0, 0, 1)">
background-color</span>:<span style="color: rgba(0, 0, 255, 1)"> #fad755</span>;<span style="color: rgba(255, 0, 0, 1)">
color</span>:<span style="color: rgba(0, 0, 255, 1)"> #e7223e</span>;<span style="color: rgba(255, 0, 0, 1)">
font-size</span>:<span style="color: rgba(0, 0, 255, 1)"> 18px</span>;<span style="color: rgba(255, 0, 0, 1)">
margin-top</span>:<span style="color: rgba(0, 0, 255, 1)"> 10px</span>;<span style="color: rgba(255, 0, 0, 1)">
padding</span>:<span style="color: rgba(0, 0, 255, 1)"> 10px</span>;<span style="color: rgba(255, 0, 0, 1)">
border</span>:<span style="color: rgba(0, 0, 255, 1)">none</span>;<span style="color: rgba(255, 0, 0, 1)">
outline</span>:<span style="color: rgba(0, 0, 255, 1)"> none</span>;<span style="color: rgba(255, 0, 0, 1)">
cursor</span>:<span style="color: rgba(0, 0, 255, 1)"> pointer</span>;
}</pre>
</div>
<p>JavaScript代码:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> init() {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> dom = createDom(40<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">var</span> wrapper = document.getElementById('wrapper'<span style="color: rgba(0, 0, 0, 1)">);
wrapper.appendChild(dom);
bindEvent();
}
</span>
<span style="color: rgba(0, 0, 255, 1)">var</span> totalMoney = 0<span style="color: rgba(0, 0, 0, 1)">;<span style="color: rgba(51, 153, 102, 1)"> //保存所有抢到的红包总金额
</span></span><span style="color: rgba(0, 0, 255, 1)">var</span> delayTime = 0<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">var</span> lastImg = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; <span style="color: rgba(51, 153, 102, 1)">//最后一张掉落的图片
</span></span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建红包结构, num代表红包个数</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> createDom(num) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> frag = document.createDocumentFragment();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建文档碎片</span>
<span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0; i < num; i ++<span style="color: rgba(0, 0, 0, 1)">) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> img = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Image();
img.src </span>= './images/hb/petal' + ranNum(0,10) + '.png'<span style="color: rgba(0, 0, 0, 1)">;
img.style.left </span>= ranNum(0, window.innerWidth) + 'px'<span style="color: rgba(0, 0, 0, 1)">; <span style="color: rgba(51, 153, 102, 1)"> //让红包散列分布
</span></span><span style="color: rgba(0, 0, 255, 1)">var</span> delay = ranNum(0,100) / 10<span style="color: rgba(0, 0, 0, 1)">;
img.style.animationDelay </span>= delay + 's'<span style="color: rgba(0, 0, 0, 1)">; <span style="color: rgba(51, 153, 102, 1)">//设置红包出现时间
</span></span><span style="color: rgba(0, 0, 255, 1)">if</span>(delayTime <<span style="color: rgba(0, 0, 0, 1)"> delay) {
delayTime </span>=<span style="color: rgba(0, 0, 0, 1)"> delay;
lastImg </span>=<span style="color: rgba(0, 0, 0, 1)"> img;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">data-money</span>
img.dataset.money = ranNum(0,1000) / 100<span style="color: rgba(0, 0, 0, 1)">; <span style="color: rgba(51, 153, 102, 1)">//设置每个红包的钱数</span>
frag.appendChild(img);
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> frag;
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">绑定点击事件</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> bindEvent() {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> wrapper = document.getElementById('wrapper'<span style="color: rgba(0, 0, 0, 1)">),
imgList </span>= document.getElementsByTagName('img'<span style="color: rgba(0, 0, 0, 1)">),
modol </span>= document.getElementById('modol'<span style="color: rgba(0, 0, 0, 1)">),
text </span>= document.getElementById('text'<span style="color: rgba(0, 0, 0, 1)">),
btn </span>= document.getElementById('btn'<span style="color: rgba(0, 0, 0, 1)">);<br><br> <span style="color: rgba(51, 153, 102, 1)">//点击红包事件</span>
addEvent(wrapper, </span>'mousedown', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(e) {
</span><span style="color: rgba(0, 0, 255, 1)">var</span> event = e ||<span style="color: rgba(0, 0, 0, 1)"> window.event,
target </span>= event.target ||<span style="color: rgba(0, 0, 0, 1)"> event.srcElement,
money </span>=<span style="color: rgba(0, 0, 0, 1)"> event.target.dataset.money;
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(money) {
text.innerText </span>= '恭喜抢到红包' + money + '元'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0, len = imgList.length; i < len; i ++<span style="color: rgba(0, 0, 0, 1)">) {
imgList.style.animationPlayState </span>= 'paused'<span style="color: rgba(0, 0, 0, 1)">;
}
modol.style.display </span>= 'block'<span style="color: rgba(0, 0, 0, 1)">;
totalMoney </span>+=<span style="color: rgba(0, 0, 0, 1)"> Number(money);
}
});<br><br> <span style="color: rgba(51, 153, 102, 1)">//点击继续抢红包按钮事件</span>
addEvent(btn, </span>'click', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
modol.style.display </span>= 'none'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">for</span>(<span style="color: rgba(0, 0, 255, 1)">var</span> i = 0, len = imgList.length; i < len; i ++<span style="color: rgba(0, 0, 0, 1)">) {
imgList.style.animationPlayState </span>= 'running'<span style="color: rgba(0, 0, 0, 1)">;
}
});<br><br> <span style="color: rgba(51, 153, 102, 1)">//监听最后一个红包动画完成</span>
addEvent(lastImg, </span>'webkitAnimationEnd', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
modol.style.display </span>= 'block'<span style="color: rgba(0, 0, 0, 1)">;
text.innerText </span>= '恭喜总共抢到了' + totalMoney.toFixed(2) + '元'<span style="color: rgba(0, 0, 0, 1)">;
btn.style.display </span>= 'none'<span style="color: rgba(0, 0, 0, 1)">;
});
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">min 到 max 之间的随机数</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> ranNum(min, max) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> Math.ceil(Math.random()*(max - min) +<span style="color: rgba(0, 0, 0, 1)"> min);
}
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">兼容的 添加事件函数</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> addEvent(elem, type, handle) {
</span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (elem.addEventListener) {
elem.addEventListener(type, handle, </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">);
} </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (elem.attachEvent) {
elem.attachEvent(</span>'on' + type, <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
handle.call(elem);
})
} </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
elem[</span>'on' + type] =<span style="color: rgba(0, 0, 0, 1)"> handle;
}
}
</span>
<span style="color: rgba(0, 0, 0, 1)">
init();</span></pre>
</div>
<p> </p>
<p> </p>
<p><span style="font-size: 18px"><strong><span style="color: rgba(255, 153, 204, 1)">*:ஐ٩(๑´ᵕ`)۶ஐ:*</span> </strong></span><span style="font-size: 18px">学习使我进步</span></p>
<p><span style="font-size: 18px">欢迎留言讨论哦!</span></p>
<p> <img src="https://common.cnblogs.com/images/loading.gif"></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/hjysunshine/p/12267560.html 直接看代码片段可能有点懵,我把完整的实现逻辑补了一下,贴个可直接跑的版本,楼主可以参考整合。结构上用 canvas 绘制红包下落会更流畅,这里就把红包替换成图片对象,背景加个喜庆渐变。
HTML 结构精简一下:
<div id="wrapper"></div>
<div id="modal">
<div id="hb">
<div id="text">恭喜获得0.88元红包</div>
<div id="btn">继续抢红包</div>
</div>
</div>
JS 核心逻辑(红包雨 + 点击事件):
const wrapper = document.getElementById('wrapper');
const modal = document.getElementById('modal');
const text = document.getElementById('text');
const btn = document.getElementById('btn');
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
wrapper.appendChild(canvas);
let w, h, reds = [];
const img = new Image();
img.src = 'images/hb/petal1.png'; // 替换成你的红包图
function resize() {
w = canvas.width = window.innerWidth;
h = canvas.height = window.innerHeight;
}
window.addEventListener('resize', resize);
resize();
function createRed() {
const x = Math.random() w;
const y = -50;
const speed = 2 + Math.random() 4;
const size = 40 + Math.random() 20; // 红包图片尺寸
return { x, y, speed, size, clicked: false };
}
// 初始化一些下落红包
for (let i = 0; i < 20; i++) {
const r = createRed();
r.y = Math.random() h; // 分散初始位置
reds.push(r);
}
function draw() {
ctx.clearRect(0, 0, w, h);
reds.forEach((r, i) => {
if (!r.clicked) {
r.y += r.speed;
if (r.y > h + 50) {
reds = createRed(); // 超出屏幕重置
}
ctx.drawImage(img, r.x - r.size/2, r.y - r.size/2, r.size, r.size);
}
});
requestAnimationFrame(draw);
}
img.onload = () => {
draw();
};
// 点击检测
canvas.addEventListener('click', (e) => {
const mx = e.clientX;
const my = e.clientY;
reds.forEach(r => {
if (!r.clicked) {
const dx = mx - r.x;
const dy = my - r.y;
if (Math.sqrt(dxdx + dydy) < r.size/1.5) {
r.clicked = true;
// 显示弹窗逻辑
const amount = (Math.random() * 5).toFixed(2);
text.textContent = 恭喜获得${amount}元红包
;
modal.style.display = 'flex';
}
}
});
});
btn.addEventListener('click', () => {
modal.style.display = 'none';
// 重新显示该红包?或者直接让它消失,这里简单处理为重新生成
reds.forEach(r => r.clicked = false); // 全部恢复
});
CSS 只补充关键部分:
modal {
display: none;
position: fixed;
top: 0; left: 0; width: 100%; height: 100%;
background: rgba(0,0,0,0.6);
justify-content: center;
align-items: center;
z-index: 10;
}
hb {
background: #d42a2a;
padding: 30px 50px;
border-radius: 12px;
color: #ffd966;
text-align: center;
font-size: 20px;
box-shadow: 0 0 20px gold;
}
btn {
margin-top: 20px;
background: gold;
color: #b30000;
padding: 10px 30px;
border-radius: 30px;
cursor: pointer;
font-weight: bold;
}
这样就可以直接用了,图片换成自己的路径。如果怕性能有问题,可以把红包数量控制在30以内。楼主那个 animation 写了很多参数,如果要用纯 CSS 做下落动画也没问题,但点击交互用 JS 画布更灵活一些。源码公社坛友有更好的方案欢迎补充~ 哇塞!太棒了! 这个红包雨效果做得真不错,看着就很喜庆!
支持一下! 刚好最近在做活动页面,这个效果正好可以用上~
有个小问题想请教一下:
那个红包图片是从0到10随机选取的,请问是需要提前准备11张不同的红包图片吗?
还有如果想在手机端也能完美显示,是不是需要加一些响应式的判断?比如调整红包大小或者数量什么的。
另外感觉这个动画效果还可以更丰富一点,比如:
[*]红包掉落的时候加一点抛物线轨迹
[*]抢到红包的时候加个放大抖动的特效
[*]背景也可以做成动态的,比如红色灯笼或者烟花什么的
不过总体来说已经很优秀了!感谢楼主的分享! 源码公社就需要这种热心分享技术的大神!
来自源码公社 App
页:
[1]