使用原生的javascript封装动画函数(有callback功能)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
#box {
width: 100px;
height: 100px;
background-color: greenyellow;
position: absolute;
}
</style>
</head>
<body>
<input type="button" value="按钮" id="btn"/> <div id="box"></div>
<script>
var btn = document.getElementById("btn");
btn.onclick = function () {
animate(box, {"height": 400, "width": 400, "borderRadius": 150, "left": 100, "top": 100}, function () {
animate(box, {"height": 200, "width": 100, "borderRadius": 20, "left": 200, "top": 50}, function () {
animate(box, {"height": 100, "width": 200, "borderRadius": 100, "left": 400, "top": 400})
})
})
}
function animate(obj, json, fn) {
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var isTrue = true;
for (var k in json) {//{k:json[k]}
var leader = parseInt(getAttr(obj, k)) || 0;//如果是nan的话,给一个默认值
var step = (json[k] - leader) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);//做一个判断,当step>0的时候,向上取整,保证至少为一;<0的时候,向下取整,保证至少为-1
leader = leader + step;
obj.style[k] = leader + "px";
console.log("代码还在执行");
if (leader != json[k]) {
isTrue = false;
}
}
if (isTrue) {
clearInterval(obj.timer);
if (fn) {
fn();
}
}
}, 15);
}
function getAttr(demo, attr) {
if (window.getComputedStyle) {
return window.getComputedStyle(demo, null)[attr];
} else {
return demo.currentStyle[attr];
}
}
</script>
</body>
</html>
使用原生的javascript封装动画函数(有callback功能)的更多相关文章
- JS---案例:移动元素,封装动画函数
案例:移动元素,封装动画函数 1. div要移动,要脱离文档流---position:absolute 2. 如果样式的代码是在style的标签中设置,外面是获取不到 3. 如果样式的代码是在styl ...
- JavaScript封装一个函数效果类似内置方法concat()
JavaScript封装一个函数效果类似内置方法concat() 首先回忆concat()的作用: concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,而仅仅会返回被连接数组的一个 ...
- 原生javascript封装的函数
1.javascript 加载的函数 window.onload = function(){} 2.封装的id函数 function $(id) { return document.getElemen ...
- javascript 自定义动画函数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- Javascript之回调函数(callback)
1.回调函数定义: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用为调用它所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方 ...
- 原生javascript 基础动画函数封装(二)
<!DOCTYPE html> <html> <head> <title></title> <style type="tex ...
- 原生javascript 基础动画函数封装(一)
<!DOCTYPE html> <html> <head> <title></title> <style type="tex ...
- javascript封装的函数
/*获取一个指定长度随机数*/ csdn.random = function (len) { if (!len) len = 5; var r = Math.random().toString(); ...
- js原生实现链式动画效果
// 1. css样式 div { width: 100px; height: 100px; background: olivedrab; position: absolute; left: 0px; ...
随机推荐
- Bitbucket - 用git 用法
核心流程: 从远端中心repo那里Git clone 到本地,再在本地开发(add, commit), 通常会利用branch管理,如果觉得code 没问题了,就push到远端的中心repo上.这里中 ...
- Mysql带返回值与不带返回值的2种存储过程
过程1:带返回值: 1 drop procedure if exists proc_addNum; 2 create procedure proc_addNum (in x int,in y int, ...
- python中的range与xrange
range 也是一种类型(type),它是一个数字的序列(s sequence of numbers),而且是不可变的,通常用在for循环中. class range(stop) class rang ...
- hdu4998 Rotate【计算几何】
Noting is more interesting than rotation! Your little sister likes to rotate things. To put it easi ...
- Postman 工具模拟Ajax请求
1.请求方式 post 2.headers设置:X-Requested-With:XMLHttpRequest 代码判断是以此为依据的 (Content-Type:application/x-w ...
- wxWidgets与其他工具库的比较(上)
本文是在wxWidgets Wiki上面找到的一篇,对比了wxWidgets和其他一些界面工具的特点.看到很多朋友在网上询问这些库各自的特点,我想先把这篇文章翻译出来——毕竟这也算是一篇官方的文章,应 ...
- ICMP报文
类型:表示ICMP消息类型 代码:表示同一消息的不同信息 其他是时间戳或者标识符及序列号 类型 编码 描述 0 0 Echo Reply 3 0 网络不可达 3 1 主机不可达 3 2 协议不可达 ...
- 聊一聊Linux中的工作队列2
上一篇文章对工作队列原理以及核心数据结构做了简单介绍,本文重点介绍下workqueue的创建以及worker的管理. 一.工作队列的创建(__alloc_workqueue_key) struct w ...
- python 面向对象 issubclass
判断是否 他的父类 class Foo(object): pass obj = Foo() class Boo(Foo): pass class Coo(Boo): pass obj = Boo() ...
- phpsocketclient以及server样例
一个菜鸟朋友,突然问了我这个问题...如今稍稍有点时间,就写了一个简单的样例给他,顺便贴上来 server端: <? php /** * @author 邹颢 zouhao619@gmail.c ...