public
Last active

modified collapsible.js

  • Download Gist
jQuery.collapsible.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/**
* --------------------------------------------------------------------
* jQuery collapsible plugin
* Author: Scott Jehl, scott@filamentgroup.com
* Copyright (c) 2009 Filament Group
* licensed under MIT (filamentgroup.com/examples/mit-license.txt)
* --------------------------------------------------------------------
*
* See: http://dwpe.googlecode.com
* http://filamentgroup.com/lab/expand_and_collapse_content_accessibly_with_progressive_enhancement_jquery/
*
* some local changes by jrochkind, eventually would like to publish these as a
* fork. For now, just diff this against a version from the repo to see changes
* I guess.
*
* Requires these CSS to be defined:
.collapsible-heading {
position: relative;
}
.collapsible-heading .ui-icon {
margin-top:-8px;
position:absolute;
top:50%;
}
.collapsible-heading .collapsible-heading-status {
padding-left:1.4em;
}
*/
$.fn.collapsible = function(){
return $(this).each(function(){
//define
var collapsibleHeading = $(this);
var collapsibleContent = collapsibleHeading.next();
//modify markup & attributes
collapsibleHeading.addClass('collapsible-heading')
.prepend('<span class="collapsible-heading-status"></span>')
.prepend('<span class="collapsible-heading-icon ui-icon ui-icon-triangle-1-s"/>')
.wrapInner('<a href="#" class="collapsible-heading-toggle"></a>');
collapsibleContent.addClass('collapsible-content');
//events
collapsibleHeading
.bind('collapse', function(){
$(this)
.addClass('collapsible-heading-collapsed')
.find(".collapsible-heading-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s")
.end()
.find('.collapsible-heading-status').text('Show ');
collapsibleContent.slideUp(function(){
$(this).addClass('collapsible-content-collapsed').removeAttr('style').attr('aria-hidden',true).hide();
});
})
.bind('expand', function(){
$(this)
.removeClass('collapsible-heading-collapsed')
.find(".collapsible-heading-icon").toggleClass("ui-icon-triangle-1-e ui-icon-triangle-1-s")
.end()
.find('.collapsible-heading-status').text('Hide ');
collapsibleContent
.slideDown(function(){
$(this).removeClass('collapsible-content-collapsed').removeAttr('style').attr('aria-hidden',false).show();
});
})
.click(function(){
if( $(this).is('.collapsible-heading-collapsed') ){
$(this).trigger('expand');
}
else {
$(this).trigger('collapse');
}
return false;
})
.trigger('collapse');
});
};

Changed from original Filmanet version to:

1) Use ThemeRoller triangle icons for expand-contract triangles. This requires inserting a span element before the link. I also manually set the style on this collapse element to float:left, which is kind of hacky, but one of my goals was avoiding the need for any additional CSS dependencies to make the collapsible element work, so I chose to inline it instead of depending on declaring a separate style to do it.

2) Explicitly calls show() and hide() on the content, again to eliminate the need for any additional CSS dependencies.

As a result, this js file alone will give you great collapsible behavior, no need to install any additional CSS or image files in your app whatseover, just this js file will do it. The previous classes are still left in place in case you do want to supply custom styles, however.

Thanks to Scott/Filament for the code and the MIT license.

Okay, I gave up on not requiring any external CSS at all. The Theme Roller triangle wasn't displaying right, no way to make it display right without some more complex CSS.

I based the CSS to make the triangle icon display properly off the JQuery-UI accordion.

This starts to get more complicated, alas. JQuery-UI/ThemeRoller icons are a bit hacky, in the end.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.