demo1四宫格:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
name="viewport">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>四宫格抽奖</title>
</head>
<style>
html,
body {
width: 100%;
height: 100%;
max-width: 750px;
margin: auto;
}
.outBox {
width: 100%;
height: 60%;
background: gray;
position: relative;
display: flex;
flex-flow: row wrap;
}
.prize {
width: 44%;
height: 40%;
margin: 5% 3%;
text-align: center;
font-size: xx-large;
color: #fff;
padding-top: 15%;
box-sizing: border-box;
border: 6px double #AB945E;
}
.prize.active {
background: goldenrod;
}
.btn {
position: absolute;
left: 30%;
bottom: -20%;
width: 40%;
text-align: center;
height: 12%;
font-size: xx-large;
}
</style>
<body>
<div class="outBox" id="lotteryBoxs">
<div class="prize prize-0 one">一等奖</div>
<div class="prize prize-1 two">二等奖</div>
<div class="prize prize-3 four">谢谢参与</div>
<div class="prize prize-2 three">三等奖</div>
<button class="btn">开启好运</button>
</div>
</body>
<script src="http://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
$(document).ready(function () {
var lottery = {
index: 0,
count: 0,
timer: 0,
speed: 10,
times: 0,
cycle: 30,
prize: 0,
init: function (id) {
if ($("#" + id).find(".prize").length > 0) {
$lottery = $("#" + id);
$units = $lottery.find(".prize");
this.obj = $lottery;
this.count = $units.length;
}
},
roll: function () {
var index = this.index;
var count = this.count;
var lottery = this.obj;
$(lottery).find(".prize").removeClass("active");
index += 1;
if (index > count - 1) {
index = 0;
}
$(lottery).find(".prize-" + this.index).addClass("active");
this.index = index;
return false;
},
stop: function (index) {
this.prize = index;
return false;
}
};
function roll() {
lottery.times += 1;
lottery.roll();
var prize_site = $("#lotteryBoxs").attr("prize_site");
if (lottery.times > lottery.cycle + 10 && lottery.index == prize_site) {
var prize_id = $("#lotteryBoxs").attr("prize_id");
var prize_name = $("#lotteryBoxs").attr("prize_name");
console.log(prize_site + "结果")
if (prize_site == 1 || prize_site == 2 || prize_site == 3) {
setTimeout(function () {
console.log("恭喜你获得" + prize_name)
}, 500)
} else {
setTimeout(function () {
console.log("中奖结果:" + prize_name)
}, 500)
}
clearTimeout(lottery.timer);
lottery.prize = -1;
lottery.times = 0;
click = false;
} else {
if (lottery.times < lottery.cycle) {
lottery.speed -= 20;
} else if (lottery.times == lottery.cycle) {
var index = Math.random() * (lottery.count) | 0;
lottery.prize = index;
} else {
if (lottery.times > lottery.cycle + 10 && ((lottery.prize == 0 && lottery.index == lottery.count - 1) || lottery.prize == lottery.index + 1)) {
lottery.speed += 90;
} else {
lottery.speed += 30;
}
}
if (lottery.speed < 30) {
lottery.speed = 30;
}
lottery.timer = setTimeout(roll, lottery.speed);
}
return false;
}
var click = false;
$(function () {
lottery.init('lotteryBoxs');
$(".btn").click(function () {
if (click) {
return false;
} else {
lottery.speed = 100;
var prizeArr = ["谢谢参与", "一等奖", "二等奖", "三等奖"]
var prizeId = Math.floor(Math.random() * (3 - 0 + 1) + 0);
var prize_site = prizeId;
console.log("位置" + prizeId);
$("#lotteryBoxs").attr("prize_site", prize_site);
$("#lotteryBoxs").attr("prize_name", prizeArr[prizeId]);
roll();
click = true;
return false;
}
});
})
});
</script>
</html>
demo2
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript 实现九宫格抽奖</title>
<style>
body {
background-color: #2c9afc;
}
#container {
width: 330px;
height: 340px;
margin: 10% auto;
border: 7px solid #99d4ff;
border-radius: 10px;
padding: 2%;
}
#list {
width: 330px;
height: 340px;
list-style: none;
margin: 0;
padding: 0;
}
#list li,
#list span {
width: 100px;
height: 100px;
float: left;
text-align: center;
line-height: 100px;
position: relative;
background-color: #384f9a;
margin: 1.4%;
border-radius: 5px;
}
#list li img {
display: block;
width: 100%;
height: 100%;
}
#list li .mask {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
background-color: pink;
display: none;
}
#list span:hover {
cursor: pointer;
color: orange;
font-size: 18px;
}
#list .active .mask {
display: block;
}
#message {
line-height: 32px;
color: #9a9a9a;
text-align: center;
position: absolute;
left: 50%;
top: 50px;
width: 300px;
height: 50px;
margin-left: -150px;
display: none;
background: #000;
color: #fff;
}
</style>
</head>
<body>
<h3 style="text-align:center;margin-top: 10%;color:white">JavaScript 实现九宫格抽奖</h3>
<div id="container">
<ul id="list">
<!-- img标签放奖品图片 -->
<!-- mask类为抽奖滚动起来的标记 -->
<li><img src="./logo.png" />
<div class="mask"></div>
</li>
<li><img src="./logo.png" />
<div class="mask"></div>
</li>
<li><img src="./logo.png" />
<div class="mask"></div>
</li>
<li><img src="./logo.png" />
<div class="mask"></div>
</li>
<span id="startbutton" onclick="startlottery()"
style="background-color:#ff3a59;color:white;font-size: 20px;">开始抽奖</span>
<li><img src="./logo.png" />
<div class="mask"></div>
</li>
<li><img src="./logo.png" />
<div class="mask"></div>
</li>
<li><img src="./logo.png" />
<div class="mask"></div>
</li>
<li><img src="./logo.png" />
<div class="mask"></div>
</li>
</ul>
<div id="message"></div><!-- 获奖信息展示 -->
</div>
</body>
<script type="text/javascript">
var container = document.getElementById('container'),
li = container.getElementsByTagName('li'),
span = document.getElementById('startbutton'),
message = document.getElementById('message'),
timer = null;
bReady = true;
var prize = [0, 1, 2, 4, 7, 6, 5, 3];
var num = 0;
function startlottery() {
if (bReady) {
message.style.display = "none";
span.style.background = "#ada7a8";
bReady = false;
num = getrandomnum(1, 9)
startinit(num);
}
}
function getrandomnum(n, m) {
return parseInt((m - n) * Math.random() + n);
}
function startinit(num) {
var i = 0;
var t = 200;
var rounds = 5;
var rNum = rounds * 8;
timer = setTimeout(startscroll, t);
function startscroll() {
for (var j = 0; j < li.length; j++) {
li[j].className = '';
}
var prizenum = prize[i % li.length];
li[prizenum].className = "active";
i++;
if (i < rNum - 8) {
timer = setTimeout(startscroll, t);
} else if (i >= rNum - 8 && i < rNum + num) {
t += (i - rNum + 8) * 5;
timer = setTimeout(startscroll, t);
}
if (i >= rNum + num) {
if (num == 1) {
message.innerHTML = "恭喜你中了耳机";
} else if (num == 2) {
message.innerHTML = "恭喜你中了iPad";
} else if (num == 3) {
message.innerHTML = "感谢参与";
} else if (num == 4) {
message.innerHTML = "恭喜你中了洋娃娃";
} else if (num == 5) {
message.innerHTML = "恭喜你中了红色鞋子";
} else if (num == 6) {
message.innerHTML = "恭喜你中了白色手机";
} else if (num == 7) {
message.innerHTML = "恭喜你中了黑色手机";
} else if (num == 8) {
message.innerHTML = "恭喜你中了蓝色鞋子";
}
var timer2 = null;
timer2 = setTimeout(function () {
message.style.display = "block";
span.style.background = "#ff3a59";
clearTimeout(timer2);
}, 1000);
bReady = true;
clearTimeout(timer);
}
}
}
</script>
</html>
demo3
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>抽奖</title>
<style>
* {
margin: 0;
padding: 0;
}
li {
list-style: none;
}
body {
background: #2c9afc;
}
.lotteryBox {
width: 420px;
height: 420px;
border: 7px solid #99d4ff;
margin: 50px auto;
border-radius: 10px;
padding: 20px;
position: relative;
}
.lotteryBox ul {
width: 100%;
height: 100%;
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
align-items: center;
}
.lotteryBox ul li {
width: 120px;
height: 120px;
border-radius: 5px;
overflow: hidden;
}
.lotteryBox ul li img {
width: 100%;
height: 100%;
display: block;
}
#lotteryBtn {
cursor: pointer;
opacity: 0.9;
}
#lotteryBtn:hover {
opacity: 1;
}
.active img {
opacity: 0.4;
}
#lotteryBtnStop {
opacity: 0.3;
}
#lotteryResult {
position: absolute;
left: 10%;
bottom: -67px;
width: 80%;
height: 60px;
background-color: rgba(0, 0, 0, .5);
text-align: center;
color: white;
line-height: 60px;
}
</style>
</head>
<body>
<div class="lotteryBox">
<ul>
<li class="item"><img src="images/1.jpg" alt=""></li>
<li class="item"><img src="images/2.jpg" alt=""></li>
<li class="item"><img src="images/3.jpg" alt=""></li>
<li class="item"><img src="images/4.jpg" alt=""></li>
<li id="lotteryBtn"><img src="images/10.jpg" alt=""></li>
<li class="item"><img src="images/5.jpg" alt=""></li>
<li class="item"><img src="images/6.jpg" alt=""></li>
<li class="item"><img src="images/7.jpg" alt=""></li>
<li class="item"><img src="images/8.jpg" alt=""></li>
</ul>
<div id="lotteryResult">等待抽奖...</div>
</div>
<!-- 内部脚本代码 -->
<script>
var lotteryBtn = document.getElementById('lotteryBtn');
var lotteryLi = document.getElementsByClassName('item');
var lotteryResult = document.getElementById('lotteryResult');
var directionArray = [0, 1, 2, 4, 7, 6, 5, 3];
var lotteryIndex = 0;
var lotteryStatus = true;
var timer = null;
lotteryBtn.onclick = function () {
if (lotteryStatus) {
lotteryStatus = false;
lotteryBtn.id = 'lotteryBtnStop';
timer = setInterval(function () {
lotteryLi[directionArray[lotteryIndex]].className = "item";
lotteryIndex++;
if (lotteryIndex >= 8) {
lotteryIndex = 0;
}
lotteryLi[directionArray[lotteryIndex]].className = "item active"
}, 100)
setTimeout(function () {
clearInterval(timer);
if (lotteryIndex == 3) {
lotteryLi[directionArray[lotteryIndex]].className = "item";
lotteryLi[directionArray[lotteryIndex + 1]].className = "item active";
}
lotteryStatus = true;
document.getElementById('lotteryBtnStop').id = 'lotteryBtn'
if (lotteryIndex == 0) {
lotteryResult.innerText = '感谢参与'
} else if (lotteryIndex == 1) {
lotteryResult.innerText = '恭喜你中了玫瑰'
} else if (lotteryIndex == 2) {
lotteryResult.innerText = '恭喜你中了盲盒'
} else if (lotteryIndex == 3) {
lotteryResult.innerText = '恭喜你中了一个小姐姐'
} else if (lotteryIndex == 4) {
lotteryResult.innerText = '恭喜你中了一个小姐姐'
} else if (lotteryIndex == 5) {
lotteryResult.innerText = '恭喜你中了耳机'
} else if (lotteryIndex == 6) {
lotteryResult.innerText = '恭喜你中了一个小哥哥'
} else if (lotteryIndex == 7) {
lotteryResult.innerText = '恭喜你中了积分'
}
}, Math.round(Math.random() * 3000) + 1000)
} else {
console.log('无法点击,正在抽奖中')
}
}
</script>
</body>
</html>
demo4
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
list-style: none;
text-decoration: none;
}
.luckyDraw {
width: 420px;
margin: 0 auto;
}
.prize {
font-size: 30px;
}
.lottery {
width: 240px;
margin: 30px auto;
position: relative;
}
.lottery li {
width: 80px;
height: 80px;
border: 1px solid #000;
box-sizing: border-box;
text-align: center;
line-height: 80px;
position: absolute;
}
.lottery li:nth-of-type(1) {
left: 0;
top: 0;
}
.lottery li:nth-of-type(2) {
left: 80px;
top: 0;
}
.lottery li:nth-of-type(3) {
left: 160px;
top: 0;
}
.lottery li:nth-of-type(4) {
left: 160px;
top: 80px;
}
.lottery li:nth-of-type(5) {
left: 160px;
top: 160px;
}
.lottery li:nth-of-type(6) {
left: 80px;
top: 160px;
}
.lottery li:nth-of-type(7) {
left: 0;
top: 160px;
}
.lottery li:nth-of-type(8) {
left: 0;
top: 80px;
}
.lottery li:nth-of-type(9) {
left: 80px;
top: 80px;
cursor: pointer;
background: gold;
}
.active:after {
position: absolute;
top: 0;
left: 0;
content: "";
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.1);
}
</style>
</head>
<body>
<div class="luckyDraw">
<ul class="lottery">
<li class="active">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li onclick="roll()">点击抽奖</li>
</ul>
<span class="prize">奖品</span>
</div>
<script>
var arr = [1, 2, 3, 4, 5, 6, 7, 8];
var lottery = document.querySelector('.lottery');
var prize = document.querySelector('.prize');
var ali = lottery.querySelectorAll('li');
var i = 0;
var count = 0;
var totalCount = 9;
var speed = 500;
var minSpeed = 500;
var timer;
var isClick = true;
var index = 5;
function roll() {
speed -= 50;
if (speed <= 10) {
speed = 10;
}
for (var j = 0; j < ali.length; j++) {
ali[j].classList.remove('active');
}
i++;
if (i >= ali.length - 1) {
i = 0;
count++;
}
prize.innerHTML = arr[i];
ali[i].classList.add('active');
if (count >= totalCount && (i + 1) == index) {
clearTimeout(timer);
isClick = true;
speed = minSpeed;
} else {
timer = setTimeout(roll, speed);
if (count >= totalCount - 1 || speed <= 50) {
speed += 100;
}
}
}
ali[ali.length - 1].onclick = function () {
if (isClick) {
count = 0;
roll();
isClick = false;
}
}
</script>
</body>
</html>
demo5
<!DOCTYPE html>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<html>
<style>
li {
width: 200px;
height: 200px;
}
.ul {
width: 606px;
height: 606px;
}
.ul li {
float: left;
border: 1px solid #000000;
list-style: none;
line-height: 200px;
text-align: center;
font-size: 50px;
}
</style>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="ul">
<li class="li1">1</li>
<li class="li2">2</li>
<li class="li3">3</li>
<li class="li8">8</li>
<li class="listart">开始</li>
<li class="li4">4</li>
<li class="li7">7</li>
<li class="li6">6</li>
<li class="li5">5</li>
</div>
<script type="text/javascript" src="js/jquery-1.12.4.js"></script>
<script>
var last_index = 0,
amplification_index = 0,
roll_flag = true,
max_number = 8,
speed = 300,
finalindex = 3,
myInterval = "",
max_speed = 40,
minturns = 8,
runs_now = 0;
$(".listart").bind("click", function () {
runs_now = 0;
if (roll_flag) {
roll_flag = false;
rolling();
}
});
function rolling() {
myInterval = setTimeout(function () {
rolling();
}, speed);
runs_now++;
amplification_index++;
var count_num = minturns * max_number + finalindex - last_index;
console.log(count_num);
if (runs_now <= (count_num / 3) * 2) {
speed -= 30;
if (speed <= max_speed) {
speed = max_speed;
}
}
else if (runs_now >= count_num) {
clearInterval(myInterval);
last_index = amplification_index;
roll_flag = true;
}
else if (count_num - runs_now <= 10) {
speed += 20;
}
else {
speed += 10;
if (speed >= 100) {
speed = 100;
}
}
if (amplification_index > max_number) {
amplification_index = 1;
}
var strli = ".li";
strli += amplification_index;
$("li").each(function () {
$(this).css("background", "#ffffff");
})
$(strli).css("background", "red");
}
</script>
</body>
</html>
demo6
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
ul {
margin: 0 auto;
padding: 0;
width: 576px;
height: 513px;
border: 4px solid red;
font-size: 0;
}
li,
a {
display: inline-block;
width: 190px;
height: 170px;
border: 0.5px solid red;
font-size: 14px;
}
ul li .mask {
width: 190px;
height: 170px;
position: relative;
left: 0;
top: -172px;
display: none;
box-shadow: 0px -5px 10px 0px white inset,
-5px 0px 10px 0px white inset,
5px 0px 10px 0px white inset,
0px 5px 10px 0px white inset;
}
img {
width: 100%;
height: 100%;
}
ul .active .mask {
display: block;
}
#page {
line-height: 32px;
color: #9a9a9a;
text-align: center;
position: absolute;
left: 50%;
top: 50px;
width: 300px;
height: 50px;
margin-left: -150px;
display: none;
background: #000;
color: #fff;
}
.act .mask {
display: block;
}
</style>
</head>
<body>
<ul>
<li><img class="active" src="images/6.jpg" alt="">
<div class="mask"></div>
</li>
<li><img src="images/1.jpg" alt="">
<div class="mask"></div>
</li>
<li><img src="images/2.jpg" alt="">
<div class="mask"></div>
</li>
<li><img src="images/3.jpg" alt="">
<div class="mask"></div>
</li>
<a><img src="images/4.jpg" alt=""></a>
<li><img src="images/5.jpg" alt="">
<div class="mask"></div>
</li>
<li><img src="images/6.jpg" alt="">
<div class="mask"></div>
</li>
<li><img src="images/7.jpg" alt="">
<div class="mask"></div>
</li>
<li><img src="images/8.jpg" alt="">
<div class="mask"></div>
</li>
</ul>
<div id="page"></div>
</body>
<script type="text/javascript">
var arr = [0, 1, 2, 4, 7, 6, 5, 3]
var arrLi = document.getElementsByTagName("li");
var oBtn = document.getElementsByTagName("a")[0];
var tim;
var index = 0
var swh = true
oBtn.onclick = function () {
if (swh) {
swh = false;
time = setInterval(function () {
arrLi[arr[index]].className = "";
index++;
if (index >= 8) {
index = 0;
}
arrLi[arr[index]].className = "active"
}, 50);
setTimeout(function () {
clearInterval(time);
if (index == 0) {
arrLi[arr[index]].className = "";
arrLi[arr[index + 1]].className = "active";
}
swh = true;
}, Math.random() * 500 + 1000)
}
}
</script>
</html>