开发者

Edit Css with animation effect

开发者 https://www.devze.com 2023-01-03 19:00 出处:网络
I\'ve a div in a absolute position with a default \"top\" value set to \"-200px\". When i click on the div the \"top\" value is updated to \"0\". In this way I see the entire Div.

I've a div in a absolute position with a default "top" value set to "-200px". When i click on the div the "top" value is updated to "0". In this way I see the entire Div.

This is the js:

$(document).ready(function(开发者_如何学Python){
  $("#search_bar").click(function () {
    $(this).css("top","0");
  });
});

Is there a way to achieve this result but with an animation? For example, a slide down effect?

Also, when i click another time to the div I'd like that the "top" value will be restored to "-200px".


Of course. Use the .animate() method. And instead of the click event, use toggle which accepts two functions for before/after.

Live Example: http://jsfiddle.net/a86tm/1/

$(document).ready(function(){

  $("#search_bar").toggle(  // toggle() takes 2 functions
      function () {
          $(this).animate({top: 0}, 500);  // Use animate() to animate the transition.
      },                                   // The 500 argument is the duration.
      function() {
          $(this).animate({top: -200}, 500);
      }
  );

});​

jQuery docs:

  • .toggle() - http://api.jquery.com/toggle
  • .animate() - http://api.jquery.com/animate
0

精彩评论

暂无评论...
验证码 换一张
取 消