吃啥大转盘

经常跟朋友出去吃饭,选择太困难了所以写了个简单的转盘,直接copy到txt文本然后把文件后缀改成html即可。

需要换食物直接在文件中找到 list 值按照格式替换一下即可。

效果:

代码块:

html 复制代码
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>吃啥?</title>

		<style type="text/css">
			body {
				text-align: center;
			}

			button {
				cursor: pointer;
			}

			.box {
				width: 500px;
				height: 500px;
				margin: 10px auto;
				position: relative;
				overflow: hidden;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.main {
				width: 100%;
				height: 100%;
				box-sizing: border-box;
				border: 5px solid black;
				border-radius: 50%;
				background: rgba(128, 128, 128, 0.49);
				overflow: hidden;
				position: relative;
				transition: all 3s cubic-bezier(0.46, 0.03, 0.52, 0.96);
			}

			.pointer {
				width: 50px;
				height: 50px;
				background: white;
				border-radius: 50%;
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);
				z-index: 9;
			}

			.pointer-bar {
				width: 60px;
				height: 25px;
				background: white;
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(0, -50%);
			}

			.pointer-cursor {
				width: 25px;
				height: 25px;
				position: absolute;
				left: 85px;
				top: 50%;
				transform: translate(-50%, -50%) rotateZ(45deg);
				background: white;
			}

			.part {
				height: 100%;
				width: 50%;
				position: absolute;
				top: 0;
				left: 50%;
				transform-origin: left center;
				z-index: 1;
			}

			.bg {
				width: 100%;
				height: 100%;
			}

			.title {
				transform: translate(0, -50%);
				transform-origin: left top;
				width: 100%;
				height: auto;
				text-align: center;
				font-size: 30px;
				color: white;
				position: absolute;
				top: 50%;
				left: 0;
				padding-left: 85px;
				box-sizing: border-box;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
			}

			.winner {
				font-size: 30px;
				color: red;
				font-weight: bold;
			}
		</style>
	</head>

	<body>
		<div class="box">
			<div class="main" ontransitionend="end()">
				<div id="temp" class="part" style="display: none;">
					<div class="bg"></div>
					<div class="title"></div>
				</div>
			</div>

			<div class="pointer">
				<div class="pointer-bar"></div>
				<div class="pointer-cursor"></div>
			</div>
		</div>

		<button onclick="start()">开始</button>
		<div class="winner"></div>

		<script type="text/javascript">
			var rotate = 0;

			var random = function(num) {
				return Math.random() * num;
			};

			var randomColor = function() {
				return 'rgb(' + random(250) + ', ' + random(250) + ', ' + random(250) + ')';
			};

			var list = [
			  { title: '华莱士', color: randomColor() },
			  { title: '荣记', color: randomColor() },
			  { title: '猪脚饭', color: randomColor() },
			  { title: '螺蛳粉', color: randomColor() },
			  { title: '麻辣烫', color: randomColor() },
			  { title: '糖水', color: randomColor() },
			  { title: '麦当劳', color: randomColor() },
			  { title: '兰州拉面', color: randomColor() },
			  { title: '木桶饭', color: randomColor() },
			  { title: '再抽一次', color: randomColor() }
			];

			var perAngle = 360 / list.length;

			var main = document.querySelector('.main');
			var temp = document.querySelector('#temp');

			for (var i in list) {
				var item = list[i];

				var newNode = temp.cloneNode(true);
				newNode.style.display = 'block';
				newNode.style.transform = 'rotateZ(' + (perAngle * i + perAngle / 2) + 'deg)';
				newNode.querySelector('.bg').style.background = item.color;

				if (list.length > 2) {
					var p = this.perAngle / 2; // 每份的角度两等分
					var d = Math.tan(p * Math.PI / 180) * 100; // 对边的长度
					var x = (100 - d) / 2; // 每份对边实际百分比
					newNode.style.clipPath = `polygon(0% 50%, 100% ${x}%, 100% ${100 - x}%)`;
				}

				newNode.querySelector('.title').innerHTML = item.title;

				main.appendChild(newNode);
			}

			var isRunning = false;

			function start() {
				if (isRunning) {
					console.warn('isRunning');
					return;
				}

				isRunning = true;

				document.querySelector('.winner').innerHTML = '';

				rotate += random(360) + 360 * 5; // 多转5圈

				main.style.transform = 'rotateZ(-' + rotate + 'deg)';
			}

			function end() {
				isRunning = false;

				console.log(rotate);
				var index = Math.floor(rotate / perAngle) % list.length;
				var winner = list[index];
				document.querySelector('.winner').innerHTML = '去吃:' + winner.title;
			}
		</script>
	</body>
</html>
相关推荐
程序员黑豆4 小时前
Java包装类:基本类型与对象的桥梁
java·前端·ai编程
bugcome_com6 小时前
JWT 知识小课堂:从入门到精通,一文吃透 JSON Web Token
前端·json
To_OC7 小时前
写了三天 Next.js,我的 React 世界观被拆了又装回去
前端·全栈·next.js
独泪了无痕7 小时前
viewer.js 安装与配置指南:实现图片预览功能
前端·vue.js
起床学FPGA8 小时前
AI回答问题之后,会自动跳到最下面的问题
开发语言·javascript·ecmascript
Fluxart.ai9 小时前
电商商品图审核怎么自动化?规则引擎、人工复核与发布门禁
java·前端·自动化
why技术9 小时前
AI 写的文章,可能都带着手敲一遍都去不掉的“隐形水印”。
前端·人工智能·后端
愚公搬代码9 小时前
【愚公系列】《Android应用案例开发大全》016-LBS类应用掌上杭州(辅助工具类的开发)
android·前端
CodeSheep9 小时前
又一个华为天才少年,离职了!
前端·后端·程序员
kyriewen10 小时前
面试官说"打开你的AI工具"——我才发现,他考的根本不是写代码
前端·人工智能·面试