npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

lazyload-picturefill-background

v1.0.1

Published

Responsive background images using Window.matchMedia() and the Intersection Observer API

Downloads

298

Readme

Lazyload Picturefill Background

Responsive background images using Window.matchMedia() and the Intersection Observer API.

This project is inspired by M6Web/picturefill-background

Demo gif

Getting Started

Install

With npm:

npm install lazyload-picturefill-background

With yarn:

yarn add lazyload-picturefill-background

With CDN:

<script src="https://unpkg.com/[email protected]/dist/lazyload-picturefill-background.min.js"></script>

You can as well clone this repository.

JS

Using ES6 and import

import lazyloadPicturefillBackground from "lazyload-picturefill-background";

new lazyloadPicturefillBackground();

If you are not using babel you can use dist/lazyload-picturefill-background.min.js

<script src="lazyload-picturefill-background.min.js"></script>
<script>
  new lazyloadPicturefillBackground();
</script>

HTML

The .is-lazy selector is optional. Use it if you want to lazyload our .picturefill-background elements.

<div class="picturefill-background is-lazy">
  <span class="picturefill-background-source" data-src="small.jpg"></span>
  <span
    class="picturefill-background-source"
    data-src="medium.jpg"
    data-media="(min-width: 400px)"
  ></span>
  <span
    class="picturefill-background-source"
    data-src="large.jpg"
    data-media="(min-width: 640px)"
  ></span>
  <span
    class="picturefill-background-source"
    data-src="big.jpg"
    data-media="(min-width: 800px)"
  ></span>
</div>

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:

.picturefill-background {
  background-size: cover;
  background-repeat: no-repeat;
}

.picturefill-background.is-lazy {
  background-color: lime;
}

Change default selectors

import lazyloadPicturefillBackground from "lazyload-picturefill-background";

new lazyloadPicturefillBackground({
  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:

<script src="https://polyfill.io/v3/polyfill.min.js?features=IntersectionObserver"></script>

License

This project is licensed under the MIT License - see the LICENSE file for details