A jQuery plugin to fit an object to its container according to a fit parameter that behaves similarly to the CSS background-size keyword syntax.
npm install fitobjectFor simple cover fitting of an object to a container:
You can size and position the object to be totally contained inside the container, as with the CSS property background-size: contain:
;Specify a safeArea parameter to define a region of the object to avoid cropping into when the object fit method is set to 'cover'.
;By default, fitObject will fit your object to its immediate parent, but can fit to ancestors further up the DOM tree by setting the container parameter, assuming any parents between are set to position: static.
Each of the fitObject parameters can be set via HTML data attributes on the object to fit:
Object data attributes will supersede parameters passed into the fitObject method.
| Parameter | Data Attribute | Type | Default | Description |
|---|---|---|---|---|
container |
fit-container |
String, DOM Node, jQuery DOM element | First parent not classed .fit-object-wrapper |
The container to fit the object to |
fit |
object-fit |
String | 'cover' |
The fit method – either 'cover' or 'contain' |
safeArea |
safe-area |
Object | { 'top': 0, 'right': 0, 'bottom': 0, 'left': 0 } |
An area of the object to avoid cropping into. Specify a percentage of the X or Y dimension for each side. |