小球触壁反弹变色

目录

涉及知识点

成品展示

一、效果展示

二、代码展示

1、基础框架

2、CSS样式

3、JS代码


涉及知识点

任何事件都有内置对象event,event代表时间状态。

兼容性: var event=event||window.event;

clientWidth:元素的宽度(不算border)clientHeight:元素的高度(不算border)

offsetWidth:元素的宽度(算border)offsetHeight:元素的高度(算border)


成品展示

一、效果展示

二、代码展示

1、基础框架

<div id="box">
        <div id="inner"></div>
    </div>
    <button id="start">开始</button>
    <button id="stop">暂停</button>
    <button id="over">结束</button>

2、CSS样式

#box {
        width: 800px;
        height: 500px;
        position: relative;
        border: 5px solid red;
        margin: 20px auto;
    }

    #inner {
        width: 50px;
        height: 50px;
        position: absolute;
        background-color: #00f;
        border-radius: 50%;
    }

3、JS代码

var box = document.querySelector("#box");
    var inner = document.querySelector("#inner");
    var start = document.querySelector("#start");
    var end = document.querySelector("#stop");
    var over = document.querySelector("#over");
    var innerLeft = 0;
    var innerTop = 0;
    end.disabled = true;
    over.disabled = true;
    var moveX = true;
    var moveY = true;

    function random(m, n) {
        return Math.floor(Math.random() * (n - m + 1) + m);
    }

    function color() {
        return "rgb(" + random(0, 255) + "," + random(0, 255) + "," + random(0, 255) + ")";
    }
    start.onclick = function () {
        start.disabled = true;
        end.disabled = false;
        over.disabled = false;
        Xmax = box.clientWidth - inner.clientWidth;
        Ymax = box.clientHeight - inner.clientHeight;
        timer = setInterval(function () {
            if (moveX == true) {
                if (innerLeft <= Xmax) {
                    innerLeft++;
                } else {
                    moveX = false;
                    inner.style.backgroundColor=color();
                }

            } else {

                if (innerLeft >= 0) {
                    innerLeft--;
                } else {
                    moveX = true;
                    inner.style.backgroundColor=color();
                }
            }
            if (moveY == true) {
                if (innerTop <= Ymax) {
                    innerTop++;
                } else {
                    moveY = false;
                    inner.style.backgroundColor=color();
                }

            } else {
                if (innerTop >= 0) {
                    innerTop--;
                } else {
                    moveY = true;
                    inner.style.backgroundColor=color();
                }
            }
            inner.style.left = innerLeft + "px";
            inner.style.top = innerTop + "px";
        }, 5);
    }
    end.onclick = function () {
        clearInterval(timer);
        start.disabled = false;
        this.disabled = true;
    }
    over.onclick = function () {
        clearInterval(timer);
        innerLeft = 0;
        innerTop = 0;
        inner.style.left = innerLeft + "px";
        inner.style.top = innerTop + "px";
        start.disabled = false;
        inner.style.backgroundColor= "#00f";
    }

 

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值