2011-02-01 72 views
11

我試圖創建一個手風琴,我可以通過一次單擊來展開/摺疊所有部分。我還需要用戶能夠打開和關閉一次打開0-n部分的部分。在這裏討論stackoverflow和jQuery論壇中的幾個討論,下面是我提出的解決方案: 我已經實現了每個部分,因爲它是自己的手風琴,每個部分都設置爲collapsible = true。jquery ui accordion - 多個手風琴展開/摺疊所有樣式的問題

<html> 
    <head> 
     <title>Accordion Test</title> 

     <script type="text/javascript" src="../scripts/jquery-1.4.2.min.js"></script> 
     <script type="text/javascript" src="../scripts/jquery-ui-1.8.4.custom.min.js"></script> 

     <link rel="stylesheet" href="../_templates/css/jquery-ui-1.8.6.custom.css" type="text/css" /> 
     <link rel="stylesheet" href="../_templates/css/jquery.ui.accordion.css" type="text/css" /> 
    </head> 

<body> 
     <a onClick="expandAll()">Expand All</a> 
     <br> 
     <a onClick="collapseAll()">Collapse All</a> 
      <div id="accordion1" class="accord"> 
      <h5><a href="#">section 1</a></h5> 
      <div> 
        section 1 text 
      </div> 
      </div> 

      <!-- orders section --> 
      <div id="accordion2" class="accord"> 
      <h5><a href="#">section 2</a></h5> 
      <div> 
        section 2 text 
      </div> 
      </div> 

      <!-- section 3 --> 
      <div id="accordion3" class="accord"> 
      <h5><a href="#">section 3</a></h5> 
      <div> 
        section 3 text 
      </div> 
      </div> 

      <!-- section 4 --> 
      <div id="accordion4"> 
      <h5><a href="#">section 4</a></h5> 
      <div> 
        section 4 text     
      </div> 
      </div> 


</body> 
</html> 


<script type="text/javascript"> 

$(function() { 
    $('#accordion1').accordion({ 
     header: 'h5', 
     collapsible: true, 
     autoHeight: false 
    }); 
}); 
$(function() { 
    $('#accordion2').accordion({ 
     header: 'h5', 
     collapsible: true, 
     autoHeight: false, 
     active: false 
    }); 
}); 
$(function() { 
    $('#accordion3').accordion({ 
     header: 'h5', 
     collapsible: true, 
     autoHeight: false, 
     active: false 
    }); 
}); 
$(function() { 
    $('#accordion4').accordion({ 
     header: 'h5', 
     collapsible: true, 
     autoHeight: false, 
     active: false 
    }); 
}); 

</script> 

<script type="text/javascript"> 
$(document).ready(function() { 

}) 

function expandAll() { 
    alert("calling expandAll"); 
    $("#accordion1, #accordion2, #accordion3, #accordion4") 
     .filter(":not(:has(.ui-state-active))") 
     .accordion("activate", 0); 
} 

function collapseAll() { 
    alert("calling collapseAll"); 
    $("#accordion1, #accordion2, #accordion3, #accordion4") 
     .filter(":has(.ui-state-active)") 
     .accordion("activate", -1); 
} 

</script> 

我快到的問題是,當我點擊的開口部分的標題中,部分預期倒塌,但頭仍然有「UI狀態爲關注焦點」級,直到我點擊頁面上的其他地方。因此,我在ui中看到的是剛剛關閉的部分的標題與我的懸停效果具有相同的背景顏色,直到我點擊其他位置,並且它轉變爲「默認的,不是焦點」的顏色。

此外,當我使用「全部摺疊」鏈接時,在Firefox中看起來都很棒。在IE中,最後一節標題具有相同的懸停焦點着色。

有什麼建議嗎?當它關閉時,我是否需要強迫手風琴失去焦點?我將如何實現這一目標?

+0

這不是一個手風琴的解決方案。在樣品中使用了多個手風琴。 – jmav 2011-04-12 12:47:30

回答

3

嘗試在頁面上覆蓋我的jquery-ui樣式,並試圖破解手風琴javascript以移除ui-state-focus類後,發現了一個簡單的解決方案。

因爲當我點擊頁面上的其他位置時,我的頁面正在顯示預期的行爲,所以我使用blur()來失去焦點。

$(document).ready(function() { 
    // forces lose focus when accordion section closed. IE and FF. 
    $(".ui-accordion-header").click(function(){ 
      $(this).blur(); 
     }); 

}) 

要解決的崩潰在IE中的所有問題,我加了1號線到我的collapseAll()方法。

function collapseAll() { 
    alert("calling collapseAll"); 
    $("#accordion1, #accordion2, #accordion3, #accordion4") 
     .filter(":has(.ui-state-active)") 
     .accordion("activate", -1); 
    $(".ui-accordion-header").blur(); 
} 
3

解決方案與所有開放面板實施手風琴。面板是靜態的,不能關閉。

不要用手風琴小部件初始化手風琴div!

$("#accordion").addClass("ui-accordion ui-widget ui-helper-reset") 
    .find('h3') 
    .addClass("current ui-accordion-header ui-helper-reset ui-state-active ui-corner-top") 
    .prepend('<span class="ui-icon ui-icon-triangle-1-s"/>') 
    .next().addClass("ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active"); 
2

這就是我的回答〜希望其幫助

多個打開你可以像這樣通過使用現有的jQuery UI的只是一個選項beforeActivate補充:下面

我的代碼:

jQuery UI accordion that keeps multiple sections open?

和復:

$("#accordion").accordion({ 
     header: "> div > h3", 
     autoHeight: false, 
     collapsible: true, 
     active: false, 
     beforeActivate: function(event, ui) { 
      // The accordion believes a panel is being opened 
      if (ui.newHeader[0]) { 
       var currHeader = ui.newHeader; 
       var currContent = currHeader.next('.ui-accordion-content'); 
      // The accordion believes a panel is being closed 
      } else { 
       var currHeader = ui.oldHeader; 
       var currContent = currHeader.next('.ui-accordion-content'); 
      } 
      // Since we've changed the default behavior, this detects the actual status 
      var isPanelSelected = currHeader.attr('aria-selected') == 'true'; 

      // Toggle the panel's header 
      currHeader.toggleClass('ui-corner-all',isPanelSelected).toggleClass('accordion-header-active ui-state-active ui-corner-top',!isPanelSelected).attr('aria-selected',((!isPanelSelected).toString())); 

      // Toggle the panel's icon 
      currHeader.children('.ui-icon').toggleClass('ui-icon-triangle-1-e',isPanelSelected).toggleClass('ui-icon-triangle-1-s',!isPanelSelected); 

      // Toggle the panel's content 
      currContent.toggleClass('accordion-content-active',!isPanelSelected)  
      if (isPanelSelected) { currContent.slideUp('fast'); } else { currContent.slideDown('fast'); } 

      return false; // Cancels the default action 
     } 
    }); 

從參考nction塌陷和膨脹

function accordion_expand_all() 
{ 
    var sections = $('#accordion').find("h3"); 
    sections.each(function(index, section){ 
    if ($(section).hasClass('ui-state-default') && !$(section).hasClass('accordion-header-active')) { 
     $(section).click(); 
    } 
    }); 

} 

function accordion_collapse_all() 
{ 
    var sections = $('#accordion').find("h3"); 
    sections.each(function(index, section){ 
    if ($(section).hasClass('ui-state-active')) { 
     $(section).click(); 
    } 
    }); 
} 

就是這樣..

+0

非常好的解決方案!喜歡它!做得好! – 2016-11-19 16:14:37