欢迎各位兄弟 发布技术文章
这里的技术是共享的
<?php
/**
* Created by PhpStorm.
* User: 16666739
* Date: 2025/4/18
* Time: 8:44
*/
$arr=[
'aaaa',
'bbbb',
'cccc',
'dddd',
'eeee',
'ffff',
'gggg',
'hhhh',
'iiii',
];
echo "<span style='color:red;'>所有人: </span>".implode(",",$arr);
$rand= array_rand ( $arr ,3 );
echo "<br/>";
$new_arr=array();
foreach($rand as $key=>$value)
{
$new_arr[] = $arr[$value];
}
echo "<span style='color:red;'>随机值班人: </span>".implode(",",$new_arr);
在网页上 用 转盘的形式实现上面的功能
下面是一个用于在网页上实现转盘随机选择功能的完整示例,使用HTML、CSS和JavaScript。
我们将创建一个简单的HTML结构,其中包括一个用于转盘的画布和触发旋转的按钮。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>随机选择器</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
canvas {
border: 2px solid #d3d3d3;
border-radius: 50%;
}
#spinButton {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
#result {
margin-top: 20px;
font-size: 20px;
color: red;
}
</style>
</head>
<body>
<h1>随机值班人选择器</h1>
<canvas id="spinner" width="400" height="400"></canvas>
<button id="spinButton">旋转选择</button>
<div id="result"></div>
<script>
const names = ['aaaa', 'bbbb', 'cccc', 'dddd', 'eeee', 'ffff', 'gggg', 'hhhh', 'iiii'];
const canvas = document.getElementById('spinner');
const ctx = canvas.getContext('2d');
let spinning = false;
function drawWheel() {
const sections = names.length;
const angle = (2 * Math.PI) / sections;
const colors = ['#FF5733', '#33FF57', '#3357FF', '#FF33A1', '#FFC300', '#DAF7A6', '#581845', '#C70039', '#900C3F'];
for (let i = 0; i < sections; i++) {
ctx.beginPath();
ctx.moveTo(200, 200);
ctx.arc(200, 200, 150, angle * i, angle * (i + 1));
ctx.fillStyle = colors[i % colors.length];
ctx.fill();
ctx.save();
ctx.translate(200, 200);
ctx.rotate(angle * i + angle / 2);
ctx.fillStyle = 'white';
ctx.fillText(names[i], 70, 10);
ctx.restore();
}
}
function spin() {
if (spinning) return;
spinning = true;
const degrees = Math.floor(Math.random() * 360 + 3600);
const spins = degrees * (Math.PI / 180);
const animationDuration = 5000; // 旋转5秒
let start = null;
function animate(timestamp) {
if (!start) start = timestamp;
const progress = timestamp - start;
const angle = spins * (progress / animationDuration);
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawWheel();
ctx.translate(200, 200);
ctx.rotate(angle);
ctx.translate(-200, -200);
ctx.drawImage(canvas, 0, 0);
if (progress < animationDuration) {
requestAnimationFrame(animate);
} else {
selectRandomName(degrees % 360);
spinning = false;
}
}
requestAnimationFrame(animate);
}
function selectRandomName(finalAngle) {
const index = Math.floor((finalAngle + 20) / (360 / names.length)) % names.length; // 根据角度调整
document.getElementById('result').innerText = "随机值班人: " + names[index];
}
document.getElementById('spinButton').onclick = spin;
drawWheel(); // 初始绘制
</script>
</body>
</html>
HTML元素:
使用<canvas>
来绘制转盘。
一个按钮用于触发旋转。
一个<div>
用于展示选定的姓名。
CSS样式:
简单的样式使转盘和按钮居中并美观。
JavaScript逻辑:
drawWheel
函数绘制转盘,每个节代表一个名字。
spin
函数负责处理转盘的动画和随机角度。
selectRandomName
函数根据转盘停止时的角度确定展示的姓名。
该实现创建了一个交互式转盘,可以从预定义的名单中随机选择一个人。转盘在预设持续时间内旋转,然后随机停下,显示选定的人员,为用户提供了一个生动且动态的体验。