Lightweight plugin for easy responsive images replacement

Overview

Responsive image replacement

Check out the example here.

resonsive-img.js is a lightweight plugin for fast, clean and easy responsive image replacement using to the viewport your visitor's browser. This way you can reduce the payload for your users by only letting them download the appropriate images for their device.

Have a look at the blog post here for a more in-depth description.

Usage

####Setting it up

To start using the plugin we only have to include the script in the footer of our document.

<script src='responsive-img.min.js'></script>

####HTML Structure

In the data-src attribute we specify the different breakpoints in combination with the image source it should use. In the data-src-base(optional) attribute we specify the base of our source urls.

<img alt='kitten!' data-src-base='demo/images/' data-src='<480:smallest.jpg,
													 	  <768:small.jpg,
													 	  <960:medium.jpg,
													 	  >960:big.jpg' />

Using the HTML above the browser would load demo/images/smallest.jpg if the size of the viewport is below 480 pixels, demo/images/small.jpg if the size of the viewport is above 480 pixels and below 768 pixels, demo/images/medium.jpg if the size of viewport above 768 pixels and below 960 pixels and demo/images/big.jpg if the size of viewport above 960 pixels.

Let's also create a fallback for non-javascript browsers:

<noscript><img alt='kitten!' src='demo/images/medium.jpg' /></noscript>

That's all! We're up and running! ;)

Options

The only required option is the data-src attribute. All of the options below can be used additionally.

Name Type Description
data-src-base attribute Adds a specified path before every image source path
data-src2x attribute Can be used instead of data-src if devicePixelRatio is above 1.2 (retina displays). Syntax is the same as data-src.
data-src-base2x attribute Can be used instead of data-src-base to specify a custom base path for devices were devicePixelRatio is above 1.2 (retina displays). Can be used in combination with both data-src and data-src-base or either one

Browser support

There are no known unsupported browsers. The plugin has currently been tested in the following browsers:

####Desktop

Browser Status
IE 11 Desktop Works!
IE 11 Release Preview Works!
IE 10 Works!
IE 9 Works!
IE 8 Works!
IE 7 Works!
IE 6 Works!
IE 5 (IE 10 desktop documentmode) Works!
Opera 10.6 Works!
Opera 12.16 Works!
Opera 15.0 Works!
Opera 18.0 dev Works!
Safari 4.0 Works!
Safari 5.0 Works!
Safari 5.1 Works!
Firefox 3 Works!
Firefox 12 Works!
Firefox 24 Works!
Firefox 26 aurora Works!
Chome 14 Works!
Chome 19 Works!
Chome 30 Works!
Chome 31 dev Works!

####Mobile

OS Status
Windows Phone 8.0 (IE 10 desktop documentmode) Works!
Windows Phone 7.0 (IE 10 desktop documentmode) Works!
Android 1.5 Works!
Android 1.6 Works!
Android 2.2 Works!
Android 2.3 Works!
Android 4.0 Works!
Android 4.0 (tablet) Works!
Android 4.1 Works!
Opera Mobile Works!
iOS 3.0 Works!
iOS 3.2 (tablet) Works!
iOS 4.0 Works!
iOS 4.3.2 (tablet) Works!
iOS 5.1 Works!
iOS 6.0 Works!
iOS 7.0 Works!

####Notes

######iOS Without meta width=device-width; some mobile devices will apply a standard width to it's viewport of 960px and return this to the javascript. This might cause the script to load an image which is to big for your visitors screen. Make sure to set a meta tag with width=device-width; in your website's head.

Comments

Let me know if you have idea’s or notice something, I would love to hear your feedback!

Comments
  • Question: using responsive-images.js to gallery

    Question: using responsive-images.js to gallery

    opened by pawelmil 8
  • using a debounced version of makeImageResponsive upon resize event

    using a debounced version of makeImageResponsive upon resize event

    Hi @kvendrik I love the way of how this plugin works, I think it would be nice and a performance boost using a debounced version of makeImageReponsive when browser resizes, like this one http://own.li/BAHt, what do you think?

    enhancement 
    opened by git-pwn 8
  • Images added server side (after page load) don't load until browser window size is changed

    Images added server side (after page load) don't load until browser window size is changed

    Is there a way to fire the function makeImagesResponsive after responsive images are loaded onto the page via server side? I'm hoping to fire the makeImagesResponsive function onclick with a possible delay after the images are triggered to load via server side.

    question 
    opened by justinbhayes 5
  • Disabling JavaScript in Chrome doesn't trigger <noscript>

    Disabling JavaScript in Chrome doesn't trigger

    If you know how to test for the <noscript> path, please add a note to the Readme. Disabling JS in Chrome does prevent script execution. Meanwhile, the <noscript> tag is ignored, it seems.

    What's your setup to test this?

    question 
    opened by DivineDominion 4
  • Add ability to process subset of images

    Add ability to process subset of images

    Right now, makeImagesResponsive processes every single image in the document, which is inefficient when you have a lot of images on the page and you only want to process one or a few of them.

    My proposal is to add a parameter to makeImagesResponsive that allows the program to specify which images should be processed. It should default to processing all of them as it does now.

    I think it would make sense to allow a querySelector string, which would basically be an argument to document.querySelector or its equivalent, or an array of DOM elements for each image you want to transform.

    makeImagesResponsive('#image');
    makeImagesResponsive('.image');
    makeImagesResponsive([document.getElementById('image')]);
    

    Here is a link to the relevant polyfills required to support querySelector and querySelectorAll

    https://github.com/inexorabletash/polyfill/blob/master/polyfill.js#L671

    enhancement 
    opened by adambom 3
  • External images?

    External images?

    Hello,

    Should your cool responsive image JavaScript support external images?

    I tried using it for some Google maps static API images, like:

    <img alt="Static Map of Antigua" data-src="<767:https://maps.googleapis.com/maps/api/staticmap?center=17.175854%2c-61.850532&zoom=11&size=720x720,
    <979:https://maps.googleapis.com/maps/api/staticmap?center=17.175854%2c-61.850532&zoom=11&size=290x290,
    <1200:https://maps.googleapis.com/maps/api/staticmap?center=17.175854%2c-61.850532&zoom=11&size=380x380,
    >1200:https://maps.googleapis.com/maps/api/staticmap?center=17.175854%2c-61.850532&zoom=11&size=470x470" />
    

    However; the src attribute gets set to just "https", would be awesome if it did support external images... or if I'm just using it wrong you could let me know how to use it correctly :)

    Kind regards,

    opened by David-Zolv 2
  • Use width of <img> parent, rather than viewport

    Use width of parent, rather than viewport

    Firstly, awesome plugin, will be adding it to my framework.

    I was wondering if it would be possible to use the width of the images parent container rather than the width of the viewport?

    I ask this because, like a lot of people, I use a grid system, and more often than not the image on a mobile (one column parent) is wider than that being served to desktops (ie 1 out of 4 columns, approx 23% of the viewport, about 260px @1140px wide)

    Would be great as an option, or a forked repo

    TY Paul

    enhancement 
    opened by Paul-Browne 2
  • Issue when referencing full URL

    Issue when referencing full URL

    I believe I've found a (potential) bug when referencing a full URL such as http://example.com/images/large.jpg. Seemingly whenever a full URL including a colon is used in the 'data-src', it stops the dynamic generation of the 'src' attribute before the colon.

    Eg: If I have data-src="<320:http://example.com/images/small.jpg" it will generate src="http" rather than src="http://example.com/images/small.jpg"

    I believe this to just be an issue with the way it parses colons, but it's late and I won't have time to look at this until tomorrow.

    opened by andrewicarlson 2
  • Does the plugin support background images, where the path to image is via the CSS stylesheet?

    Does the plugin support background images, where the path to image is via the CSS stylesheet?

    Not sure if this is the right place for this question, but am curious. Overall the plugin looks great and background image support would be wonderful :)

    question 
    opened by imohkay 2
  • IE and FF has horizontal scrollbar

    IE and FF has horizontal scrollbar

    When testing the demo here in a web browser, At smaller resolution, IE and FireFox have a horizontal scrollbar due to the image not fitting the viewport. Chrome looks great!

    enhancement wontfix 
    opened by emage 1
  • License

    License

    Hi there,

    I would love to use this for a commercial product. Would you be able to add a license file to this repo? And would it be ok to use this for commercial use?

    Thanks in advance and great stuff mate! Sam

    question 
    opened by samwalshnz 1
  • Height?

    Height?

    Love this JS, been looking for something that works across all browsers. However, I have a question. Does this work on device height as well? For example, using <980 and min-height.

    Thanks.

    opened by openbayou 0
  • Feature/targeted

    Feature/targeted

    Now you can provide targets to makeImagesResponsive. Will accept:

    • A valid document.querySelector string
    • A valid HTMLImageElement
    • An array of HTMLImageElements
    • No argument passed defaults to all images in the DOM

    Must still define a data-src or data-src2x attribute or else targets will be passed.

    opened by adambom 0
  • Throttle makeImagesResponsive for window resize event to twice per second. FIxes #4

    Throttle makeImagesResponsive for window resize event to twice per second. FIxes #4

    Did a couple things. Wrap the entire program in a closure to allow us to safely create local variables. Also introduced a local throttle function, which will execute the given function at most every wait ms (see http://underscorejs.org/docs/underscore.html#section-71).

    When attached to the window's resize event, we instruct makeImagesResponsive to execute at most every 500ms, or twice per second.

    opened by adambom 0
Owner
Koen Vendrik
Staff Front End Developer @Shopify
Koen Vendrik
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A beautiful, responsive, highly customizable and accessible replacement for JavaScript's popup boxes. Zero dependencies.Alerts ,dialogs

AsgarAlert (v1) for JS Install <script defer src="/asgar-alert.js"></script> Examples The most basic message: asgar("Hello world!"); A message signali

Asgar Aliyev 5 Dec 20, 2022
Easy responsive tabs - is a lightweight jQuery plugin which optimizes normal horizontal or vertical tabs to accordion on multi devices

Easy responsive tabs - is a lightweight jQuery plugin which optimizes normal horizontal or vertical tabs to accordion on multi devices like: web, tablets, Mobile (IPad & IPhone). This plugin adapts the screen size and changes its action accordingly.

Samson Onna 600 Dec 8, 2022
Responsive Tabs is a jQuery plugin that provides responsive tab functionality.

Responsive Tabs is a jQuery plugin that provides responsive tab functionality. The tabs transform to an accordion when it reaches a CSS breakpoint. You can use this plugin as a solution for displaying tabs elegantly on desktop, tablet and mobile.

Jelle Kralt 537 Dec 8, 2022
A jquery plugin that makes images truly responsive, without sacrificing anyone's face. Give it more stars!

Responsify.js A jquery plugin that makes images truly responsive, without sacrificing anyone's face :D When images are used in a responsive container

Wenting Zhang 1.3k Dec 14, 2022
Simple jQuery plugin that will allow users to zoom in your images, perfect for product images and galleries.

Image Zoom (jQuery) Plugin Simple jQuery plugin that will allow users to zoom in your images, perfect for product images and galleries that is less th

Mario Duarte 8 Aug 3, 2022
A Cypress plugin that generates test scripts from your interactions, a replacement Cypress Studio for Cypress v10 🖱 ⌨

DeploySentinel Cypress Recorder Plugin Create Cypress tests scripts within the Cypress test browser by simply interacting with your application, simil

DeploySentinel 13 Dec 15, 2022
Simple, responsive and lightweight Masonry Grid jQuery Plugin.

jquery-masonry-grid Simple, responsive and lightweight Masonry Grid jQuery Plugin. Installation Add the script before closing the <body> tag (make sur

Peter Breitzler 8 Jun 9, 2022
An easy-to-use jQuery plugin that allows the user to pick an icon from a responsive icon browser and shows the corresponding icon class in an input element.

Font Awesome Browser An easy-to-use jQuery plugin that allows the user to pick an icon from a responsive icon browser and shows the corresponding icon

Gianluca Chiarani 1 May 1, 2021
High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.

lazysizes lazysizes is a fast (jank-free), SEO-friendly and self-initializing lazyloader for images (including responsive images picture/srcset), ifra

Alexander Farkas 16.6k Jan 1, 2023
Responsive no-jQuery pure JS/CSS Lightbox for images, no dependencies, 10kb unminified source code, with demo

img-lightbox Responsive no-jQuery pure JS/CSS Lightbox for images, no dependencies, 10kb unminified source code, with demo Demo codepen jsfiddle jsbin

englishextra 12 Jun 13, 2022
Compare James Webb Space Telescope images to older images.

How much more powerful is the James Webb Space Telescope when compared to Hubble? Find out! More info Want to help out? CONTRIBUTING.md Blog post with

John Christensen 399 Jan 3, 2023
Easiest 1-click way to install and use Stable Diffusion on your own computer. Provides a browser UI for generating images from text prompts and images. Just enter your text prompt, and see the generated image.

Stable Diffusion UI Easiest way to install and use Stable Diffusion on your own computer. No dependencies or technical knowledge required. 1-click ins

null 3.5k Dec 30, 2022
MidJourney is an AI text-to-image service that generates images based on textual prompts. It is often used to create artistic or imaginative images, and is available on Discord and through a web interface here.

Midjourney MidJourney is an AI text-to-image service that generates images based on textual prompts. It is often used to create artistic or imaginativ

Andrew Tsegaye 8 May 1, 2023
Automatic arxiv->ar5iv link replacement in Chrome.

Automatic arxiv->ar5iv link replacement in Chrome. This chrome extension will automatically replace arxiv.org/pdf/* links with ar5iv links for more we

yobi byte 44 Oct 29, 2022
Replacement for comma.ai backend and useradmin dashboard

Replacement for comma.ai backend and useradmin dashboard. Bundled with a modified version of comma's cabana to allow viewing & analyzing drives.

null 15 Jan 1, 2023
A Hackable Markdown Note Application for Programmers. Version control, AI completion, mind map, documents encryption, code snippet running, integrated terminal, chart embedding, HTML applets, plug-in, and macro replacement.

Yank Note A hackable markdown note application for programmers Download | Try it Online >>> Not ecommended English | 中文说明 [toc]{level: [2]} Highlights

洋子 4.3k Dec 31, 2022