All Projects → constancecchen → Object Fit Polyfill

constancecchen / Object Fit Polyfill

Licence: isc
A Javascript polyfill for browsers that don't support the object-fit CSS property.

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Object Fit Polyfill

v8go-polyfills
Add polyfills to rogchap/v8go
Stars: ✭ 25 (-94.93%)
Mutual labels:  polyfill
Polyfill Php70
This component provides features unavailable in releases prior to PHP 7.0.
Stars: ✭ 3,270 (+563.29%)
Mutual labels:  polyfill
Avif.js
AVIF polyfill for the browser
Stars: ✭ 399 (-19.07%)
Mutual labels:  polyfill
Abortcontroller Polyfill
Polyfill for the AbortController DOM API and abortable fetch (stub that calls catch, doesn't actually abort request).
Stars: ✭ 273 (-44.62%)
Mutual labels:  polyfill
Ie8
some damn DOM fix for this damned browser
Stars: ✭ 297 (-39.76%)
Mutual labels:  polyfill
Css3 Mediaqueries Js
CSS3 Media Queries Shim
Stars: ✭ 333 (-32.45%)
Mutual labels:  polyfill
Gapotchenko.FX
.NET polyfill to the future. A versatile RAD framework for .NET platform.
Stars: ✭ 23 (-95.33%)
Mutual labels:  polyfill
React Lifecycles Compat
Backwards compatibility polyfill for React class components
Stars: ✭ 457 (-7.3%)
Mutual labels:  polyfill
Standardized Audio Context
A cross-browser wrapper for the Web Audio API which aims to closely follow the standard.
Stars: ✭ 300 (-39.15%)
Mutual labels:  polyfill
Polyfill Ctype
This component provides a partial, native PHP implementation for the Ctype extension.
Stars: ✭ 3,774 (+665.52%)
Mutual labels:  polyfill
Bootstrap Ie8
Bootstrap 4 for IE8 and IE9
Stars: ✭ 278 (-43.61%)
Mutual labels:  polyfill
Url Polyfill
Polyfill URL and URLSearchParams to match last ES7 specifications
Stars: ✭ 294 (-40.37%)
Mutual labels:  polyfill
Loading Attribute Polyfill
Fast and lightweight dependency-free vanilla JavaScript polyfill for native lazy loading / the awesome loading='lazy'-attribute.
Stars: ✭ 335 (-32.05%)
Mutual labels:  polyfill
React Native Drawer Layout
A platform-agnostic drawer layout for react-native
Stars: ✭ 258 (-47.67%)
Mutual labels:  polyfill
Polyfill Library
NodeJS module to create polyfill bundles tailored to individual user-agents.
Stars: ✭ 404 (-18.05%)
Mutual labels:  polyfill
rangefix
Workaround for browser bugs in Range.prototype.getClientRects and Range.prototype.getBoundingClientRect.
Stars: ✭ 35 (-92.9%)
Mutual labels:  polyfill
Promise Fun
Promise packages, patterns, chat, and tutorials
Stars: ✭ 3,779 (+666.53%)
Mutual labels:  polyfill
Sugar
A Javascript library for working with native objects.
Stars: ✭ 4,457 (+804.06%)
Mutual labels:  polyfill
Webappsec Trusted Types
A browser API to prevent DOM-Based Cross Site Scripting in modern web applications.
Stars: ✭ 424 (-14%)
Mutual labels:  polyfill
Fakeindexeddb
A pure JS in-memory implementation of the IndexedDB API
Stars: ✭ 373 (-24.34%)
Mutual labels:  polyfill

object-fit-polyfill

A polyfill for browsers that don't support the object-fit CSS property. Unsure of what the object-fit does? Essentially object-fit is to <img> tags what background-size is to background-image. You can check out the MDN page for more details.

Features

  • Works with img, picture, srcset, video, and canvas
  • Supports object-position
  • Supports IE 9+, Edge 18-, iOS 7-, and Android 4.4-
  • Lightweight
    • 3KB (2KB with the basic version)
    • No dependencies: vanilla Javascript (works with or without jQuery)
  • Flexible usage

Requirements

  • This plugin requires setting data attributes on elements that you want polyfilled (data-object-fit).
  • This plugin makes the assumption that the parent container is acting as a picture frame - it must have a height & width set.

Demo

You can check out the bare-bones demo here. Note that the plugin simply won't do anything if you're on a browser that already supports object-fit, so you'll want to test it on IE or older iOS/Android browsers.

How does it work?

Unlike object-fit-images or Primož Cigler's method (both excellent alternatives if you'd rather not use this one), this polyfill does not set a background image on the parent container, but instead resizes and repositions the image (using inline CSS for height, width, absolute positioning, and negative margins).

The polyfilled item will receive the class object-fit-polyfill if styling issues occur that require overrides.

Why bother?

If you're wondering: why bother using <img> tags versus background-image? Here's a couple reasons:

  1. <img> tags have better SEO/crawling visibility.
  2. In cases where images are dynamically returned and can't simply be added to your stylesheets (e.g., CMS's), you're forced to inline your background-image. This solves that somewhat-ugly-looking inline CSS.
  3. background-image doesn't work with picture, video, or canvas elements.

Of course, there's still plenty of cases where using a background image makes more sense than a regular image.

Usage

Initialization:

<!-- Minimum CSS -->
<style>
  .container {
    width: 25em; /* Or whatever you want it to be */
    height: 25em; /* Or whatever you want it to be */
  }
  .media {
    width: 100%;
    height: 100%;
    object-fit: cover; /* Or whatever object-fit you want */
  }
</style>

<!-- Minimum HTML -->
<div class="container">
  <img
    alt=""
    src="https://unsplash.it/800/600/"
    class="media"
    data-object-fit="cover"
  />
</div>

<script src="dist/objectFitPolyfill.min.js"></script>

Customized object-fit/object-position:

<div class="container">
  <img
    alt=""
    src="https://unsplash.it/800/600/"
    class="media"
    data-object-fit="contain"
    data-object-position="top left"
  />
</div>

<div class="container">
  <img
    alt=""
    src="https://unsplash.it/800/600/"
    class="media"
    data-object-fit="none"
    data-object-position="25% 75%"
  />
</div>

<div class="container">
  <img
    alt=""
    src="https://unsplash.it/800/600/"
    class="media"
    data-object-fit="scale-down"
    data-object-position="3em -1em"
  />
</div>

If you're only interested in using the basic polyfill (which assumes object-fit: cover and object-position: 50% 50%), you can save yourself some bytes by using:

<div class="container">
  <img
    alt=""
    src="https://unsplash.it/800/600/"
    class="media"
    data-object-fit
  />
</div>

<script src="dist/objectFitPolyfill.basic.min.js"></script>

Advanced usage

If you need to dynamically call the polyfill on the fly for any reason (for example, carousels or lazy-loaded images), you can do so quite easily:

// Rerun the polyfill on all elements with the data attribute
objectFitPolyfill();

// Rerun the polyfill on a single DOM node
var element = document.querySelector('.foo');
objectFitPolyfill(element);

// Rerun the polyfill on multiple elements
var elements = document.querySelectorAll('.bar');
objectFitPolyfill(elements);

// Rerun the polyfill with a jQuery selector
objectFitPolyfill($('.baz'));

Installation via package managers

If you prefer not to manually add Javascript files to your sites, you can use bower and npm like so:

npm install objectFitPolyfill
yarn add objectFitPolyfill
# Or:
bower install objectFitPolyfill

Usage within a modern ES6/webpack JS project

import 'objectFitPolyfill';
// Or:
require('objectFitPolyfill');

window.objectFitPolyfill();

Note that in SPA's, you must manually call window.objectFitPolyfill() after component mount / once you're sure your media is loaded in & available. See this example React usage.

Requests?

If you'd like to make feature requests such as IE 8- or adding object-position support for Safari, feel free to open an issue or pull request! It's doable and on my radar, but I probably won't get to it without some prodding.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].