老师检查下代码,我自己怎么写不出来啊

老师检查下代码,我自己怎么写不出来啊

<style>

        body {

            background-color: black;

        }


        .ball {

            position: absolute;

            border-radius: 50%;

        }

    </style>

</head>


<body>

    <script>

        function ball(x, y) {

            this.x = x;

            this.y = y;

            this.r = '20'

            this.opacity=1;

            this.color = colorarr[parseInt(Math.random()*colorarr.length)];

            do{

                this.dx=parseInt(Math.random()*20)-10;

            this.dy=parseInt(Math.random()*20)-10;

            }while(this.dx==0&&this.dy==0)

           

            this.init();

            ballarr.push(this);

        }

        ball.prototype.init = function () {

            this.dom = document.createElement('div')

            this.dom.className = 'ball'

            this.dom.style.width = this.r * 2 + 'px'

            this.dom.style.height = this.r * 2 + 'px'

            this.dom.style.left = this.x - this.r + 'px';

            this.dom.style.top = this.y - this.r + 'px'

            this.dom.style.backgroundColor = this.color;

            document.body.appendChild(this.dom);

        }

        ball.prototype.update = function () {

            this.x+=this.dx;

            this.y-=this.dy

            this.r+=0.2;

            this.opacity-=0.01;

            this.dom.style.width = this.r * 2 + 'px'

            this.dom.style.height = this.r * 2 + 'px'

            this.dom.style.left = this.x - this.r + 'px';

            this.dom.style.top = this.y - this.r + 'px'

           

            this.dom.style.opacity=this.opacity;

            if(this.opacity<0){

                for(var i=0;i<ballarr.length;i++){

                    if(ballarr[i]==this){

                        ballarr.splice(i,1);

                    }

                }

                document.body.removeChild(this.dom);

            }

        }


        var ballarr = [];

        var colorarr=['#66cccc','#ccff66','#ff99cc','#ff6666','#cc3399','#ff6600']

        new ball(200,600);

        setInterval(function () {

            for (var i = 0; i < ballarr.length; i++) {

                ballarr[i].update()

            }

        }, 20);  

        document.onmousemove=function(e){

            var x=e.clientX;

            var y=e.clientY;

            new ball(x  ,y);

        }

搜索

复制

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕然然 2021-12-05 14:09:50

同学你好,代码中给小球设置半径属性时,把属性值设为了字符串类型,应该要设置成数字类型才对,如图

https://img1.sycdn.imooc.com//climg/61ac579409a6ff1506910299.jpg

祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师