Created
March 27, 2014 21:54
-
-
Save bruno-de-queiroz/9819896 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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