Skip to content

Instantly share code, notes, and snippets.

@bruno-de-queiroz
Created March 27, 2014 21:54
Show Gist options
  • Select an option

  • Save bruno-de-queiroz/9819896 to your computer and use it in GitHub Desktop.

Select an option

Save bruno-de-queiroz/9819896 to your computer and use it in GitHub Desktop.
#container { background:lightgrey; border:1px solid #CCC; height:400px; width:400px; position:relative}
#box { position:absolute; bottom:0; transition: all 1s ease-in; height:10%; width:10%; background:black; border-radius:20px; }
#box:before { content:''; width:50%; height:50%; border-radius:10px; display:inline-block; background:green; position:absolute; left:0; top:50%; margin-top:-25%;}
#box:after { content:''; width:50%; height:50%; border-radius:10px; display:inline-block; background:green; position:absolute; right:-25%; top:50%; margin-top:-25%;}
#box.top { transition:all .3s ease-in-out; }
#box.initial { bottom:50%; margin-bottom:-5px }
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-git2.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="container">
<div id="box" class="initial"></box>
</div>
</body>
</html>
var box = $('#box'),
container = $("#container"),
time = Math.round(container.height()/100)/10,
jumpHeightPercent = 20,
boxPercent = box.height()/container.height() * 100,
t = 0;
window.addEventListener('keypress', function( e ) {
if( box.offset().top > 0){
var containerHeight = $( container ).height(),
bottom = 100 - ($( box ).offset().top/$( container ).height() *100),
next = (bottom + jumpHeightPercent) > 100 ? bottom + (100 - bottom) : bottom + jumpHeightPercent,
dropTime = time + ( time * (next/100));
if( e.keyCode == 32 ){
$( box ).removeAttr( 'style' ).removeClass().addClass( 'top' ).css({
bottom : (next - boxPercent) + "%"
});
clearTimeout( t );
t = setTimeout(function() {
$( box ).removeClass( 'top' ).removeAttr( 'style' );
$( box ).css('-webkit-transition', 'all '+ dropTime + 's ease-in');
}, 300);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment