2012-10-08 79 views
1

我正在製作手風琴效果,使用切換來展開和收縮div的高度。.toggle()所有高度超過99px的div

當用戶切換一個div時,它會展開高度,我希望前一個切換回來。 因此,我需要選擇所有的兄弟姐妹,但我只想定位已擴展的div,以便再次縮小它的高度。

我想選擇擴展的div使用條件,如果高度超過99像素的功能,我認爲這是最好的方式來選擇只擴展的div。

我哪裏錯了?

我的代碼。

$(function() { 
    jQuery.fn.selectOpen = (function(){ 
     //(this).css('background-color', 'red'); 
       if ($(this).height() > 99) { 
       $(this).trigger(".toggle"); 
       } 
       }); 
      }); 


$("#arrow_01").toggle(function(){ 
$("#arrow_01").attr('src','images/arrow_down.png'); 
    $("#expanding_box_01").animate({height: '100px', }).siblings().selectOpen(); 
}, function(){  
    $('#arrow_01').attr('src','images/arrow_right.png'); 
    $("#expanding_box_01").animate({height: '27px' }); 
});  

回答

3

使用.each()遍歷所有的兄弟姐妹,你也錯誤地使用.toggle作爲事件的名稱,而它應該只是toggle,像這樣:

jQuery.fn.selectOpen = function(){ 
     //(this).css('background-color', 'red'); 
     this.each(function() { 
      if ($(this).height() > 99) { 
       $(this).trigger("toggle"); 
      } 
     }); 
}; 
0

我認爲你是在複雜的。你可以使用CSS和一些類,以確定是否一個項目是開放的:

// bind a click event to an anchor tag 
$("#accordian li > a").click(function(){   
    var $box = $(this).parent(); 
    // if the item has the closed class, proceed, otherwise 
    // it is already open 
    if ($box.is(":not(.open)")) { 
     // animate the currently open sibling closed 
     // remove the open class 
     $box.siblings(".open").animate({height: '27px' }).removeClass("open"); 
     // animate the clicked item and add open class 
     $box.animate({height: '100px' }).addClass("open"); 
    }    
}); ​ 

http://jsfiddle.net/hunter/qSE69/