0

jsfiddle 在这里:http: //jsfiddle.net/M86nA/

代码是这样的:

$('div.wrap').click(function(){
   var $minY = $('div.imgur-wrap').height();
   if ($minY = 280) {
      $('div.wrap').animate({height: 500});
   }else{
      $('div.wrap').animate({height: 280});
   }
});​

我想这是因为它在执行 if 时确实有新的高度(500)?我怎样才能做到这一点?

谢谢!

编辑:用“==”替换“=”并在选择器中删除“imgur”修复它。

4

2 回答 2

3

You could use jQuery .toggle() like in this example

$('div.wrap').toggle(function() {
    $(this).animate({
        height: 500
    });
}, function() {
    $('div.wrap').animate({
        height: 280
    });
});​

jQuery will "toggle" between the functions provided in .toggle(), one after the other and back. Since 280px is the initial state, we fire the height 500 first, then the 280 and back again.

于 2012-06-23T00:20:32.413 回答
1

Using your current code, fix the class reference and = to ==.

Change

var $minY = $('div.imgur-wrap').height();

To

var $minY = $('div.wrap').height();

Complete code for reference:

$('div.wrap').click(function() {
    var $minY = $('div.wrap').height();
    if ($minY == 280) {
        $('div.wrap').animate({
            height: 500
        });
    } else {
        $('div.wrap').animate({
            height: 280
        });
    }
});​

See DEMO

于 2012-06-23T00:26:33.160 回答