Skip to content

Instantly share code, notes, and snippets.

@harukizaemon
Created November 23, 2010 23:00
Show Gist options
  • Select an option

  • Save harukizaemon/712716 to your computer and use it in GitHub Desktop.

Select an option

Save harukizaemon/712716 to your computer and use it in GitHub Desktop.
/* Toggle Resize an element (if necessary) to fit its container. */
$.fn.toggleSizeToFit = function() {
var width = this.width();
var containerWidth = this.parent().width();
var dimensions;
// If bigger than container
if (width > containerWidth) {
dimensions = { width: width, height: this.height() };
// Shrink to fit
var ratio = containerWidth / dimensions.width;
this.width(containerWidth);
this.height(dimensions.height * ratio);
// Save original dimensions
this.data("sizeToFitDimensions", dimensions);
} else {
dimensions = this.data("sizeToFitDimensions");
// If already shrunk
if (dimensions) {
// Restore original dimensions
this.width(dimensions.width);
this.height(dimensions.height);
// Clear saved copy
this.removeData("sizeToFitDimensions");
} else {
// Otherwise, ensure the element is centred.
this.width("100%");
}
}
};
window.addEventListener("SVGLoad", function() {
/* Resize SVG images (if necessary) to fit their container. */
$(".concept-map svg").each(function() {
$(this).toggleSizeToFit();
});
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment