/*! * Vallenato 1.0 * A Simple JQuery Accordion * * Designed by Switchroyale * * Use Vallenato for whatever you want, enjoy! * * Modified version: added ability to activate specified sections by passing header id in url hash. * Following url hash "#accordion-header-{id}" will activate header with data attribute data-header="{id}" */ var isDisableAccordionOpen = false; function disableAccordionOpen () { isDisableAccordionOpen = true; } $(function () { var headers = $('.accordion-header'); var activateSection = function (container, headerPrefix) { "use strict"; headerPrefix = headerPrefix || 'accordion-header-'; headerPrefix = headerPrefix.replace(/([-^$.+*?|(){}[\]\/\\])/g, '\\$1'); var headerExpr = new RegExp('#' + headerPrefix + '(.*)'); var hash, headerId, header; hash = window.location.hash || ''; headerId = hash && hash.match(headerExpr)[1]; if (headerId) { header = container.find('.accordion-header[data-header="' + headerId + '"]:first'); } else { header = container.find('.accordion-header:first'); } header.addClass('active-header').removeClass('inactive-header'); header.next('.accordion-content').slideDown().addClass('open-content'); if (headerId && header.size()) { $('html, body').animate({ scrollTop: header.offset().top }, 500); } }; // add inactive class to all accordion headers headers.addClass('inactive-header'); // set the accordion content width var contentWidth = headers.width(); $('.accordion-content').css({'width' : contentWidth }); // open the first accordion section or section specified by url hash when page loads $('.accordion-container').each(function () { if (!isDisableAccordionOpen) { activateSection($(this)); } }); // the accordion effect headers.click(function () { if($(this).is('.inactive-header')) { $(this).parent('.accordion-container').find('.active-header').toggleClass('active-header').toggleClass('inactive-header').next().slideToggle().toggleClass('open-content'); $(this).toggleClass('active-header').toggleClass('inactive-header'); $(this).next().slideToggle().toggleClass('open-content'); } else { $(this).toggleClass('active-header').toggleClass('inactive-header'); $(this).next().slideToggle().toggleClass('open-content'); } }); });