前言:
有时候我也挺佩服我这沙雕的大脑,明明敲了很长时间的代码,
晚上超级瞌睡时,躺在床上居然睡不着了。
总能想写莫名其妙的东西,这个点名系统是我也不知道
起因是什么想出来的,当时想到了,
就直接用备忘录记录下来,怎么实现,
用那些知识点去实现,都能想出来。
但有时可能会与我想象的不一样,
这些需要在代码实现的过程中一点一点修改。
本来对于CSS的样式,我也想过美化的好看点,奈何审美有限,
在测试的颜色中,完了之后发现挺协调的,也就懒得再去修改了。
这个案例不是太难,主要就锻炼DOM的添加元素,获取元素,
数组的增删以及字符串与数组之间的转换。
先放一张成品图,如果其中的样式不喜欢,可以自己改变,或者直接向我的git提交申请。
点击链接查看具体效果点我查看效果
实现的草稿:
这个是当时睡觉前大脑的想法。
这个是我开始进行代码编写时,写出来的分析(字有些丑,请不要介意):
虽然做出了分析,但是代码编写时,发现缺漏了很多东西,也就没有去写出来,直接代码实现了。
下面直接上代码。
HTML代码:
<body class="clearfix">
<h1>随机点名小测试</h1>
<!-- 输入按钮 -->
<input type="button" value="输入" class="input">
<!-- 删除按钮 -->
<input type="button" value="删除" class="del">
<!-- 清空按钮 -->
<input type="button" value="清空" class="delAll">
<!-- 创建花名册 -->
<div class="box">
<h2>花名册</h2>
<ul></ul>
</div>
<!-- 随机点名 -->
<input type="button" value="点名" class="rollCall">
<!-- 创建点名的名单 -->
<h2 class="boxs_h2">幸运儿</h2>
<div class="boxs"></div>
<div class="point">
<dl>
<dt>说明:</dt>
<dd>请用PC端打开,移动端没学,暂时不会做</dd>
<dd>花名册中显示当前可以随机点到的名字</dd>
<dd>点击输入按钮可以进行输入姓名,可单个输入,也可多个输入,中间用'-'隔开</dd>
<dd>点击删除可以删除花名册中最后一个姓名</dd>
<dd>点击清空可以删除花名册中所有的姓名</dd>
<dd>点击点名,会在幸运儿中随机产出一名幸运儿</dd>
<dd>若发现bug请联系1419148453@qq.com</dd>
</dl>
</div>
</body>
CSS代码:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
ul,
li {
list-style: none;
}
.clearfix::after,
.clearfix::before {
content: '';
display: table;
}
.clearfix::after {
clear: both;
}
input {
position: absolute;
display: inline-block;
width: 150px;
height: 80px;
border: 0;
margin-bottom: 5px;
background-color: #d38d72;
border-radius: 15px;
z-index: 99;
font-size: 20px;
}
input:hover {
background-color: #5cebeb;
}
h1 {
margin: 30px auto;
text-align: center;
color: #c5e775;
}
.input {
top: 100px;
left: 20px;
}
.rollCall {
top: 200px;
left: 20px;
}
.del {
top: 100px;
right: 20px;
}
.delAll {
top: 200px;
right: 20px;
}
.box {
position: absolute;
width: 200px;
background-color: pink;
top: 300px;
right: 20px;
text-align: center;
border-radius: 20px;
max-height: 330px;
}
.box ul li {
line-height: 25px;
}
.box h2 {
line-height: 40px;
color: #d38d72;
}
.boxs {
position: absolute;
width: 200px;
height: 100px;
background-color: pink;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -100px;
border-radius: 20px;
text-align: center;
line-height: 100px;
}
.boxs_h2 {
position: absolute;
width: 200px;
text-align: center;
color: #d38d72 ;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -100px;
}
.point {
position: absolute;
color: #ccc;
font-size: 12px;
top: 80%;
left: 40%;
}
背景动画的代码(js实现,代码我也忘记从哪个地方搜到的)这些东西我也看不懂,直接拿来就用的:
window.onload = function () {
//定义body的margin由默认值8px->0px
document.body.style.margin = "0";
document.body.style.background = "#30333F";
//创建canvas画布
document.body.appendChild(document.createElement('canvas'));
var canvas = document.querySelector('canvas'),
ctx = canvas.getContext('2d') //ctx返回一个在canvas上画图的api/dom
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
canvas.style.position = 'fixed';
ctx.lineWidth = .3;
ctx.strokeStyle = (new Color(150)).style;
//定义鼠标覆盖范围
var mousePosition = {
x: 30 * canvas.width / 100,
y: 30 * canvas.height / 100
};
var dots = {
nb: 1000,//Dot的总数
distance: 50,
d_radius: 100,
array: []
};
//创建颜色类,Color类返回字符串型rgba(*,*,*,.8)
function mixComponents(comp1, weight1, comp2, weight2) {
return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
}
function averageColorStyles(dot1, dot2) {
var color1 = dot1.color,
color2 = dot2.color;
var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
}
function colorValue(min) {
return Math.floor(Math.random() * 255 + min);
}
function createColorStyle(r, g, b) {
return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
}
function Color(min) {
min = min || 0;
this.r = colorValue(min);
this.g = colorValue(min);
this.b = colorValue(min);
this.style = createColorStyle(this.r, this.g, this.b);
}
//创建Dot类以及一系列方法
function Dot() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.vx = -.5 + Math.random();
this.vy = -.5 + Math.random();
this.radius = Math.random() * 2;
this.color = new Color();
}
Dot.prototype = {
draw: function () {
ctx.beginPath();
ctx.fillStyle = this.color.style;
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
ctx.fill();
}
};
function moveDots() {//Dot对象的移动
for (i = 0; i < dots.nb; i++) {
var dot = dots.array[i];
if (dot.y < 0 || dot.y > canvas.height) {
dot.vx = dot.vx;
dot.vy = - dot.vy;
}
else if (dot.x < 0 || dot.x > canvas.width) {
dot.vx = - dot.vx;
dot.vy = dot.vy;
}
dot.x += dot.vx;
dot.y += dot.vy;
}
}
function connectDots() {//DOt对象的连接
for (i = 0; i < dots.nb; i++) {
for (j = i; j < dots.nb; j++) {
i_dot = dots.array[i];
j_dot = dots.array[j];
if ((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > - dots.distance && (i_dot.y - j_dot.y) > - dots.distance) {
if ((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > - dots.d_radius && (i_dot.y - mousePosition.y) > - dots.d_radius) {
ctx.beginPath();
ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
ctx.moveTo(i_dot.x, i_dot.y);
ctx.lineTo(j_dot.x, j_dot.y);
ctx.stroke();//绘制定义的路线
ctx.closePath();//创建从当前点回到起始点的路径
}
}
}
}
}
function createDots() {//创建nb个Dot对象
for (i = 0; i < dots.nb; i++) {
dots.array.push(new Dot());
}
}
function drawDots() {//引用Dot原型链,使用draw方法,在canvas上画出Dot对象
for (i = 0; i < dots.nb; i++) {
var dot = dots.array[i];
dot.draw();
}
}
function animateDots() {
ctx.clearRect(0, 0, canvas.width, canvas.height);//清除画布,否则线条会连在一起
moveDots();
connectDots();
drawDots();
requestAnimationFrame(animateDots);
}
createDots();//使用创建Dot类函数
requestAnimationFrame(animateDots);//使用canvas独有的60Hz刷新屏幕画布的方法
document.querySelector('canvas').addEventListener('mousemove', function (e) {
mousePosition.x = e.pageX;
mousePosition.y = e.pageY;
})
document.querySelector('canvas').addEventListener('mouseleave', function (e) {//鼠标离开时,连接自动返回到画布中心
mousePosition.x = canvas.width / 2;
mousePosition.y = canvas.height / 2;
})
}
功能实现js:
window.addEventListener('load', function () {
// 定义数组来存放点名的信息
let names = [];
let inputs = null;
// 获取信息
let input = document.querySelector('.input');
let del = document.querySelector('.del');
let delAll = document.querySelector('.delAll');
let box = document.querySelector('.box');
let ul = box.querySelector('ul');
let rollCall = document.querySelector('.rollCall');
let boxs = document.querySelector('.boxs');
if (names.length != 0) {
roster();
}
// 点击输入框后将输入的字符串添加到数组内
input.addEventListener('click', function () {
inputs = prompt('可以输入单个名字,也可输入多个名字,中间用-隔开:');
if (inputs == '') {
alert('输入不能为空');
} else if (inputs.indexOf('-') != -1) {
lis = inputs.split('-');
console.log(lis.length);
for (let i = 0; i < lis.length; i++) {
names.push(lis[i]);
}
roster();
} else {
names.push(inputs);
roster();
}
})
// 点击删除按钮,删除数组中的最后一个姓名
del.addEventListener('click', function () {
names.pop();
roster();
})
// 点击清空,删除数组内所有的名字
delAll.addEventListener('click', function () {
names = [];
// for (let i = names.length; i > 0; i--) {
// names.pop();
// }
roster();
})
// 创建花名册,用li来保存数组内的数据
function roster() {
for (let i = ul.children.length; i > 0; i--) {
ul.removeChild(ul.children[ul.children.length - 1]);
}
for (let i = 0; i < names.length; i++) {
let li = document.createElement('li');
li.innerHTML = names[i];
ul.appendChild(li);
}
}
// 生成随机数
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}
// 随机点名
rollCall.addEventListener('click', function () {
let num = getRandom(0, names.length - 1);
boxs.innerHTML = names[num];
})
})
ok了,今天的分享就到这里了,关于背景,
大家如果不喜欢这个背景,可以自行去找别的背景,
将其换掉就可以了。
这几天正在看视频,跟着做小米闪购的页面,感觉这个适合初学者,
因为我也是初学者,如果你想找项目练手的话,我推荐这个。
如果喜欢的话,就点个收藏吧。