Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions animatedList.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/jquery-2.1.4.js">
</script>
<script src="js/animatedList.js">
</script>
<script>
$(function(){
var $ul = $('<ul>')
.appendTo($('body'))
.animatedList(6)
var $ul = $('<ul>')
.appendTo($('body'))
.animatedList(6)
})
</script>
</head>
<body>
</body>
</html>
40 changes: 40 additions & 0 deletions bouncingBall.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/jquery-2.1.4.js">
</script>
<script>
var PERIOD = 300;
$(function(){
var $ball = $('.ball');
var startTop = parseInt($ball.css('top'));
$('.ball').animate({
left: 1000
}, {
easing: 'linear',
duration: 5000,
step: function(now, tween){
this.style.top = parseInt(startTop - startTop * Math.sin((tween.now - tween.start) * 2 * Math.PI / PERIOD)) + 'px';
}
});
});
</script>
<style type="text/css">
.ball {
position: absolute;
top: 100px;
left: 5px;
width: 5px;
height: 5px;
background-color: red;
border-radius: 2.5px;
}
</style>
</head>
<body>
<div class="ball">
</div>
</body>
</html>
67 changes: 67 additions & 0 deletions js/animatedList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
$.fn.animatedList = function(count){
var END_WIDTH = 200;
var cb = function(){
var $next = $(this).next();
if ($next[0]) animateLi($next)
}
var $ul = this;
var $li;
var parseColor = function(color) {
if (color.charAt(0) == '#') {
var nhex = (color.length - 1) / 3;
return [
parseInt(color.substr(1, nhex), 16),
parseInt(color.substr(1+nhex, nhex), 16),
parseInt(color.substr(1+2*nhex, nhex), 16),
]
} else {
color = color.replace(/rgb\((.*?)\)/, '$1').split(', ');
return [
parseInt(color[0]),
parseInt(color[1]),
parseInt(color[2])
];
}
};

var buildColor = function(channels) {
return 'rgb('+
channels[0]+', '+
channels[1]+', '+
channels[2]+')';
}

var animateBgColor = function(now, tween) {
var fromColor = parseColor(this.style.backgroundColor)
var toColor = [255,127,63];
var coef = tween.now/tween.end;
var currentColor = [
parseInt(fromColor[0]+(toColor[0]-fromColor[0])*coef),
parseInt(fromColor[1]+(toColor[1]-fromColor[1])*coef),
parseInt(fromColor[2]+(toColor[2]-fromColor[2])*coef)
];
this.style.backgroundColor = buildColor(currentColor);
};

var animateLi = function($el){
$el.animate({
'width':END_WIDTH
}, {
'duration':500,
'complete':cb,
'step':animateBgColor
});
};

for (var i=0; i<count; i++) {
$li = $('<li>')
.width(1)
.appendTo($ul)
.css({
'backgroundColor': '#f00',
'height': 20
})
if (!i)
animateLi($li);
}
};
Loading