【WorkTile赞助】jQuery编程挑战#009:生成两个div元素互相追逐的动画
HTML页面:
- <!-- HTML代码片段中请勿添加<body>标签 //-->
- <div id="container">
- <div id="first"></div>
- <div id="second"></div>
- </div>
- <!-- 推荐开源CDN来选取需引用的外部JS //-->
- <script type="text/javascript" src="http://cdn.gbtags.com/jquery/1.11.1/jquery.min.js"><
页面CSS:
- /*CSS源代码*/
- #container{
- position: absolute;
- width: 320px;
- height: 320px;
- border:1px solid #ccc;
- }
- #first{
- width: 100px;
- height: 100px;
- background: orange;
- }
- #second{
- width: 100px;
- height: 100px;
- background: red;
- position: absolute;
- right:0;
- bottom:0;
- }
JS:
var $first=$('#first');
var $second=$('#second');
(function firstMove(){
$first.animate({
"left":220,
"top": 0,
},400,"linear",function(){
$first.animate({
"left":220,
"top":220
},400,"linear",function(){
$first.animate({
"left":0,
"top":220
},400,"linear",function(){
$first.animate({
"left":0,
"top":0
},400,"linear",function(){
firstMove();
});
});
});
});
})();
(function secondMove(){
$second.animate({
"right":220,
"bottom":0
},400,"linear",function(){
$second.animate({
"right":220,
"bottom":220
},400,"linear",function(){
$second.animate({
"right":0,
"bottom":220
},400,"linear",function(){
$second.animate({
"right":0,
"bottom":0
},400,"linear",function(){
secondMove();
});
});
});
});
})();
/*Javascript代码片段*/
var $first=$('#first');
var $second=$('#second');
function h(x, currentTime, beginningValue, changeValue, duration){
if(beginningValue){
if(x<0.25){
return (0.25-x)*4;
}else if(x<0.5){
return 0;
}else if(x<0.75){
return (x-0.5)*4;
}
return 1;
}else{
if(x<0.25){
return x*4;
}else if(x<0.5){
return 1;
}else if(x<0.75){
return (0.75-x)*4;
}
return 0;
}
}
function v(x, currentTime, beginningValue, changeValue, duration){
if(beginningValue){
if(x<0.25){
return 1;
}else if(x<0.5){
return (0.5-x)*4;
}else if(x<0.75){
return 0;
}
return (x-0.75)*4;
}else{
if(x<0.25){
return 0;
}else if(x<0.5){
return (x-0.25)*4;
}else if(x<0.75){
return 1;
}
return (1-x)*4;
}
}
$.extend( $.easing,
{
"v":v,
"h":h
});
(function firstMove(){
$first.animate({
"left":220,
"top":220
},{
duration: 1600,
specialEasing: {
left: 'h',
top: 'v'
},
complete: function() {
firstMove();
}
});
})();
(function secondMove(){
$second.animate({
"left":0,
"top":0
},{
duration: 1600,
specialEasing: {
left: 'h',
top: 'v'
},
complete: function() {
secondMove();
}
});
})();
【WorkTile赞助】jQuery编程挑战#009:生成两个div元素互相追逐的动画的更多相关文章
- [jQuery编程挑战]006 生成一个倒计时效果
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- [jQuery编程挑战]008 生成逗号分隔数字
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- [jQuery编程挑战]005 使用最短的代码生成元素的闪烁效果
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- Jquery 动态交换两个div位置并添加Css动画效果
前端网页开发中我们经常会遇到需要动态置换两个DIV元素的位置,常见的思路大多是不考虑原始位置,直接采用append或者appendTo方式将两元素进行添加,该法未考虑原始位置,仅会添加为元素的最后一子 ...
- [jQuery编程挑战]001:实现页面元素加速动画效果
要求: 页面包含两个HTML元素:一个按钮,一个小方块 动画要求:点击按钮,小方块从页面坐标300,300,加速移动到0,0 相关知识点: jQuery动画方法animate easing参数的设置 ...
- [jQuery编程挑战]003 克隆一个页面元素及其相关事件
挑战: a) 绑定一个点击方法到这个div,点击后此元素会淡出消失 b) 同时克隆一个新的div元素到页面,元素内容是前面div文字内容反向书写(即,sgatbg olleh),同样也具有上面的点击事 ...
- [jQuery编程挑战]007 切换数据表格的行列
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- [jQuery编程挑战]004 针对选择框词典式排序
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- [jQuery编程挑战]002:实现一个转盘大抽奖
body { background-color: #F2F2F2; text-align: center; } .container { position: relative; width: 500p ...
随机推荐
- web本地存储-UserData
userData,IE中持久化用户数据的方法. 使用userData用户数据首先必须使用css指定userData行为.代码示例: var ud = document.createElement(&q ...
- WMware VMX格式转换成OVF
1.关于VMX格式 vmx文件是vmware虚拟机系统的配置文件,注意:刚刚安装好VMware Workstation以后是找不到这个文件的,当你在VMware Workstation中建立了一个虚拟 ...
- 文件上传与下载/Mail
文件上传与下载 提交方式:post 表单中要有文件上传的表单项 input type=”file”而且必须有name属性 表单类型要加入 encytype=”mulitpart/form-data” ...
- Unity UGUI 使用 CCTween 实现 打字效果
最近闲来无事 就研究了下 打字效果的实现 结果发现很简单 直白了说是太简单了 下边效果图加代码 走着 增加可拖拽 脚本 (场景物体不支持 alpha 隐藏) 目前 CCAnim 封装了 move R ...
- 如风一样,飞翔------Day37
我一直都觉得自己是个反应没有那么敏锐的人,而不幸的是其实也是,所以,在菜鸟的头衔上,我不得不再背负上一个笨鸟的"光芒".我不苛求一飞冲天的传奇,却也有着不甘寂寞的激昂,我选择先飞, ...
- ASP.NET递归添加树节点
表设计如图: id title parentid 1 asp.net 0 2 c# 0 3 c#_ ...
- Python进阶之面向对象编程概述
Python面向对象编程(一) .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB& ...
- Python进阶之闭包
闭包 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB","S ...
- NodeJS + express访问html、css、JS等静态资源文件
原先做前端开发时都是用XAMPP或LAMP,把HTML.CSS.JS等前端资源放到htdocs下,测试自己的前端代码,但有些不方便的地方是,在调用Ajax请求后无法模拟请求返回的数据,最近学了点Nod ...
- PHP利用递归法获取多级类别的树状数组
数据结构:category(id, pid, name),对应:信息ID,父项ID,类别名 测试数据: $aryCate = array( array('id' => 1, 'pid' => ...