js-小方块运动(点击按钮加速)

    <!-- 点加速按钮以后,移动速度变快 -->
    <button style="width: 100px; height: 50px; background: linear-gradient(to left,#999, #000, #432, #fcc); position: fixed; right:0; top:50%;
    text-align: center; line-height: 50px; color: #fff; font-size: 25px; font-family: arial;">加速</button>

    <script>
        var btn = document.getElementsByTagName('button')[0];
        var div = document.createElement('div');

        document.body.appendChild(div);
        div.style.width = "100px";
        div.style.height = "100px";
        div.style.backgroundColor = "red";
        div.style.position = "absolute";
        div.style.left = "0";
        div.style.top = "0";
        var speed = 5;
        btn.onclick = function(e){
            speed ++;
        }
        document.onkeydown = function(e){
            switch(e.which) {
                case 38:
                    div.style.top = parseInt(div.style.top) - speed + "px";
                    break;
                case 40:
                    div.style.top = parseInt(div.style.top) + speed + "px";
                    break;
                case 37:
                    div.style.left = parseInt(div.style.left) - speed + "px";
                    break;
                case 39:
                    div.style.left = parseInt(div.style.left) + speed + "px";
                    break;
            }
        }
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读