Created
November 23, 2010 23:00
-
-
Save harukizaemon/712716 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 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