例1
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,body{
width: 100%;
height: 100%;
}
#code{
font-family:Arial;
font-style:italic;
font-weight:bold;
border:0;
letter-spacing:2px;
color:blue;
}
</style>
</head>
<body>
<div>
<input type = "text" id = "input" value="" />
<input type = "button" id="code" onclick="createCode()"/>
<input type = "button" value = "验证" onclick = "validate()"/>
</div>
<script type="text/javascript">
//设置一个全局的变量,便于保存验证码
var code;
function createCode(){
//首先默认code为空字符串
code = '';
//设置长度,这里看需求,我这里设置了4
var codeLength = 4;
var codeV = document.getElementById('code');
//设置随机字符,长度为36
var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 'S','T','U','V','W','X','Y','Z');
// console.log(Math.floor(0.5)) //0
// console.log(Math.floor(3.7)) //3
// console.log(Math.floor(-0.4)) //-1
// console.log(Math.floor(-3.2)) //4
// console.log(Math.random()*36)
//循环codeLength 我设置的4就是循环4次
for(var i = 0; i < codeLength; i++){
//设置随机数范围,这设置为0 ~ 36,Math.floor对数字向下舍入
var index = Math.floor(Math.random()*36);
//字符串拼接 将每次随机的字符 进行拼接
code += random[index];
}
//将拼接好的字符串赋值给展示的Value
codeV.value = code;
}
//下面就是判断是否== 的代码,无需解释
function validate(){
var oValue = document.getElementById('input').value.toUpperCase();//value.toUpperCase()把字符窜转换为大写
if(oValue ==0){
alert('请输入验证码');
}else if(oValue != code){
alert('验证码不正确,请重新输入');
oValue = ' ';
createCode();
}else{
window.open('http://www.baidu.com','_self');
}
}
//设置此处的原因是每次进入界面展示一个随机的验证码,不设置则为空
window.onload = function (){
createCode();
}
</script>
</body>
</html>
例2:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script>
//创建一个div
var div = document.createElement("div");
//将div写入body中
document.body.appendChild(div);
//为div设置样式
div.style.width="800px";
div.style.height="400px";
div.style.position="relative";
div.style.border="1px solid red";
//div的点击事件
div.onclick = function(){
//创建一个box
var box = document.createElement("div");
//随机颜色
var r = parseInt(Math.random()*256);
var g = parseInt(Math.random()*256);
var b = parseInt(Math.random()*256);
//为box设置样式
box.style.width="20px";
box.style.height="20px";
box.style.borderRadius="50%";
box.style.position="absolute";
box.style.left="0px";
box.style.top="0px";
box.style.backgroundColor="rgb("+r+","+g+","+b+")";
//将box作为div的子集写入
div.appendChild(box);
//随机一个沿着x轴移动的初始值
var speedX=parseInt(Math.random()*10+1);
console.log(speedX);
//随机一个沿着y轴移动的初始值
var speedY=parseInt(Math.random()*10+1);
console.log(speedY);
//获得当前box的left值
var nowLeft = box.offsetLeft;
console.log(nowLeft);
//获得当前box的top值
var nowTop = box.offsetTop;
console.log(nowTop);
//获得小球x轴最大的移动距离
var maxWidth=div.offsetWidth-box.offsetWidth;
console.log(maxWidth);
//获得小球y轴最大的移动距离
var maxHeight=div.offsetHeight-box.offsetHeight;
console.log(maxHeight);
//计时器进入循环
setInterval(function(){
console.log(nowLeft);
//改变小球的left值
nowLeft+=speedX;
//当小球到达最右边时,获得的速度需要变为负值
if(nowLeft>=maxWidth){
speedX=parseInt(Math.random()*10+1);
speedX=-speedX;
}
//当小球再次到达最左边的时候,获得的速度是正值
if(nowLeft<=0){
speedX=parseInt(Math.random()*10+1);
}
//为小球设置left值
box.style.left=nowLeft+"px";
//改变小球的top值
nowTop+=speedY;
//当小球到达最底部的时候,速度变成负值
if(nowTop>=maxHeight){
speedY=parseInt(Math.random()*10+1);
speedY=-speedY;
}
//当小球再次到达最顶部小球的速度为正值
if(nowTop<=0){
speedY=parseInt(Math.random()*10+1);
}
//为小球设置top值
box.style.top=nowTop+"px";
},30);
}
</script>
</html>