Git Product home page Git Product logo

imagefitcover's Introduction

imageFitCover

This simple jQuery plugin lets you scale images to fit within their container element, as specified for the new CSS object-fit: cover property. It should work in all modern browsers that do not yet support object-fit natively and is compatible with jQuery 1.7+ and 2.*. This has the same effect as using background-size: cover with background images, but lets you use img tags and the new picture module instead.

A simple demo shows you the desired effect

All images within the targeted container object will fill its entire area without changing their aspect ratio. The replaced content is sized to maintain its aspect ratio while filling the element’s entire content box: its concrete object size is resolved as a cover constraint against the element’s used width and height.

As of November 2014 the latest Webkit browsers and Firefox 36 support object-fit. However, Internet Explorer, Firefox before version 36 and older versions of Safari prior to version 7.1 do not.

This plugin only implements object-fit: cover property for img tags wrapped in the targeted containers.

Other plugins, most notably Simon Schmid's jquery.object-fit and Anselm Hannemann's vanilla javascript polyfill, provide more complete support for object-fit and implement other values such as fill, contain and scaled-down and work directly on any HTML object that should respond to object-fit rather than only on elements containing image tags.

Our approach may be less flexible, but is more efficient for situations where you need to fit images within an area within a different aspect ratio. As a jQuery plugin it can be combined with numerous other plugins for slide shows and other effects. Inline styles are added only to browsers that do not support object-fit via a simple test on initial page load.

v0.2 Release Note

The plugin will now only check for support once and adds two body classes object-fit-checked and, if support is detected, object-fit-supported. This works much better with multiple images or when loading content asynchronously.

Sample usage:

HTML Snippet:

<figure class="image-cover">
	<img src="/path-to-images-folder/image.jpg" width="800" height="600" />
</figure>

Stylesheet:

figure.image-cover {
  position: relative;
  width: 50%;
  height: 20vh; /* viewport height, not supported in IE8*/
  overflow: hidden; /* important */
}

figure.image-cover img { position: relative; /must have a non-static position to work in browsers that do not support object-fit/ width: 100%; /* initial value overriden by object-fit and/or plugin / height: 100%; / initial value overriden by object-fit and/or plugin / object-fit: cover; / works in latest Webkit browsers + FF 36+ */ }

Javascript in jQuery context:

$('figure.image-fit').imageFitCover();

Image Loading

The plugin does not wait for images to load fully via an onload event handler, as it can work height and width attributes alone if added to your <img /> tags. In the absence of height and width attributes, the plugin will use naturalWidth or naturalHeight with recent browses and width and height alone with legacy IE. For best performance, I would recommend adding height and width attributes that match correct natural pixel size and aspect ratio. The plugin does not override rendered image sizes in pixels but merely sets one axis to 100% and the other to auto. It needs the correct image aspect ratio to calculate top or left margin offsets. Only top or left margins are in set in pixels.

Delay and Picture Module

When integrated with other Javascript libraries such as picturefills.js it may be necessary to add an extra delay. Just specify milliseconds as the first parameter to wait for the other script to complete. Alternatively, if the other script provides a callback function, call imageFitCover() on completion.

$('picture').imageFitCover(200); /* will apply offsets after 0.2 seconds */

imagefitcover's People

Contributors

2dareis2do avatar neilg63 avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.