[html]使用css绘制一个抽奖转盘

使用html、css3、js绘制一个抽奖转盘

 

1.画一个圆形container

设置溢出内容隐藏,并且使其内部元素靠上半部分居中

<div class="container">

</div>
.container{
   position: relative;
   width: 300px;
   height: 300px;
   border-radius: 50%;
   background: #f7c894;
   display: flex;
   justify-content: center;
   overflow: hidden;
   border: 20px solid #ffbd72;
}

2.我们添加一个扇形元素 宽度 暂时设为100px

使用剪切路径的css属性中的多边形方法 clip-path: polygon() 来画这个扇形部分

改变旋转用的中心点 transform-origin: 50% 100%;

<div class="container">
   <div class="content">
       <div class="fan-blade"></div>
   </div>
</div>
.content{
   position: relative;
   width: 100%;
   height: 100%;
   display: flex;
   justify-content: center;
   border-radius: 50%;
   overflow: hidden;
}
.fan-blade{
   position: absolute;
   width: 100px;
   height: 50%;
   clip-path: polygon(50% 100%, 0% 0%, 100% 0%);
   -webkit-clip-path: -webkit-polygon(50% 100%, 0% 0%, 100% 0%);
   transform-origin: 50% 100%;
}

3.我们将扇形复制8份,每个依次旋转45deg,并且单数子元素和双数子元素使用不同的背景颜色

.container .fan-blade:nth-child(1){
            transform: rotateZ(45deg);
        }
        .container .fan-blade:nth-child(2){
            transform: rotateZ(90deg);
        }
        .container .fan-blade:nth-child(3){
            transform: rotateZ(135deg);
        }
        .container .fan-blade:nth-child(4){
            transform: rotateZ(180deg);
        }
        .container .fan-blade:nth-child(5){
            transform: rotateZ(225deg);
        }
        .container .fan-blade:nth-child(6){
            transform: rotateZ(270deg);
        }
        .container .fan-blade:nth-child(7){
            transform: rotateZ(315deg);
        }
        .container .fan-blade:nth-child(8){
            transform: rotateZ(360deg);
        }

        .container .fan-blade:nth-child(odd){
            background-color: #f7c894;
        }
        .container .fan-blade:nth-child(even){
            background-color: #ffebd4;
        }

4.这时扇形之间还有很大的空间,我们使用js来计算正确的宽度应该是多少,并把宽度设置到对应的扇形元素上

//设置一个选择器
let $ = function(selector){return document.querySelectorAll(selector)}

let num = 8             //个数
let diameter = 300      //转盘直径

let width = 0           //扇叶元素宽度
let deg = 360 / num     //每一叶的旋转角度

width = diameter * Math.tan((deg/2) * Math.PI/180)

$('.fan-blade').forEach(e=>{
    e.style.width = width + 'px'
})

5.给转盘加上小彩灯

<div class="lights-content">
    <div class="lights"><div class="light"></div></div>
    <div class="lights"><div class="light"></div></div>
    <div class="lights"><div class="light"></div></div>
    <div class="lights"><div class="light"></div></div>
    <div class="lights"><div class="light"></div></div>
    <div class="lights"><div class="light"></div></div>
    <div class="lights"><div class="light"></div></div>
    <div class="lights"><div class="light"></div></div>
</div>
.lights-content{
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    border-radius: 50%;
}
.lights{
    position: absolute;
    width: 16px;
    height: 50%;
    transform-origin: 50% 100%;
}
.light{
    position: absolute;
    top: -16px;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    
}
    
.lights-content .lights:nth-child(1){
    transform: rotateZ(45deg);
}
.lights-content .lights:nth-child(2){
    transform: rotateZ(90deg);
}
.lights-content .lights:nth-child(3){
    transform: rotateZ(135deg);
}
.lights-content .lights:nth-child(4){
    transform: rotateZ(180deg);
}
.lights-content .lights:nth-child(5){
    transform: rotateZ(225deg);
}
.lights-content .lights:nth-child(6){
    transform: rotateZ(270deg);
}
.lights-content .lights:nth-child(7){
    transform: rotateZ(315deg);
}
.lights-content .lights:nth-child(8){
    transform: rotateZ(360deg);
}

.lights-content .lights:nth-child(odd) .light{
    box-shadow: 0 0 7.5px #ff0200;
	background-color: #ff0200;
	animation: lottery_light1 0.75s ease-in-out infinite;
}
.lights-content .lights:nth-child(even) .light{
    box-shadow: 0 0 7.5px #ffffff;
	background-color: #ffffff;
	animation: lottery_light2 0.75s ease-in-out infinite;
}

@keyframes lottery_light1 {
	0%{
		box-shadow: 0 0 7.5px #ff0200;
		background-color: #ff0200;
	}
	10%{
		box-shadow: 0 0 7.5px #ff0200;
		background-color: #ff0200;
	}
	40%{
		box-shadow: 0 0 7.5px #ffffff;
		background-color: #ffffff;
	}
	60%{
		box-shadow: 0 0 7.5px #ffffff;
		background-color: #ffffff;
	}
	90%{
		box-shadow: 0 0 7.5px #ff0200;
		background-color: #ff0200;
	}
	100%{
		box-shadow: 0 0 7.5px #ff0200;
		background-color: #ff0200;
	}
}

@keyframes lottery_light2 {
	0%{
		box-shadow: 0 0 7.5px #ffffff;
		background-color: #ffffff;
	}
	10%{
		box-shadow: 0 0 7.5px #ffffff;
		background-color: #ffffff;
	}
	40%{
		box-shadow: 0 0 7.5px #ff0200;
		background-color: #ff0200;
	}
	60%{
		box-shadow: 0 0 7.5px #ff0200;
		background-color: #ff0200;
	}
	90%{
		box-shadow: 0 0 7.5px #ffffff;
		background-color: #ffffff;
	}
	100%{
		box-shadow: 0 0 7.5px #ffffff;
		background-color: #ffffff;
	}
}

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,我可以为你提供一个简单的HTML、JS、CSS实现的抽奖转盘。 首先,我们需要一个 HTML 页面来放置大转盘的内容。 ```html <!DOCTYPE html> <html> <head> <title>抽奖转盘</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <div class="container"> <div class="wheel"> <div class="section"> <div class="content">1</div> </div> <div class="section"> <div class="content">2</div> </div> <div class="section"> <div class="content">3</div> </div> <div class="section"> <div class="content">4</div> </div> <div class="section"> <div class="content">5</div> </div> <div class="section"> <div class="content">6</div> </div> <div class="section"> <div class="content">7</div> </div> <div class="section"> <div class="content">8</div> </div> </div> <button class="spin" onClick="spin()">开始抽奖</button> </div> <script type="text/javascript" src="script.js"></script> </body> </html> ``` 接下来,我们需要一个 CSS 文件来设置大转盘的样式。 ```css .container { display: flex; flex-direction: column; align-items: center; } .wheel { position: relative; width: 400px; height: 400px; border-radius: 50%; background: url("https://cdn.pixabay.com/photo/2015/09/23/09/22/background-952755_960_720.jpg") no-repeat center center / cover; overflow: hidden; } .section { position: absolute; top: 0; left: 0; width: 100%; height: 100%; transform-origin: 50% 100%; } .section:nth-child(1) { transform: rotate(0deg); } .section:nth-child(2) { transform: rotate(45deg); } .section:nth-child(3) { transform: rotate(90deg); } .section:nth-child(4) { transform: rotate(135deg); } .section:nth-child(5) { transform: rotate(180deg); } .section:nth-child(6) { transform: rotate(225deg); } .section:nth-child(7) { transform: rotate(270deg); } .section:nth-child(8) { transform: rotate(315deg); } .content { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 80%; height: 80%; border-radius: 50%; background-color: #fff; display: flex; justify-content: center; align-items: center; font-size: 36px; font-weight: bold; } .spin { margin-top: 30px; padding: 10px 20px; border: none; border-radius: 5px; background-color: #f44336; color: #fff; font-size: 24px; cursor: pointer; } .spin:active { background-color: #e53935; } ``` 最后,我们需要一个 JS 文件来实现大转盘的旋转效果。 ```javascript var wheel = document.querySelector(".wheel"); var button = document.querySelector(".spin"); var sections = wheel.children; var deg = 0; function spin() { button.style.pointerEvents = "none"; deg = Math.floor(5000 + Math.random() * 5000); wheel.style.transition = "all 10s ease-out"; wheel.style.transform = "rotate(" + deg + "deg)"; wheel.addEventListener("transitionend", function() { button.style.pointerEvents = "auto"; wheel.style.transition = "none"; var actualDeg = deg % 360; sections[Math.floor(actualDeg / 45)].classList.add("winner"); alert("恭喜获得第" + (Math.floor(actualDeg / 45) + 1) + "个奖品!"); }); } function reset() { for (var i = 0; i < sections.length; i++) { sections[i].classList.remove("winner"); } } ``` 这样,一个简单的 HTML、JS、CSS 实现的抽奖转盘就完成了。你可以在浏览器中打开 HTML 文件,点击“开始抽奖”按钮来进行抽奖

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值