HTML + CSS + JS 实现随机抽奖

HTML + CSS + JS 实现随机抽奖

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #wrap{
            text-align: center;
            width:500px;
            margin: 100px auto;
            position: relative;
        }
        #ul1{
            width: 303px;
            height: 303px;
            margin: 50px auto;
            padding:0;
            border-top:1px solid black;
            border-left: 1px solid black; 
        }
        #ul1 li{
            float: left;
            border-right: 1px solid black;
            border-bottom: 1px solid black;
            list-style: none;
            width: 100px;
            height: 100px;
            line-height: 100px;
            text-align: center;
        }
        #tooltips{
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            position: absolute;
            top: 0;
            z-index: 999;
            display: none;
        }
        #info{
            width: 400px;
            height: 200px;
            background-color: white;
            margin: 150px auto;
        }
        #info .title{
            width: 100%;
            height: 40px;
            background-color: #009f95;
            line-height: 40px;
            color: white;
            padding-left: 20px;
            box-sizing: border-box;
        }
        #info .btn button{
            background-color: #009f95;
            color: white;
            outline: none;
            font-size: 10px;
            width:60px;
            height: 30px;
            margin-left: 300px;
        }
        #info .content {
            height: 120px;
            padding: 20px;
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <div id="wrap">
        <button id="btn">开始抽奖</button>
		<button id="btn2">停止</button>
        <ul id="ul1">
            <li>鼠标</li>
            <li>1000万</li>
            <li>100优惠券</li>
            <li>很遗憾</li>
            <li>键盘</li>
            <li>iphoneX</li>
            <li>很遗憾</li>
            <li>迪拜10日游</li>
            <li>很遗憾</li>
        </ul>
    </div>
    <!-- 提示信息 -->
    <div id="tooltips">
        <div id="info">
            <div class="title">信息</div>
            <div class="content" id="content-z">恭喜你,中奖了</div>
            <div class="btn">
                <button id="confirm">确定</button>
            </div>
        </div>
    </div>
    <script type="text/javascript">
    	//思路: 1 实现红色背景切换  2 当运动停止,弹出对话框--用js去修改tooltips的display 变为block
		var oStart = document.getElementById("btn")	
		var aLi = document.getElementsByTagName("li")
		var oStop = document.getElementById("btn2")
		var oTooltips = document.getElementById("tooltips")
		var oConfirm = document.getElementById("confirm")
		var oContentz = document.getElementById("content-z")
		var nowIndex = 0
		var timer = null
		oStart.onclick = function(){
			clearInterval(timer)
			timer = setInterval(function(){
				for(var i = 0;i<aLi.length;i++){
					aLi[i].style.backgroundColor = "white"
				}
				aLi[nowIndex].style.backgroundColor = "red"
				nowIndex = (nowIndex+1) % (aLi.length)
			},10)
		}
		oStop.onclick = function(){
			clearInterval(timer)
			oTooltips.style.display = "block"
			var zhong_jiang = aLi[nowIndex].innerText
			console.log(zhong_jiang)
			if(zhong_jiang == "很遗憾"){
				oContentz.innerText = "很遗憾,没有中奖"
			}else{
				oContentz.innerText = "恭喜你,抽中了" + zhong_jiang + "!" 
			}
		}
		oConfirm.onclick = function(){
			oTooltips.style.display = "none"
			for(var i = 0;i<aLi.length;i++){
				aLi[i].style.backgroundColor = "white"
			}
		}
	</script>
</body>
</html>

在这里插入图片描述

好的,以下是一个使用HTMLCSSJS实现抽奖转盘: HTML代码: ```html <div class="container"> <div class="wheel"> <div class="section"><span>1</span></div> <div class="section"><span>2</span></div> <div class="section"><span>3</span></div> <div class="section"><span>4</span></div> <div class="section"><span>5</span></div> <div class="section"><span>6</span></div> </div> <button class="btn" onclick="start()">开始抽奖</button> </div> ``` CSS代码: ```css .container { display: flex; justify-content: center; align-items: center; height: 100vh; background-color: #f2f2f2; } .wheel { position: relative; width: 300px; height: 300px; border-radius: 50%; background-color: #ff9966; overflow: hidden; transform: rotate(0deg); transition: transform 6s ease-in-out; } .section { position: absolute; width: 100%; height: 100%; text-align: center; font-size: 30px; font-weight: bold; color: #fff; line-height: 300px; transition: transform 1s ease-in-out; } .section:nth-child(1) { transform: rotate(0deg); background-color: #ffcc99; } .section:nth-child(2) { transform: rotate(60deg); background-color: #ff9933; } .section:nth-child(3) { transform: rotate(120deg); background-color: #ffcc33; } .section:nth-child(4) { transform: rotate(180deg); background-color: #ff6633; } .section:nth-child(5) { transform: rotate(240deg); background-color: #ff9933; } .section:nth-child(6) { transform: rotate(300deg); background-color: #ffcc99; } .btn { margin-top: 30px; padding: 10px 20px; font-size: 20px; font-weight: bold; color: #fff; background-color: #ff9933; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease-in-out; } .btn:hover { background-color: #ffcc33; } ``` JS代码: ```javascript var deg = 0; var speed = 0; var prize = ''; function start() { // 生成一个随机速度 speed = Math.floor(Math.random() * 20) + 10; // 生成一个随机奖品 prize = Math.floor(Math.random() * 6) + 1; // 计算需要旋转的角度 deg = 360 / 6 * (prize - 1) + 360 / 6 / 2 - Math.random() * 360 / 6; // 开始旋转转盘 var wheel = document.querySelector('.wheel'); wheel.style.transform = 'rotate(' + deg + 'deg)'; wheel.style.transition = 'transform 6s ease-in-out'; // 6秒后停止旋转 setTimeout(function() { stop(); }, 6000); } function stop() { // 停止旋转 var wheel = document.querySelector('.wheel'); wheel.style.transform = 'rotate(' + deg + 'deg)'; wheel.style.transition = 'transform 0s'; // 显示中奖结果 alert('恭喜您获得:' + document.querySelector('.section:nth-child(' + prize + ') span').innerHTML); } ``` 这个抽奖转盘使用了CSS3的旋转效果来实现转盘的转动,使用JS生成随机速度和奖品,并计算需要旋转的角度。在点击开始抽奖按钮后,将转盘旋转到指定角度,并在6秒后停止旋转,并弹出中奖结果提示框。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值