Guest User

Untitled

a guest
Jun 18th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. (function($){
  2. $.fn.accordion = function(options){
  3. options = $.extend({
  4. 'header': 'h1', //The header that will be clicked
  5. 'content': 'div', //The content, this must be a sibling of the header
  6. 'showFirst': false, //If true the first item will be expanded on page load
  7. 'easing': null, //The easing style of the animation
  8.  
  9. //Called when the content will hide
  10. 'willHide': null,
  11. //Called when the content was hidden
  12. 'didHide': null,
  13.  
  14. //The content element is passed to these function
  15. //Called when the content will be shown
  16. 'willShow': null,
  17. //Called when the content was shown
  18. 'didShow': null
  19. }, options);
  20.  
  21. return this.each(function(){
  22. var root = this;
  23. //set the width to help slideDown figure out the right height
  24. var allContents = $(root).find(options.content).width($(root).width());
  25.  
  26. //find headers
  27. $(root).find(options.header).each(function(i){
  28. var header = this;
  29.  
  30. //find the content
  31. var content = $(header).siblings(options.content)[0];
  32. if(content){
  33. if(i > 0 || i === 0 && options.showFirst === false){
  34. $(content).hide();
  35. }
  36.  
  37. //run the show function if the first item is visable on load
  38. if(i === 0 && options.showFirst === true && (options.willShow || options.didShow)){
  39. if(options.willShow){
  40. options.willShow(content);
  41. }else if(options.didShow){
  42. options.didShow(content);
  43. }
  44. }
  45.  
  46. $(header).click(function(){
  47. //slide up all content elements
  48. allContents.slideUp(options.easing);
  49.  
  50. if(options.willHide){
  51. options.willHide();
  52. }
  53.  
  54. //if the content element is hidden show it
  55. if($(content).is(':hidden') === true){
  56. if(options.willShow){
  57. options.willShow(content);
  58. }
  59.  
  60. $(content).slideDown(options.easing, function(){
  61. options.didShow(content);
  62. });
  63. }
  64. });
  65. }
  66. });
  67. });
  68. };
  69. })(jQuery);
Add Comment
Please, Sign In to add comment