博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
按轨迹周期运动
阅读量:7172 次
发布时间:2019-06-29

本文共 3958 字,大约阅读时间需要 13 分钟。

一、水平方向上的反复运动

例如:
水平宽度:50px
小球直径:6px

html结构:
    <div id="line" class="line">
        <div id="point" class="point" >
        </div>
    </div>
css样式:
.line{position: relative;width: 100px;height: 100px;border-bottom: 1px solid red;left: 100px;top: 100px;}
.point{width: 6px;height: 6px;background-color: Red;position: absolute;left: 47px;top: 47px;border-radius: 3px;}
js代码:
            var point = document.getElementById("point");
            var line = document.getElementById("line");
            var i = 0, j = 0;
            var b = false;
            setInterval(function () {
                if (i < line.offsetWidth && false == b) {
                    i++;
                    point.style.left = 47 - 50 + i + "px";
                    point.style.top = 47 + 50 + "px";
                }
                if (contline.offsetWidth == i) { b = true; }
                if (i > 0 && true == b) {
                    i--;
                    point.style.left = 47 - 50 + i + "px";
                    point.style.top = 47 + 50 + "px";
                }
                if (0 == i) {
                    b = false;
                }
            }, 60);

View Code
1   2   3   4     水平周期运动  5       9      35  36  37     
38
39
40
41 42

二、按长方形(正方形)周长运动

长方形的长和宽分别是150px和100px
小球直径:6px

html结构:
    <div id="rectangle" class="rectangle">
        <div id="point" class="point">
        </div>
    </div>
css样式:
.rectangle{position: relative;width: 150px;height: 100px;border: 1px solid red;left: 100px;top: 100px;}
.point{width: 6px;height: 6px;background-color: Red;position: absolute;left: 47px;top: 47px;border-radius: 3px;}
js代码:

var point = document.getElementById("point");

            var rectangle = document.getElementById("rectangle");
            var i = 0, j = 0;
            setInterval(function () {
                if (i < rectangle.offsetWidth && 0 == j) {
                    i++;
                    point.style.left = 47 - 50 + i + "px";
                    point.style.top = 47 - 50 + j + "px";
                }

                else if (rectangle.offsetWidth == i && j < rectangle.offsetHeight) {

                    j++;
                    point.style.left = 47 - 50 + i + "px";
                    point.style.top = 47 - 50 + j + "px";
                }
                else if (i > 0 && rectangle.offsetHeight == j) {
                    i--;
                    point.style.left = 47 - 50 + i + "px";
                    point.style.top = 47 - 50 + j + "px";
                }
                else {
                    j--;
                    point.style.left = 47 - 50 + i + "px";
                    point.style.top = 47 - 50 + j + "px";
                }
            }, 60);

View Code
1   2   3   4     水平周期运动  5       9      39  40  41     
42
43
44
45 46

三、按圆形周长运动 2∏R

圆形直径:100px
小球直径:6px
html结构:
    <div class="circle">
        <div id="point" class="point">
        </div>
    </div>
css样式:
.circle{position: relative;width: 100px;height: 100px;border: 1px solid red;left: 100px;top: 100px;border-radius: 50px;}
.point{width: 6px;height: 6px;background-color: Red;position: absolute;left: 47px;top: 47px;border-radius: 3px;}
js代码:
var point = document.getElementById("point");
            var i = 0;
            setInterval(function () {
                if (360 == i) {
                    i = 0;
                    point.style.left = 47 + 50 * Math.cos(0 * 2 * Math.PI / 360) + "px";
                    point.style.top = 47 + 50 * Math.sin(0 * 2 * Math.PI / 360) + "px";
                }
                else {
                    i++;
                    point.style.left = 47 + 50 * Math.cos(i * 2 * Math.PI / 360) + "px";
                    point.style.top = 47 + 50 * Math.sin(i * 2 * Math.PI / 360) + "px";
                }
            }, 60);

View Code
1   2   3   4     水平周期运动  5       9      27  28  29     
30
31
32
33 34

四、椭圆周长运动 ∏(a+b)

椭圆长半径:100px、短半径:50px
小球直径:6px
html结构:
    <div class="oval">
        <div id="point" class="point">
        </div>
    </div>
css样式:
.oval{position: relative;width: 200px;height: 100px;border: 1px solid red;left: 100px;top: 100px;border-radius: 100px/50px;}
.point{width: 6px;height: 6px;background-color: Red;position: absolute;left: 47px;top: 47px;border-radius: 3px;}
js代码:
            var point = document.getElementById("point");
            var i = 0;
            setInterval(function () {
                if (360 == i) {
                    i = 0;
                    point.style.left = 97 + 100 * Math.cos(0 * 2 * Math.PI / 360) + "px";
                    point.style.top = 47 + 50 * Math.sin(0 * 2 * Math.PI / 360) + "px";
                }
                else {
                    i++;
                    point.style.left = 97 + 100 * Math.cos(i * 2 * Math.PI / 360) + "px";
                    point.style.top = 47 + 50 * Math.sin(i * 2 * Math.PI / 360) + "px";
                }
            }, 60);

View Code
1   2   3   4     水平周期运动  5       9      27  28  29     
30
31
32
33 34

转载地址:http://vmbzm.baihongyu.com/

你可能感兴趣的文章
Linux文件系统上的特殊权限
查看>>
IBM携手红帽将助力企业加快虚拟化步伐
查看>>
8.C++引用
查看>>
利用imgateaselect插件实现前端页面图片截取功能
查看>>
Java super()
查看>>
xinetd服务介绍及配置
查看>>
在Redis-Sentinel的client-reconfig-script脚本中设置VIP
查看>>
服务器资源使用情况统计--脚本
查看>>
Oracle查询数据库的索引字段以及查询用索引
查看>>
第二讲、实例变量可见度、方法
查看>>
zabbix监控基础知识
查看>>
mysql四:数据操作
查看>>
Div的定位
查看>>
Activity ca.ct.activity.OBaccaratActivity has leak
查看>>
nginx+tomcat+resin+jdk一键自动化安装脚本(1--父shell安装脚本)
查看>>
strspn
查看>>
Rancher如何对接Ceph-RBD块存储
查看>>
3DTouch学习笔记
查看>>
Linux下 vi 操作Found a swap file by the name
查看>>
filebeat 插件开发
查看>>