Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-fit-images

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-fit-images

Adds support to object-fit to images on IE9, IE10, IE11 and Edge. (untested)

  • 0.5.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
41K
decreased by-60.46%
Maintainers
1
Weekly downloads
 
Created
Source

100% untested at the moment. Will test soon

object-fit-images module size module gzipped size

Adds support to object-fit to images on IE9, IE10, IE11 and Edge

This script was made with the main use-case in mind: images on IE/Edge. Main features:

  • The code is light on the CPU
  • No other elements are created to make it work
  • Once set, position is taken care by the browser
  • You can normally get and set the <img>'s src attribute: img.src = 'other-image.jpg'

Comparison table with alternative solutions

Supportobject-fit-images (this)tonipinel/object-fit-polyfilljonathantneal/fitie
BrowsersIE 9-11, Edge"All browsers"IE 8-11, Edge
Tagsimgimgimg, video
cover/contain💚💚💚
fill💚💚💚
none💚💚💔
scale-down💔💔💔
object-position💚💔💔
Performanceobject-fit-images (this)tonipinel/object-fit-polyfilljonathantneal/fitie
Size1.2KB1.8KB1.5KB
Update wait💚 No wait, applied before image load💚 No wait, applied before image load💔 Wait until full image load
Additional DOM elements necessary💚 No💔 Yes, a wrapping element is added💔 Yes, a wrapping element is added
Performance overhead💰💰💰💰💰💰
Technique descriptionTransparent src image; Image in <img>'s backgroundWrapper element with style copied from <img>; CSS+JS positioning; Original <img> hiddenWrapper element with style copied from <img>; JS positioning
Ease of useobject-fit-images (this)tonipinel/object-fit-polyfilljonathantneal/fitie
Object-fit definition💚 Automatic in CSS💔 Via data attribute in HTML (data-object-fit="cover")💔 Via class in HTML (class="cover")
Updates on resize💚 Unnecessary if media queries don't change object-fit💛 Unnecessary if media queries don't change object-fit, impossible otherwise.💔 Yes, manually
Updates on object-fit change💚 Automatic💔 Impossible💔 Impossible
Fix new elements automatically💚 Optional💔 Impossible💛 Manually
<img> src changes💚 Automatically reflected💔 Image not updated at all💔 Fix not updated
Other limitations💔 Any onload events on <img> will fire again when it fixes💚 I didn't find any💔 Some CSS declaration might be broken because partially moved to the wrapper

Runner-up: anselmh/object-fit is deprecated, doesn't support Edge and clocks in at 14KB.

Usage

Fix all the images on the page, present and future.

objectFitImages();

Alternatively, just fix them once. The first parameter can be:

// a selector
objectFitImages('img.some-image');

// an array/NodeList
var someImages = document.querySelectorAll('img.some-image');
objectFitImages(someImages);

// a single element
var oneImage = document.querySelector('img.some-image');
objectFitImages(oneImage);

You can safely disable the resize listener this way:

objectFitImages('img.some-image', {onresize: false});

... if your code DOESN'T contain something like this:

                            img { object-fit: cover }
@media (max-width: 500px) { img { object-fit: contain } }

Load and enable with plain HTML

<script src="ofi.min.js"></script>
<script>objectFitImages();</script>

Load with with browserify

npm install --save object-fit-images
var objectFitImages = require('object-fit-images');
objectFitImages();

API

objectFitImages([images, [opts]])

parameterdescription
imagesType: string (as a selector) or array-like optional
The images to apply the fix on. If it's not supplied, OFI will enter the automatic mode (which means that new images in the DOM will automatically be fixed).
optsType: object optional
Set to {onresize: false} if you don't expect object-fit to vary in a media query.

License

MIT © Federico Brigante

Keywords

FAQs

Package last updated on 11 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc