一、js的运动

  匀速运动

  1. 清除定时器
  2. 开启定时器
  3. 运动是否完成:a、运动完成,清除定时器;b、运动未完成继续

  匀速运动停止条件:距离足够近  Math.abs(当然距离-目标距离) < 最小运动距离

div的匀速运动(简单运动)

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<style>
#div1{ width:100px; height:100px; background:red; position:absolute; top:50px; left:500px;}
span{ width:1px; height:300px; background:black; position:absolute; left:300px; top:0; display:block;}

</style>
<script>
window.onload = function()
{
    var oBtn = document.getElementById('btn1');
    var oDiv = document.getElementById('div1');

    oBtn.onclick = function()
    {
        startMove(oDiv, 300);
    };
};
var timer = null;
function startMove(obj, iTarget)
{
    clearInterval(timer);
    timer = setInterval(function(){
        var iSpeed = 0;

        if(obj.offsetLeft < iTarget)
        {
            iSpeed = 7;
        }
        else
        {
            iSpeed = -7;
        }

        if( Math.abs( obj.offsetLeft - iTarget ) < 7 )
        {
            clearInterval(timer);
            obj.style.left = iTarget + 'px';
        }
        else
        {
            obj.style.left = obj.offsetLeft + iSpeed + 'px';
        }
    }, 30);
}
</script>
</head>

<body>
<button id="btn1">移动</button>
<div id="div1"></div>
<span></span>
</body>
</html>

javascript每日一练(九)——运动一:匀速运动的更多相关文章

  1. javascript每日一练—运动

    1.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...

  2. javascript每日一练(十四)——弹性运动

    一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...

  3. javascript每日一练(十二)——运动框架

    运动框架 可以实现多物体任意值运动 例子: <!doctype html> <html> <head> <meta charset="utf-8&q ...

  4. javascript每日一练(十一)——多物体运动

    一.多物体运动 需要注意:每个运动物体的定时器作为物体的属性独立出来互不影响,属性与运动对象绑定,不能公用: 例子1: <!doctype html> <html> <h ...

  5. javascript每日一练(十)——运动二:缓冲运动

    一.缓冲运动 实现原理:(目标距离-当前距离) / 基数 = 速度(运动距离越大速度越小,运动距离和速度成反比) (500 - oDiv.offsetLeft) / 7 = iSpeed; 需要注意: ...

  6. javascript每日一练(十三)——运动实例

    一.图片放大缩小 <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  7. javascript每日一练(一)——javascript基础

    一.javascript的组成 ECMAScript DOM BOM 二.变量类型 常见类型有:number, string, boolean, undefined, object, function ...

  8. javascript每日一练(八)——事件三:默认行为

    一.阻止默认行为 return false; 自定义右键菜单 <!doctype html> <html> <head> <meta charset=&quo ...

  9. javascript每日一练(七)——事件二:键盘事件

    一.键盘事件 onkeydown触发, keyCode键盘编码 ctrlKey altKey shiftKey 键盘控制div移动 <!doctype html> <html> ...

随机推荐

  1. string.Format()字符串格式化

    Format()基本语法:     {索引[,对齐][:格式字符串]}     ·索引:表示引用的对象列表中的第n个对象参数.     ·对齐(可选):设置宽度与对齐方式,该参数为带符号的整数.正数为 ...

  2. BZOJ 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛( floyd )

    对于第 i 头牛 , 假如排名比它高和低的数位 n - 1 , 那么他的 rank 便可以确定 . floyd -------------------------------------------- ...

  3. Objective-C 程序设计第四版

    1,@class  XYPoint; 写在.h文件里,告诉编译器有这类,然后用的时候,其实是在.m文件引入的. 例如.#import “XYPoint.h”  然后在.m文件里就能用XYPoint.h ...

  4. PROPAGATION_REQUIRED

    PROPAGATION_REQUIRED (2009-05-13 13:26:52) 转载▼   事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传 ...

  5. 「操作系统」: Conditional Move Instructions(trap)

    Not all conditional expressions can be compiled using conditional moves. Most significantly, the abs ...

  6. notify()、notifyAll()和wait()

    看到一道面试题,写一个多线程程序,交替输出1.2.1.2…… 先写下程序: /** * Created by Andrew on 2015/10/28. */ public class OutputT ...

  7. poj 1664 put apples(dfs)

    题目链接:http://poj.org/problem?id=1664 思路分析:数据较小,考虑深度优先搜索搜索解空间. 代码如下: #include <iostream> using n ...

  8. 用 PS 调整服务器时间

    用 PS 调整服务器时间 Powershell 有一个命令用来调整计算机的时间, Set-Date -Adjust,但是,只能调整本地时间,不能调整远程的计算机时间. function AdjustD ...

  9. dhtmlx之dhtmlXGrid显示数据

    引用 <link href="../../dhtmlXGridScripts/dhtmlxgrid.css" rel="stylesheet" type= ...

  10. MYSQL - php 使用 localhost 无法连接数据库

    php 使用 localhost 无法连接数据库,而使用127.0.0.1却能连接成功. 可能原因: 系统hosts文件未提供127.0.0.1到localhost的解析.解决方法(以win7系统为例 ...