美文网首页
2020-08-03

2020-08-03

作者: 5d169b30b744 | 来源:发表于2020-08-06 09:32 被阅读0次

前端小白,初来乍到。封装了几个基础方法。希望大佬们指点!

  • 随机区间数方法
function randomNum(min,max){
    return Math.round(Math.random()*(max-min)+min);
}
  • 随机颜色方法
function randomColor(){
    let str="0123456789abcdef";
    let a="#";
    for(let i=0;i<6;i++){
        let random=randomNum(0,15);
        a+=str.charAt(random);
    }
    return a;
}
  • num位随机混合验证码方法

function randomCode(num){//随机生成num位数字字母混合验证码
    let code="";
    for(let i=0;i<num;i++){
        let str=String.fromCharCode(randomNum(48,122));//asc码随机
        if((str>='a'&&str<='z')||('0'<=str&&str<='9')||('A'<=str&&str<='Z')){
            code+=str;
        }else{
            i--;
        }
    }
    return code;
}
基于上面的方法做一个小demo,随机生成大小的小圆圈,碰壁运动,每次碰壁变色。
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0px;
                padding: 0px;
            }
            body{
                background-color:black;
            }
            div{
                position:absolute;
                border-radius: 50%;
                box-shadow: 1px 1px 5px 5px rgba(211,211,211,.5);
                text-align: center;
                color: #F6F9FC;
                font-size: 24px;
            }
        </style>
    </head>
    <body>
    </body>
</html>
<script src="hubble.js"></script>
<script type="text/javascript">
        let time=null;
        let count=1;
        time=setInterval(function(){
            clearInterval("time");
            var obox=document.createElement("div");
            document.body.appendChild(obox);
            obox.style.backgroundColor=randomColor();
            obox.style.width=randomNum(60,150)+"px";
            obox.style.height=obox.offsetWidth+"px";
            obox.style.lineHeight=obox.offsetHeight+"px";
            obox.innerText=count;
            // if(count==30){//停止个数
            //  clearInterval(time);
            // }
            touch(obox);
            count++;
        },1000);
</script>
/* hubble.js文件代码*/
function randomColor(){//随机颜色
    let str="0123456789abcdef";
    let a="#";
    for(let i=0;i<6;i++){
        let random=randomNum(0,15);
        a+=str.charAt(random);
    }
    return a;
}

function randomNum(min,max){//随机区间数
    return Math.round(Math.random()*(max-min)+min);
}

function randomCode(num){//随机生成num位数字字母混合验证码
    let code="";
    for(let i=0;i<num;i++){
        let str=String.fromCharCode(randomNum(48,122));//asc码随机
        if((str>='a'&&str<='z')||('0'<=str&&str<='9')||('A'<=str&&str<='Z')){
            code+=str;
        }else{
            i--;
        }
    }
    return code;
}

function touch(obj){//碰壁回弹
    let speedX=randomNum(2,12);
    let speedY=randomNum(5,15);
    let time=null;
    time=setInterval(function(){
        obj.style.top=obj.offsetTop+speedY+"px";
        obj.style.left=obj.offsetLeft+speedX+"px";
        if(obj.offsetTop<0){
            speedY*=-1;//碰到上边框换Y的方向
            obj.style.backgroundColor=randomColor();
        }
        if(obj.offsetLeft<0){
            speedX*=-1;//碰到左边框换X的方向
            obj.style.backgroundColor=randomColor();
        }
        let maxLeft=window.innerWidth-obj.offsetWidth-15;
        if(obj.offsetLeft>maxLeft){
            speedX*=-1;//碰到右边框换X的方向
            obj.style.backgroundColor=randomColor();
        }
        let maxBottom=window.innerHeight-obj.offsetHeight-15;
        if(obj.offsetTop>maxBottom){
            speedY*=-1;//碰到下边框换Y的方向
            obj.style.backgroundColor=randomColor();
        }
    },30)
}
效果截图

![ZXWIU~]E)IS@J`FNN{CK_SU.png](https://img.haomeiwen.com/i3666741/19e1a3a5f95c7443.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

谢谢大家!!!

相关文章

  • 眼睛

    望着你的眼睛,寂静着关掉的灯,空洞里生不出烟火。 记于2020-08-03 午

  • 【D219】构建人生的基础:接纳+感恩——写作营共读打卡第186

    2020-08-03,周二,晴 今天阅读《心》第一章。 Day186《构建人生的基础:接纳+感恩》 ——写作营第1...

  • 独立开发者记录的第五周

    记录日期(2020-08-03) 上周太忙了,没有更新,这周继续记录! 不多介绍了,像了解我具体是干什么的,可以看...

  • 世界麻烦不断,我们怎么保持好自己的生产力?

    2020-08-03 已关注 如果你感觉最近什么都做不了。本文是如何让你保持清醒的头脑的建议,不要落入忙碌的陷阱。...

  • 不同格式U盘 复制 含特殊字符名称的文件结果不一样

    2020-08-03 遇到的一点小问题 今天做特殊字符的测试用例,才发现将包含特殊字符文件名的文件拷贝进不同分区格...

  • 阅读打卡之《我们仨》04-06

    04(2020-08-03) 打卡《我们仨》第二部第三章《古驿道上相失》。 这一章读完最是令人感伤。就在这样日复一...

  • 2020-08-03

    【六项精进打卡】 2020.8.3日 姓名:陈岗 企业名称:上海孚因流体动力设备股份有限公司 打卡第831天 【知...

  • 2020-08-03

    【551号】入驻者:尚赜 【551号】04410-黄花 【551号】04412-故事 【551号】04563-中年...

  • 2020-08-03

    永远不要原谅故意而为之的伤害,你的善解人意往往换来的都是得寸进尺,他会用行动和演技告诉你,良心是最不值钱的

  • 2020-08-03

    2020.8.2 六点多就睁眼了,做的梦很累。 今天上午很忙,又要直播又要学习。国学费还没交齐。总之别让自己不爽。...

网友评论

      本文标题:2020-08-03

      本文链接:https://www.haomeiwen.com/subject/globrktx.html