A JavaScript plugin to contain an element within its parent element
Find a file
2017-01-13 16:02:20 -05:00
.editorconfig Clean up syntax, upgrade gulp-uglify and rebuild the minified version 2016-06-20 16:53:45 -04:00
.gitignore Clean up syntax, upgrade gulp-uglify and rebuild the minified version 2016-06-20 16:53:45 -04:00
bower.json Use the non-minified version of contain-element in the bower.json main field as minified files aren't allowed 2017-01-13 16:02:20 -05:00
contain-element.js Add a new alternative to the "id" option named "element" that allows the contained element to be passed directly, update the readme to reflect this change, update gulp-concat and re-generate the minified version 2017-01-13 15:47:52 -05:00
contain-element.min.js Add a new alternative to the "id" option named "element" that allows the contained element to be passed directly, update the readme to reflect this change, update gulp-concat and re-generate the minified version 2017-01-13 15:47:52 -05:00
gulpfile.js Clean up syntax, upgrade gulp-uglify and rebuild the minified version 2016-06-20 16:53:45 -04:00
LICENSE Clean up syntax, upgrade gulp-uglify and rebuild the minified version 2016-06-20 16:53:45 -04:00
package.json Add a new alternative to the "id" option named "element" that allows the contained element to be passed directly, update the readme to reflect this change, update gulp-concat and re-generate the minified version 2017-01-13 15:47:52 -05:00
README.md Add a new alternative to the "id" option named "element" that allows the contained element to be passed directly, update the readme to reflect this change, update gulp-concat and re-generate the minified version 2017-01-13 15:47:52 -05:00

Contain Element

A JavaScript plugin to contain an element within its parent element.

Description

The plugin scales an element to the minimum size required for it to be completely contained within its parent, and retains its original aspect ratio by cropping portions that don't fit based on its vertical and horizontal alignment (by default both are set to: center).

Demo

Features

  • Pure JavaScript with no dependencies
  • Configures the horizontal and vertical alignment of the contained element
  • Can detect the native size or have it specified at runtime

Usage

window.onload = function() {
    var contain = new ContainElement({
        element: document.getElementById("element"), // the element to be contained (alternative to 'id')
        id: "element", // the id of the element to be contained (alternative to 'element')
        width: 100, // (optional) native element width in pixels (unset: detected element width)
        height: 100, // (optional) native element height in pixels (unset: detected element height)
        valign: "top", // (optional) vertical alignment: top|bottom (unset: center)
        halign: "left" // (optional) horizontal alignment: left|right (unset: center)
    });

    // (example) update the size and positioning on window resize
    window.onresize = contain.update;

    // (example) update the size and positioning on orientation change
    window.addEventListener('orientationchange', contain.update);
};

Functions

  • update(): Update the size and position of the contained element.
  • setWidth(width): Set the width (run update() to apply).
  • setHeight(height): Set the height (run update() to apply).
  • setValign(valign): Set the vertical alignment (run update() to apply).
  • setHalign(halign): Set the horizontal alignment (run update() to apply).
  • getWidth(): Return the current width.
  • getHeight(): Return the current height.
  • getValign(): Return the current vertical alignment.
  • getHalign(): Return the current horizontal alignment.
  • getScale(): Return the current scale factor being applied to the contained element.

Credits

Written by Williams New York

Authors

LICENSE

Licensed under the MIT license.