Lazyload Picturefill Background
Responsive background images using Window.matchMedia() and the Intersection Observer API.
This project is inspired by M6Web/picturefill-background
Getting Started
Install
With npm:
npm install lazyload-picturefill-background
With yarn:
yarn add lazyload-picturefill-background
With CDN:
You can as well clone this repository.
JS
Using ES6 and import
; ;
If you are not using babel you can use dist/lazyload-picturefill-background.min.js
HTML
The .is-lazy
selector is optional. Use it if you want to lazyload our .picturefill-background
elements.
By default:
.picturefill-background
: apply the background-image attribute.is-lazy
: Set observer to lazyload the image (Optional).picturefill-background-source
:[data-src]
: specify the image path[data-media]
: apply in specific media settings (Optional)
CSS
Set your CSS as you need:
Change default selectors
; pictureFillBackgroundSelector: ".picturefill-background" //String or node list lazySelector: ".is-lazy" //String pictureFillBackgroundSourceSelector: ".picturefill-background-source" //String;
Browser support
Lazyload Picturefill Background will work in all modern browsers. the Intersection Observer API is not supported on IE you can use the w3c polyfill.
The IntersectionObserver polyfill can be included in your project using polyfill.io, which will automatically include dependencies where necessary:
License
This project is licensed under the MIT License - see the LICENSE file for details