Skip to content

Instantly share code, notes, and snippets.

@emperorcezar
Created December 14, 2010 20:16
Show Gist options
  • Save emperorcezar/741018 to your computer and use it in GitHub Desktop.
Save emperorcezar/741018 to your computer and use it in GitHub Desktop.
This is a small jquery plugin that allows a div that partially collapses
(function( $ ){
$.fn.pCollapse = function( options ) {
var settings = {
'start': '100px',
};
// there's no need to do $(this) because
// "this" is already a jquery object
// $(this) would be the same as $($('#element'));
return this.each(function() {
// If options exist, lets merge them
// with our default settings
if ( options ) {
$.extend( settings, options );
}
var $this = $(this);
$this.attr('full-height', $this.height());
var after = $this.after('<div><a href="#">More</a></div>').next();
$(after).click(function(){
if ( $this.height() == $this.attr('full-height') ){
// Collapse
$this.animate({height:settings.start});
$('a', after).html('More');
} else {
$this.animate({height:$this.attr('full-height') + 'px'});
$('a', after).html('Less');
}
});
$this.css('height', settings.start);
$this.css('overflow', 'hidden');
});
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment