bom案例5-简单动画
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
margin: 0;
}
#box {
position: relative;
width: 100px;
height: 100px;
background-color: red;
} #box1 {
margin-top: 10px;
position: relative;
width: 100px;
height: 100px;
background-color: blue;
}
</style>
</head>
<body>
<input type="button" id="btn" value="动画 800">
<input type="button" id="btn1" value="动画 400"> <div id="box">
</div>
<div id="box1">
</div>
<script>
var btn = document.getElementById('btn');
var btn1 = document.getElementById('btn1'); var box = document.getElementById('box');
var box1 = document.getElementById('box1'); btn.onclick = function () {
animate(box, 800);
animate(box1, 800);
// console.log(box.style.left);
// console.log(box.offsetLeft); // box.style.left = box.offsetLeft + 10 + 'px'; // console.log(box.style.left);
// console.log(box.offsetLeft);
} btn1.onclick = function () {
animate(box, 400);
animate(box1, 400);
// console.log(box.style.left);
// console.log(box.offsetLeft); // box.style.left = box.offsetLeft - 10 + 'px'; // console.log(box.style.left);
// console.log(box.offsetLeft);
}
// var timerId = null;
// 封装动画的函数
function animate(element, target) {
// 通过判断,保证页面上只有一个定时器在执行动画
if (element.timerId) {
clearInterval(element.timerId);
timerId = null;
} element.timerId = setInterval(function () {
// 步进 每次移动的距离
var step = 10;
// 盒子当前的位置
var current = element.offsetLeft;
// 当从400 到 800 执行动画
// 当从800 到 400 不执行动画 // 判断如果当前位置 > 目标位置 此时的step 要小于0
if (current > target) {
step = - Math.abs(step);
} // Math.abs(current - target) < Math.abs(step)
if (Math.abs(current - target) < Math.abs(step)) {
// 让定时器停止
clearInterval(element.timerId);
// 让盒子到target的位置
element.style.left = target + 'px';
return;
}
// 移动盒子
current += step;
element.style.left = current + 'px';
}, 30);
}
</script>
</body>
</html>
bom案例5-简单动画的更多相关文章
- 原生JS封装简单动画效果
原生JS封装简单动画效果 一致使用各种插件,有时候对原生JS陌生了起来,所以决定封装一个简单动画效果,熟悉JS原生代码 function animate(obj, target,num){ if(ob ...
- jquery添加光棒效果的各种方式以及简单动画复杂动画
过滤器.绑定事件.动画 一.基本过滤器 语法 描述 返回值 :first 选取第一个元素 单个元素 :last 选取最后一个元素 单个元素 :not(selector) 选取去除所有与给定选择器匹 ...
- UIView简单动画
UIView动态实现的效果有以下几种: 1.动态改变frame 2.动态改变color 3.动态改变alpha 4.动态改变bounds 首先,我们先看几种BasicView动画 #pragma ma ...
- IOS 简单动画 首尾式动画
首尾式动画 首尾式动画即通过实现控件由初始状态到结束状态的过程.(主要表现在控件的Frame 透明度 ) // // ViewController.m // CX 简单动画 // // Created ...
- jQuery中自定义简单动画的实现
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- canvas制作简单动画
在画布元素<canvas>中,除了绘制图形.图像.文字外,还可以制作一些简单的动画,制作过程十分简单,主要分为两步操作: 1.自定义一个函数,用于图形的移动或其他动作. 2.使用setIn ...
- Cocos2d-x 2.1.5 简单动画
Cocos2d新版本函数更改了一些. 下面的代码可以产生一个简单动画. //第一步:生成动画需要的数据 CCTexture2D *texture=CCTextureCache::sharedTextu ...
- UI设计篇·入门篇·简单动画的实现,透明动画/旋转动画/移动动画/缩放动画,混合动画效果的实现,为动画设置监听事件,自定义动画的方法
基本的动画构成共有四种:透明动画/旋转动画/移动动画/缩放动画. 配置动画的方式有两种,一种是直接使用代码来配置动画效果,另一种是使用xml文档配置动画效果 相比而言,用xml文档写出来的动画效果,写 ...
- python实现简单动画——生命游戏
生命游戏 生命游戏的宇宙是一个无限的,其中细胞的二维正交网格,每个细胞处于两种可能的状态之一,即*活着*或*死亡*(分别是*人口稠密*和*无人居住*).每个细胞与它的八个邻居相互作用,这八个邻居是水平 ...
随机推荐
- Chapter 7 Confounding
目录 7.1 The structure of confounding Confounding and exchangeability Confounding and the backdoor cri ...
- 编写Java程序,使用菜单组件制作一个记事本编辑器
返回本章节 返回作业目录 需求说明: 使用菜单组件制作一个记事本编辑器 实现思路: 创建记事本菜单工具栏JMenuBar. 创建多个菜单条JMenu. 创建多个菜单项JMenuItem. 将菜单添加至 ...
- Unity——火烧+水波纹效果(噪音图)
使用噪声图实现火烧和水波纹效果: 1.溶解 关闭裁剪,根据noise纹理取样,r通道和_BurnAmount比较,裁剪掉小于_BurnAmount的片元: 通过菲尼尔得到裁剪边缘,添加火焰燃烧的颜色进 ...
- golang 算法题 : 两数相加
package mainimport "fmt"type ListNode struct { Val int Next *ListNode}func main() { l1 := ...
- Linux 中安装、升级、配置 Swoole 扩展
从源码编译安装 # 下载Swoole wget http://pecl.php.net/get/swoole-4.5.2.tgz tar -zxvf swoole-4.5.2.tgz cd swool ...
- python 使用@property 操作属性时,报“RecursionError:maximun recursion depth exceeded”
使用@property获取和修改属性,出现报错"RecursionError:maximun recursion depth exceeded",超过了最大的递归深度 原因: 方法 ...
- hive 之 常用基本操作
show databases; -- 查看所有数据库 use 数据库; -- 进入某个数据库 select current_database(); -- 查看当前使用的数据库 show tables; ...
- 初识python: 类练习 - 随机数生成
1.提取指定值之间的指定个数的随机整数. 2.继承生成随机数的类,打印"坐标". 生成随机数类: import random class GetRadndom(object): ' ...
- Python面向对象时最常见的3类方法
为了节省读友的时间,先上结论(对于过程和细节感兴趣的读友可以继续往下阅读,一探究竟): [结论] 类中定义的方法类型 关键词 本质含义 如何定义 如何调用 使用场景举例 实例方法 一般无任何修饰时,默 ...
- SpringMVC拦截器的应用
一.作用 好文章参考:https://www.cnblogs.com/panxuejun/p/7715917.html 对请求进行预处理和后处理: 使用场景: 登录验证,判断用户是否登录 权限验证,判 ...