196 lines
5.2 KiB
HTML
196 lines
5.2 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||
<title>Document</title>
|
||
<style>
|
||
body {
|
||
margin: 0;
|
||
padding: 0;
|
||
width: 100%;
|
||
height: 100vh;
|
||
}
|
||
|
||
img {
|
||
width: 80px;
|
||
height: 96px;
|
||
position: fixed;
|
||
top: -96px;
|
||
}
|
||
|
||
.dialog {
|
||
position: fixed;
|
||
width: 100%;
|
||
height: 100%;
|
||
background: #0000008c;
|
||
z-index: 9999;
|
||
display: none;
|
||
}
|
||
|
||
.center {
|
||
width: 40%;
|
||
background: #fff;
|
||
padding: 15px 10px;
|
||
border-radius: 4px;
|
||
position: absolute;
|
||
left: 50%;
|
||
top: 50%;
|
||
transform: translate(-50%, -50%);
|
||
}
|
||
|
||
.title {
|
||
height: 40px;
|
||
line-height: 40px;
|
||
border-bottom: 1px solid #ebe9e9;
|
||
|
||
}
|
||
|
||
.content {
|
||
padding: 20px 0;
|
||
text-align: center;
|
||
}
|
||
|
||
.footer {
|
||
display: flex;
|
||
justify-content: flex-end;
|
||
}
|
||
|
||
.footer .ok {
|
||
padding: 3px 15px;
|
||
background: #2a6bf4;
|
||
color: #fff;
|
||
font-size: 13px;
|
||
border-radius: 4px;
|
||
cursor: pointer;
|
||
}
|
||
</style>
|
||
</head>
|
||
|
||
<body>
|
||
<div class="dialog">
|
||
<div class="center">
|
||
<div class="title">提示</div>
|
||
<div class="content"></div>
|
||
<div class="footer">
|
||
<div class="ok">确定</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<!-- 1 获取事件; 2 绑定事件; 3 事件处理函数。
|
||
|
||
该事件:首先:红包随机出现在屏幕上:
|
||
1、获取屏幕的长宽,用来计算红包出现的位置;2、创建红包,引入红包图片,并给它设置在x轴上随机出现Math.random();
|
||
3、给红包绑定点击事件,并限制3次点击次数;3、点击确定红包消失;4、红包展现在html上;
|
||
|
||
其次:设置动画:1、红包从屏幕顶部上方随机生成,之后开始下落,完全下降到底部以后消失;
|
||
|
||
最后:打开并去除红包:1、红包被点击以后被打开;2、规定中奖几率和次数;3、点击确定按钮以后,关闭红包;4、红包随即消失;
|
||
甲方需求:屏幕上随机生成红包雨,用户可点击,点击时红包雨暂停,确定后该红包消失,红包雨继续。要规定中奖机率和抽奖次数 -->
|
||
</body>
|
||
<script>
|
||
//返回值
|
||
function $(className) {
|
||
return document.querySelector(className)
|
||
}
|
||
// 返回数组
|
||
function _(className) {
|
||
return document.querySelectorAll(className)
|
||
}
|
||
var dialog = $('.dialog')
|
||
var center = $('.center')
|
||
var content = $('.content')
|
||
var ok = $('.ok')
|
||
var body = $('body')
|
||
var {clientWidth,clientHeight} = document.body;
|
||
var click = 0
|
||
|
||
|
||
// 红包相关属性创建
|
||
function HB() {
|
||
var img = document.createElement("img");
|
||
|
||
var x = Math.random() * (clientWidth - 80)
|
||
img.src = "../hb.png";
|
||
img.style.left = `${x}px`;
|
||
img.style.top = '-96px'
|
||
img.onclick = function () {
|
||
clickImg = img
|
||
click += 1
|
||
//清除红包向下滚动定时器
|
||
_('img').forEach((value, index) => {
|
||
clearTimeout(value.time)
|
||
})
|
||
//清除创建红包定时器
|
||
clearInterval(createdNumber)
|
||
if (click > 3) {
|
||
content.innerText = "三次抽奖已经用完"
|
||
dialog.style.display = "block"
|
||
} else {
|
||
rate()
|
||
dialog.style.display = "block"
|
||
}
|
||
|
||
}
|
||
body.appendChild(img)
|
||
move(img)
|
||
|
||
}
|
||
|
||
// 定义中奖几率
|
||
function rate() {
|
||
var num = Math.random();
|
||
if (num > 0 && num <= 0.5) {
|
||
content.innerText = "恭喜您中了九折"
|
||
} else if (num > 0.5 && num <= 0.9) {
|
||
content.innerText = "恭喜您中了八折"
|
||
} else {
|
||
"恭喜您中了五折"
|
||
}
|
||
}
|
||
// 红包定时生成
|
||
createdNumber = setInterval(() => {
|
||
HB()
|
||
}, 1000)
|
||
|
||
// 红包运动
|
||
function move(tag) {
|
||
var now = Number(tag.style.top.split('px')[0])
|
||
|
||
if (now <= clientHeight) {
|
||
tag.time = setTimeout(() => {
|
||
tag.style.top = `${now + 3}px`
|
||
move(tag)
|
||
}, 15)
|
||
|
||
|
||
} else {
|
||
body.removeChild(tag)
|
||
}
|
||
}
|
||
|
||
// 红包关闭
|
||
ok.onclick = function (){
|
||
dialog.style.display = "none"
|
||
|
||
body.removeChild(clickImg)
|
||
|
||
//重新设置 创建红包定时器
|
||
createdNumber = setInterval(() => {
|
||
HB()
|
||
}, 1000)
|
||
|
||
//重新设置页面红包向下滚动
|
||
_('img').forEach((value, index) => {
|
||
value.time = setTimeout(() => {
|
||
move(value)
|
||
}, 15)
|
||
})
|
||
}
|
||
|
||
|
||
</script>
|
||
|
||
</html> |