在"onClick"上切换DIV高度 [英] Toggle DIV height upon `onClick`

查看:89
本文介绍了在"onClick"上切换DIV高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想切换分割的高度.我尝试将.animate与if/else语句一起使用.但它只会反弹. 我现在使用的代码将隐藏我的部门而不是切换高度.单击后将触发.

I want to toggle the height of a division. I've tried using .animate with a if/else statement. but it would just bounce back. The code im now using will hide my division instead of toggling the height. It will trigger when clicked.

$(document).ready(function() {
       $("#content1").toggle(function(){
         $(this).animate({height:'400px'});
   }, function() {
         $(this).animate({height:'200px'});
   });
});

我希望有人能找到答案,因为我无法在google/stackoverflow上找到答案.

I hope someone has the answer because I could nowhere find a answer on google/stackoverflow.

推荐答案

为什么不直接使用.animate()来设置div的高度并在每次单击div时动画化更改!

Why don't you use .animate() directly to set the height of a div and animate the change each time the div is clicked!

喜欢:

$( "div" ).click(function() {
    if ( $(this).height() != 50)
          $( this ).animate({ height: 50 }, 1000 );
    else
          $( this ).animate({ height: 100 }, 1000 );
});

这是它的完整示例: http://jsfiddle.net/62jcH/3/

这篇关于在"onClick"上切换DIV高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆