zoukankan      html  css  js  c++  java
  • 20.JavaScript实现简单的圆球反弹运动

    JavaScript实现简单的圆球反弹运动

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .circle {
                 100px;
                height: 100px;
                border: 1px solid #ccc;
                border-radius: 50%;
                background: #008c8c;
                position: fixed;
                left: 100px;
                top: 100px;
            }
        </style>
    </head>
    
    <body>
        <div class="circle"></div>
    
        <script>
            var circle = document.querySelector(".circle");
            var style = window.getComputedStyle(circle);
            var disX = 10,
                disY = 10,
                time = 30;
    
    
            circle.onclick = function () {
                setInterval(function () {
                    var left = parseFloat(style.left);
                    var top = parseFloat(style.top);
                    var newLeft = left + disX;
                    var newTop = top + disY;
    
                    if (left < 0) {
                        newLeft = 0;
                        disX = -disX;
    
                    }
                    if (left > document.documentElement.clientWidth - parseInt(style.width)) {
                        newLeft = document.documentElement.clientWidth - parseInt(style.width);
                        disX = -disX;
                    }
                    if (top < 0) {
                        newTop = 0;
                        disY = -disY;
    
                    }
                    if (top > document.documentElement.clientHeight - parseInt(style.height)) {
                        newTop = document.documentElement.clientHeight - parseInt(style.height);
                        disY = -disY;
                    }
    
                    circle.style.left = newLeft + "px";
                    circle.style.top = newTop + "px";
    
                }, time);
            }
        </script>
    </body>
    
    </html>
    index.html
  • 相关阅读:
    js 面试的坑:变量提升
    meta 标签大全
    一个极为简单的requirejs实现
    AMD 的 CommonJS wrapping
    浅解析js中的对象
    javascript运动系列第二篇——变速运动
    开发汉澳即时通信网,2006年上线,QQ死期到了
    SpringMVC中的异步提交表单
    HDU 3698 DP+线段树
    黑马程序猿_反射、内省、泛型
  • 原文地址:https://www.cnblogs.com/lanshanxiao/p/12801494.html
Copyright © 2011-2022 走看看