44

我在屏幕顶部有一个 10 像素的条,当点击它时,我希望它动画到 40 像素的高度,然后如果再次点击,动画回到 10 像素。我尝试更改 div 的 id,但它不起作用。我怎样才能让它工作,或者有更好的方法吗?

正文 html:

<div id="topbar-show"></div>

CSS:

#topbar-show { width: 100%; height: 10px; background-color: #000; }
#topbar-hide { width: 100%; height: 40px; background-color: #000; }

javascript:

$(document).ready(function(){
  $("#topbar-show").click(function(){
    $(this).animate({height:40},200).attr('id', 'topbar-hide');
  });
  $("#topbar-hide").click(function(){
    $(this).animate({height:10},200).attr('id', 'topbar-show');
  });
});
4

9 回答 9

65

试试这个:

$(document).ready(function(){
  $("#topbar-show").toggle(function(){
    $(this).animate({height:40},200);
  },function(){
    $(this).animate({height:10},200);
  });
});
于 2011-02-11T02:36:05.863 回答
18

您可以使用toggle-event(docs)方法分配 2 个(或更多)处理程序,这些处理程序在每次单击时切换。

示例:http: //jsfiddle.net/SQHQ2/1/

$("#topbar").toggle(function(){
    $(this).animate({height:40},200);
},function(){
    $(this).animate({height:10},200);
});

或者您可以创建自己的切换行为:

示例:http: //jsfiddle.net/SQHQ2/

$("#topbar").click((function() {
    var i = 0;
    return function(){
        $(this).animate({height:(++i % 2) ? 40 : 10},200);
    }
})());
于 2011-02-11T02:24:31.557 回答
18

您应该使用 aclass来实现您想要的:

CSS:

#topbar { width: 100%; height: 40px; background-color: #000; }
#topbar.hide { height: 10px; }

javascript:

  $(document).ready(function(){
    $("#topbar").click(function(){
      if($(this).hasClass('hide')) {
        $(this).animate({height:40},200).removeClass('hide');
      } else { 
        $(this).animate({height:10},200).addClass('hide');
      }
    });
  });
于 2011-02-11T02:27:40.830 回答
3

很晚了,但我很抱歉。抱歉,如果这“效率低下”,但如果您发现以上所有方法都不起作用,请尝试一下。也适用于 1.10 以上

<script>
  $(document).ready(function() {
    var position='expanded';

    $("#topbar").click(function() {
      if (position=='expanded') {
        $(this).animate({height:'200px'});
        position='collapsed';
      } else {
        $(this).animate({height:'400px'});
        position='expanded';
      }
    });
   });
</script>
于 2013-11-16T18:03:53.480 回答
1

那将是一种可能性:

$(document).ready(function(){
  $("#topbar").toggle(function(){
    $(this).animate({height:40},200);
  }, 
  function(){
    $(this).animate({height:10},200);
  });
});
#topbar {
  width: 100%;
  height: 10px;
  background-color: #000;
  color: #FFF;
  cursor: pointer;
}
<!DOCTYPE html>
    <html>
    <head>
      <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
      <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    </head>
    <body>
      <div id="topbar"> example </div>
    </body>
    </html>

于 2011-02-11T02:27:19.607 回答
1

我只是想告诉您您的解决方案不起作用的原因:

$(document).ready()执行时,只有$('#topbar-show')选择器可以从 DOM 中找到匹配的元素。该#topbar-show元素尚未创建。

要解决这个问题,您可以使用实时事件绑定

$('#topbar-show').live('click',function(e){});
$('#topbar-hide').live('click',function(e){});

这是解决您的解决方案的最简单方法。这些答案的其余部分进一步为您提供更好的解决方案,而不是修改希望永久的 id 属性。

于 2011-02-11T02:44:25.483 回答
1

你也可以使用这个技巧:替换

$("#topbar").click(function(){

经过

$(document).on("click", "#topbar", function(){

这将在尚未加载的对象上绑定一个事件... ;)

于 2013-06-07T07:42:56.717 回答
1

下面的代码在 jQuery2.1.3 中对我有用

$("#topbar").animate('{height:"toggle"}');

无需计算您的 div 高度、填充、边距和边框。它会照顾。

于 2015-09-25T20:05:49.807 回答
0

为我工作:

$(".filter-mobile").click(function() {
   if ($("#menuProdutos").height() > 0) {
      $("#menuProdutos").animate({
         height: 0
      }, 200);
   } else {
      $("#menuProdutos").animate({
         height: 500
      }, 200);
   }
});
于 2017-01-31T13:13:22.270 回答