javascript - Applying a CSS transition when adding a class via JS -
i creating button, when clicked, reveal email input user fill out. working, want smooth transition action not "jerky".
this html button , form
<button id="work" onclick="workyes()">sign up!</button> <div id="workyes" class="invisible"> <form><input type='email'></form> </div>
the css i'm using:
button { border: 0; background: #5786c1; color: white; padding: 8px 14px; font-weight: bold; font-size: 18px; text-decoration: none; display: inline-block; /* needed anchors */ position: relative; box-shadow: 1px 0px #3a587f, 0px 1px #4171ae, 2px 1px #3a587f, 1px 2px #4171ae, 3px 2px #3a587f, 2px 3px #4171ae, 4px 3px #3a587f, 3px 4px #4171ae, 5px 4px #3a587f, 4px 5px #4171ae, 6px 5px #3a587f, 5px 6px #4171ae; -webkit-transition: 1s; /* safari 3.1 6.0 */ transition: 1s; max-height: inherit; } button:hover { box-shadow: none; -webkit-transition: 1s; /* safari 3.1 6.0 */ transition: 1s; } .invisible { display: none; } .visible { display: block; }
and js ties together:
function workyes() { var box = $('#work'); var form = $('#workyes'); box.toggleclass('invisible'); form.toggleclass('visible'); }
i have on hover transition works can't 1 work when .invisible
or .visible
classes added script.
is there way make css transitions work or add effect in different.
i think can add second argument toggleclass();
function specifies duration smooth out transition:
function workyes() { var box = $('#work'); var form = $('#workyes'); box.toggleclass('invisible', 1000); form.toggleclass('visible', 500); }
alternatively can use fadetoggle();
, slidetoggle();
or fadein();
/ fadeout();
functions.
note - should use jquery's click function rather onclick html attribute.
html
<button id="clicktoshowform">sign up!</button> <div class="hidden"> <form><input type='email'></form> </div>
css
.hidden { display:none; }
jquery
$('clicktoshowform').click(function(){ $('hidden').fadetoggle(); });
Comments
Post a Comment