jquery - Animate click toggle -
i animate div when click (click toggle in example).i want animate initial position when click again. (i try variable , if, not work. perhaps there easier way? or there mistake?)
please check answer here:http://jsfiddle.net/uxvxh/2/
html:
<div id="logo"></div>
css:
#logo { position:fixed; bottom:-40px;left: 5%; width:70px; height:80px; background:blue; cursor:pointer; }
jquery:
$(function(){ /* click simple $("#logo").click(function() { $("#logo").animate({bottom: "0"}, 1200) }); */ /*click toggle ?*/ var hidden = true; $("#logo").click(function() { if (hidden) { $("#logo").animate({bottom: "0"}, 1200); } else { $("#logo").animate({bottom: "-40"}, 1200); } state = !hidden; }); })
your example working did little mistake change state
hidden
variable.
var hidden = true; $("#logo").click(function() { if (hidden) { $("#logo").animate({bottom: "0"}, 1200); } else { $("#logo").animate({bottom: "-40"}, 1200); } hidden = !hidden; });
Comments
Post a Comment