• 【JavaScript】撞墙的小球


    参考:

    1、JS 元素位置 设置元素位置:http://blog.sina.com.cn/s/blog_a2ec891e01011v9f.html

    2、用JavaScript修改CSS属性

    3、使用JavaScript动态更改CSS样式:http://www.jianshu.com/p/0260cddff86a、http://kimi.it/289.html、http://www.jb51.net/article/65625.htm

    效果图:

    思路:

    1、绘制的静态小球。

    2、定义小球的四种飞行状态。

    3、初始化小球状态,定义切换状态的条件。

    代码:

    1、初步实现。。。缺点是无法根据屏幕大小调整“墙”的位置。。还需要小改一下。

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <title>Dynamic Ball</title>
            <style>
                html {
                    background: #F2F2F2;
                }
                
                p {
                    text-align: center;
                }
                
                .ball {
                     100px;
                    height: 100px;
                    
                    background: black;
                    border-radius: 50%;
                    
                    position: absolute;
                    left: 100px;
                    top: 300px;
                }
            </style>
        </head>
        <body>
            <p>Dynamic Ball</p>
            <div class="ball"></div>
            <script>
                function setPosition(x , y) {
                    target.style.left = x + "px";
                    target.style.top = y + "px";
                }
                var target = document.querySelector('.ball');
                // 定义一对变量来管理target的位置        
                var X = Math.random()*1024;
                var Y = Math.random()*768;
                // 定义一个变量管理target的飞行状态
                var STATE = 1;
                function f1() {
                    X++;
                    Y++;
                    STATE = 1;
                    setPosition(X, Y);
                }
                function f2() {
                    X--;
                    Y++;
                    STATE = 2;
                    setPosition(X, Y);
                }
                function f3() {
                    X++;
                    Y--;
                    STATE = 3;
                    setPosition(X, Y);
                }
                function f4() {
                    X--;
                    Y--;
                    STATE = 4;
                    setPosition(X, Y);
                }
                function checkDirection() {
                    if (X > 1024 && STATE == 1) STATE = 2;
                    if (X > 1024 && STATE == 3) STATE = 4;
                    if (X < 0 && STATE == 4) STATE = 3;
                    if (X < 0 && STATE == 2) STATE = 1;
                    if (Y > 768 && STATE == 1) STATE = 3;
                    if (Y > 768 && STATE == 2) STATE = 4;
                    if (Y < 0 && STATE == 4) STATE = 2;
                    if (Y < 0 && STATE == 3) STATE = 1;
                    if (STATE == 1) f1();
                    if (STATE == 2) f2();
                    if (STATE == 3) f3();
                    if (STATE == 4) f4();
                }
                function start() {
                    setInterval("checkDirection()", 10);
                }
                setPosition(X, Y);
                start();
                // target.f1 target.f2 target.f3 target.f4
                // target.setPosition(randomX, randomY);
                // target.start();
            </script>
        </body>
    </html>

     2、改。参考:http://www.cnblogs.com/Henllyee/archive/2008/04/20/1162517.html

    <script>
                var screenWidth = document.documentElement.clientWidth;
                var screenHeight = document.documentElement.clientHeight;
                function setPosition(x , y) {
                    target.style.left = x + "px";
                    target.style.top = y + "px";
                }
                var target = document.querySelector('.ball');
                // 定义一对变量来管理target的位置        
                var X = Math.random()*screenWidth;
                var Y = Math.random()*screenHeight;
                // 定义一个变量管理target的飞行状态
                var STATE = 1;
                function f1() {
                    X++;
                    Y++;
                    STATE = 1;
                    setPosition(X, Y);
                }
                function f2() {
                    X--;
                    Y++;
                    STATE = 2;
                    setPosition(X, Y);
                }
                function f3() {
                    X++;
                    Y--;
                    STATE = 3;
                    setPosition(X, Y);
                }
                function f4() {
                    X--;
                    Y--;
                    STATE = 4;
                    setPosition(X, Y);
                }
                function checkDirection() {
                    screenWidth = document.documentElement.clientWidth;
                    screenHeight = document.documentElement.clientHeight;
                    if (X > screenWidth && STATE == 1) STATE = 2;
                    if (X > screenWidth && STATE == 3) STATE = 4;
                    if (X < 0 && STATE == 4) STATE = 3;
                    if (X < 0 && STATE == 2) STATE = 1;
                    if (Y > screenHeight && STATE == 1) STATE = 3;
                    if (Y > screenHeight && STATE == 2) STATE = 4;
                    if (Y < 0 && STATE == 4) STATE = 2;
                    if (Y < 0 && STATE == 3) STATE = 1;
                    if (STATE == 1) f1();
                    if (STATE == 2) f2();
                    if (STATE == 3) f3();
                    if (STATE == 4) f4();
                }
                function start() {
                    setInterval("checkDirection()", 10);
                }
                setPosition(X, Y);
                start();
                // target.f1 target.f2 target.f3 target.f4
                // target.setPosition(randomX, randomY);
                // target.start();
            </script>
  • 相关阅读:
    好用的抓取dump的工具-ProcDump
    好用的抓取dump的工具-ProcDump
    使用Windbg调试系统弹出的内存不可读错误
    使用Windbg调试系统弹出的内存不可读错误
    驱动层hook系统函数的时,如何屏蔽掉只读属性?
    Rootkit之SSDT hook(通过CR0)
    Rootkit之SSDT hook(通过CR0)
    #define SYSTEMSERVICE(_func) KeServiceDescriptorTable.ServiceTableBase[ *(PULONG)((PUCHAR)_func+1) 这
    华南理工大学“三七互娱杯”程序设计竞赛(重现赛)B HRY and fibonacci
    华南理工大学“三七互娱杯”程序设计竞赛(重现赛)A HRY and codefire
  • 原文地址:https://www.cnblogs.com/xkxf/p/6747502.html
Copyright © 2020-2023  润新知