Advertisement
putuebo

smk-accordion.js

Mar 18th, 2017
1,297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * SMK Accordion jQuery Plugin v1.3
  3.  * ----------------------------------------------------
  4.  * Author: Smartik
  5.  * Author URL: http://smartik.ws/
  6.  * License: MIT
  7.  */
  8. ;(function ( $ ) {
  9.  
  10.     $.fn.smk_Accordion = function( options ) {
  11.        
  12.         if (this.length > 1){
  13.             this.each(function() {
  14.                 $(this).smk_Accordion(options);
  15.             });
  16.             return this;
  17.         }
  18.        
  19.         // Defaults
  20.         var settings = $.extend({
  21.             animation:  true,
  22.             showIcon:   true,
  23.             closeAble:  false,
  24.             closeOther: true,
  25.             slideSpeed: 150,
  26.             activeIndex: false
  27.         }, options );
  28.  
  29.         if( $(this).data('close-able') )    settings.closeAble = $(this).data('close-able');
  30.         if( $(this).data('animation') )     settings.animation = $(this).data('animation');
  31.         if( $(this).data('show-icon') )     settings.showIcon = $(this).data('show-icon');
  32.         if( $(this).data('close-other') )   settings.closeOther = $(this).data('close-other');
  33.         if( $(this).data('slide-speed') )   settings.slideSpeed = $(this).data('slide-speed');
  34.         if( $(this).data('active-index') )  settings.activeIndex = $(this).data('active-index');
  35.  
  36.         // Cache current instance
  37.         // To avoid scope issues, use 'plugin' instead of 'this'
  38.         // to reference this class from internal events and functions.
  39.         var plugin = this;
  40.  
  41.         //"Constructor"
  42.         var init = function() {
  43.             plugin.createStructure();
  44.             plugin.clickHead();
  45.         }
  46.  
  47.         // Add .smk_accordion class
  48.         this.createStructure = function() {
  49.  
  50.             //Add Class
  51.             plugin.addClass('smk_accordion');
  52.             if( settings.showIcon ){
  53.                 plugin.addClass('acc_with_icon');
  54.             }
  55.  
  56.             //Create sections if they were not created already
  57.             if( plugin.find('.accordion_in').length < 1 ){
  58.                 plugin.children().addClass('accordion_in');
  59.             }
  60.  
  61.             //Add classes to accordion head and content for each section
  62.             plugin.find('.accordion_in').each(function(index, elem){
  63.                 var childs = $(elem).children();
  64.                 $(childs[0]).addClass('acc_head');
  65.                 $(childs[1]).addClass('acc_content');
  66.             });
  67.            
  68.             //Append icon
  69.             if( settings.showIcon ){
  70.                 plugin.find('.acc_head').prepend('<div class="acc_icon_expand"></div>');
  71.             }
  72.  
  73.             //Hide inactive
  74.             plugin.find('.accordion_in .acc_content').not('.acc_active .acc_content').hide();
  75.  
  76.             //Active index
  77.             if( settings.activeIndex === parseInt(settings.activeIndex) ){
  78.                 if(settings.activeIndex === 0){
  79.                     plugin.find('.accordion_in').addClass('acc_active').show();
  80.                     plugin.find('.accordion_in .acc_content').addClass('acc_active').show();
  81.                 }
  82.                 else{
  83.                     plugin.find('.accordion_in').eq(settings.activeIndex - 1).addClass('acc_active').show();
  84.                     plugin.find('.accordion_in .acc_content').eq(settings.activeIndex - 1).addClass('acc_active').show();
  85.                 }
  86.             }
  87.            
  88.         }
  89.  
  90.         // Action when the user click accordion head
  91.         this.clickHead = function() {
  92.  
  93.             plugin.on('click', '.acc_head', function(){
  94.                
  95.                 var s_parent = $(this).parent();
  96.                
  97.                 if( s_parent.hasClass('acc_active') == false ){
  98.                     if( settings.closeOther ){
  99.                         plugin.find('.acc_content').slideUp(settings.slideSpeed);
  100.                         plugin.find('.accordion_in').removeClass('acc_active');
  101.                     }  
  102.                 }
  103.  
  104.                 if( s_parent.hasClass('acc_active') ){
  105.                     if( false !== settings.closeAble ){
  106.                         s_parent.children('.acc_content').slideUp(settings.slideSpeed);
  107.                         s_parent.removeClass('acc_active');
  108.                     }
  109.                 }
  110.                 else{
  111.                     $(this).next('.acc_content').slideDown(settings.slideSpeed);
  112.                     s_parent.addClass('acc_active');
  113.                 }
  114.  
  115.             });
  116.  
  117.         }
  118.  
  119.         //"Constructor" init
  120.         init();
  121.         return this;
  122.  
  123.     };
  124.  
  125.  
  126. }( jQuery ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement