我是JS的新手,但是我想要一个非常简单的 Accordion ,所以我制作了一个。出于某种原因,当尝试为它的打开/关闭添加放松效果时,我感到茫然。任何帮助将非常感激。谢谢!

CodePen of Accordion

js代码:

(function(){
// This class will be added to the expanded item
var activeItemClass = 'accordion-expanded';
var accordionItemSelector = '.accordion-section';
var toggleSelector = '.accordion-head';

$(toggleSelector).on('click', function() {

    $(this)
        .closest(accordionItemSelector) // go up to the accordion item element
        .toggleClass(activeItemClass)
            .siblings()
            .removeClass(activeItemClass);
});

})();

最佳答案

由于您使用的是jQuery,为什么不这样做:

var accordionItemSelector = '.accordion-body';
var toggleSelector = '.accordion-head';

$(toggleSelector).on('click', function() {
  if (!$(this).next(accordionItemSelector).is(":visible"))
    $(toggleSelector).not($(this)).next(accordionItemSelector).slideUp();
  $(this).next(accordionItemSelector).slideToggle();
});

UPDATED PEN

关于javascript - 在 Accordion 的打开/关闭中添加轻松的效果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34623699/

10-11 20:18