【问题标题】:Edit Css with animation effect使用动画效果编辑 Css
【发布时间】:2010-06-13 08:31:43
【问题描述】:

我有一个绝对位置的 div,默认“顶部”值设置为“-200px”。 当我单击 div 时,“顶部”值更新为“0”。这样我就看到了整个Div。

这是js:

$(document).ready(function(){
  $("#search_bar").click(function () {
    $(this).css("top","0");
  });
});

有没有办法通过动画实现这个结果?比如滑下效果?

另外,当我再次点击 div 时,我希望“top”值恢复为“-200px”。

【问题讨论】:

标签: jquery css slidedown


【解决方案1】:

当然。使用.animate() 方法。而不是click 事件,使用toggle 接受之前/之后的两个函数。

现场示例: 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 文档:

【讨论】:

  • 此代码不适用于谷歌浏览器!控制台给我这个错误: Uncaught SyntaxError: Unexpected token ILLEGAL at line 6 这是脚本的最后一行。
  • @Pennywise - 我猜你从 jsFiddle 复制了代码?如果是这样,则有一些不可见的(非法)字符会被复制。要么删除代码末尾的一些空格,要么从上面复制并粘贴代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2015-03-13
相关资源
最近更新 更多