Skip to content

Instantly share code, notes, and snippets.

@ingowennemaring
Created August 22, 2015 23:53
Show Gist options
  • Save ingowennemaring/e14df731b53a2431bf7c to your computer and use it in GitHub Desktop.
Save ingowennemaring/e14df731b53a2431bf7c to your computer and use it in GitHub Desktop.
Script for an simple accordion
( function ( sR, $ ) {
sR.ACCORDION = {
// set the default options
//defaults: {},
init: function ( /*options*/) {
// merge the options
//this.settings = $.extend( {}, this.defaults, options );
if ( $( '.accordion' ).length ) {
this.initModule();
}
// listen at global event for initializing accordions
$( document ).on(
'srEvent.InitAccordion',
function () {
sR.ACCORDION.accordion();
}
);
},
initModule: function () {
this.accordion();
},
accordion: function () {
var closeSelfOnClick = true,
closeOtherOnClick = true;
var accordionClickHandler = function ( $sectionLink ) {
var $section = $sectionLink.closest( '.accordion__section' );
// if this section is already active then close it
if ( $section.is( '.is-active' ) ) {
if ( closeSelfOnClick ) close( $section );
} else {
// if there is no subsection and element is a link then just open the link
if ( !$section.find( '.accordion__section__sub' ).length && $sectionLink.is( 'a' ) ) {
location.href = $sectionLink.attr( 'href' );
// or else, open the section
} else {
if ( closeOtherOnClick ) close( $( '.accordion__section.is-active' ) );
open( $section );
}
}
};
var open = function ( $section ) {
if ( sR.isTouch ) {
$section.addClass( 'is-active' ).find( '.accordion__section__content' ).show();
} else {
$section.addClass( 'is-active' ).find( '.accordion__section__content' ).slideDown( {
duration: 200
// step: function() {
// // Event feuern, falls andere Skripts etwas neu positionieren müssen
// $(window).trigger('scroll').trigger('resize');
// }
} );
}
};
var close = function ( $section ) {
if ( sR.isTouch ) {
$section.removeClass( 'is-active' ).children( '.accordion__section__content' ).hide();
} else {
$section.removeClass( 'is-active' ).children( '.accordion__section__content' ).slideUp( {
duration: 200
// step: function() {
// // Event feuern, falls andere Skripts etwas neu positionieren müssen
// $(window).trigger('scroll').trigger('resize');
// }
} );
}
};
// toggle the respective accordion section on click
$( '.accordion' ).find( '.accordion__section__link' ).on(
'click',
function ( event ) {
event.preventDefault();
accordionClickHandler( $( this ) );
}
);
}
};
} )( sR, jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment