javascript - CSS3 transitions seem to need a "warmup" time before they kick off? Do I have to add a wait? -
i have css3 transition shrink height of element. i'm doing by:
- css3 transition , destination height specified on class (e.g. height:5px in class)
- assign height=$el.height() element in style attribute (e.g. height:100px on style)
- add class element
- remove height defined in style attribute
- computed css value changes style's 100px class's 5px, triggers transition
what i'm discovering there seems delay on setting css3 transition trigger, , if step #4 done after #3, trigger see height auto instead of 100px, , no transition happens (due limitation of css3, can't transition height:auto height:x).
here's actual code:
$(document).ready(function() { $a = $('#box'); // height:auto, not specified $a.css('height',$a.height()); // height:100px on style; $a.addclass('shrink'); // height:100px on style, height:5px on class $a.css('height',''); // height removed style, falls 5px on class });
here's jsfiddle demonstrating issue: http://jsfiddle.net/1escylqf/2/
in fiddle, if add delay of 20ms before removing height value, transition works. if comment line out , call css('height','') after adding class, it's fast , no transition happens.
is bug in browser implementation of css3 transitions? or need add in timeout?
create css3 animation appended in jquery dynamic starting height.
the jquery
get boxes height:
var boxheight = $('#box').height();
create animation , apply shrink class. starting height variable.
var animation = ' <style type="text/css"> .shrink { -webkit-animation: shrink 12s forwards; animation: shrink 12s forwards; } @-webkit-keyframes shrink { 0% { height:' + boxheight + 'px; } 100% { height: 5px; } } @keyframes shrink { 0% { height:' + boxheight + 'px; } 100% { height: 5px; } } </style>';
append style
<head>
$('head').append(animation);
complete example
$(document).ready(function() { var boxheight = $('#box').height(); var animation = '<style type="text/css"> .shrink { -webkit-animation: shrink 12s forwards; animation: shrink 12s forwards; } @-webkit-keyframes shrink { 0% { height:' + boxheight + 'px; } 100% { height: 5px; } } @keyframes shrink { 0% { height:' + boxheight + 'px; } 100% { height: 5px; } }</style>'; $('head').append(animation); });
#box, #box2 { background: red; margin: 10px; } .shrink { overflow: hidden; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <div id="box" class="shrink"> <p>make height</p> <p>make height</p> <p>make height</p> <p>make height</p> </div>
Comments
Post a Comment