【WorkTile赞助】jQuery编程挑战#009:生成两个div元素互相追逐的动画
2015-03-10 13:53
169 查看
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源代码*/
#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();
}
});
})();
<!-- 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();
}
});
})();
相关文章推荐
- Html5实现如何在两个div元素之间拖放图像
- JQuery 两个ul标签中的li互相移动(动画队列)
- tween.js是一款可生成平滑动画效果的js动画库。tween.js允许你以平滑的方式修改元素的属性值。它可以通过设置生成各种类似CSS3的动画效果。
- js 交换两个元素的位置,动画效果
- 用js实现两个select下拉框之间的元素互相移动
- 如下两个div元素垂直距离是多少?
- 两个select之间的元素互相移动并保持顺序
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree(给你n个点,m条边,有两个给定的点S,T以及它们在生成树中最大的度数)
- 动画队列,第一个动画的字体变大和第二个动画的元素高度增加同时进行,两个动画完成之后,才触发第3个动画
- JSP动态生成页面中使用JavaScript消除里面没有信息的div等元素
- Html 5:如何在两个 <div> 元素之间拖放图像
- 把页面上DIV元素生成图片
- 你能相信吗?这是由一个DIV元素实现的动画,纯CSS3技术
- 同一个div两个元素并排,间距10
- 使用jQuery生成div元素闪烁效果
- 用js实现两个select下拉框之间的元素互相移动
- 利用<div>层 和<img>标签,实现一个图片两个动画效果
- 将一个div中的两个P元素同一行居中显示
- QT 利用两种方式实现两个list中元素的互相移动