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

badgrset

v1.0.2

Published

Responsive background images

Downloads

1

Readme

BadgrSet!

A javascript utility that allows you to leverage the browser parsing of img tag's srcset and sizes attributes to provide responsive image loading for your background images.

Based heavily on Alexander Cleas' method from here https://aclaes.com/responsive-background-images-with-srcset-and-sizes/

Usage

import BadgrSet from 'badgrset'

// One element
new BadgrSet(document.getElementById('myElement');

// Can't be bothered to write my own CSS, do it for me.
BadgrSet.addStyles();

// All elements matching '.badgrset'
BadgrSet.scan()

// A little extra CSS
<style>
    .badgrset {
        height: 0;
        padding-bottom: 56.25%;
    }

// Markup
<div class="badgrset">
    <div class="some-class">Here is some awesome content"</div>
    <img
        class="badgrset-image"
        src="my-original-image.jpg"
        srcset="my-1200w-image.jpg 1200w,
            my-800w-image.jpg 800w,
            my-400w-image.jpg 400w"
        sizes="(min-width: 768px) 80vw,
            (min-width: 992px) 60vw,
            (min-width: 1200px) 50vw,
            100vw"
    >
</div>

Methods

constructor ( HTMLElement element string imgSelector = '.badgrset-image' ) BadgrSet Creates a new instance of BadgrSet

  • element (required): The element on which the background will be applied.
  • imgSelector: The CSS selector to find the image within the element default: .badgrset-image

scan( string|DOMElement containerSelector = 'body', string itemSelector = '.badgrset', string imgSelector = '.badgrset-image' ) BadgrSet Scans a given container for elements matching itemSelector, creating a new BadgrSet for each one found. If string, uses document.queryselector.

  • containerSelector: CSS Selector for the containing element to scan default: body
  • itemSelector: CSS Selector for the elements to have background images attached to default: badgrset
  • imgSelector: The CSS selector to find the image within each element default: .badgrset-image

addStyles ( string itemSelector = '.badgrset', string imgSelector = '.badgrset-image' ) BadgrSet Appends as style element to the page with some basic styling (see notes).

  • itemSelector: CSS Selector for the elements to have background images attached to default: badgrset
  • imgSelector: The CSS selector to find the image within each element default: .badgrset-image

Events

badgrsetupdate Fires when the background-image src changes event.detail contains src, the current src of the background-image.

NB: If using jQuery you may need to look at event.originalEvent.detail

Notes

  • This package is designed to handle the JS side of background images only - you will need to provide styles yourself. At a minimum the '.badgrset-image' element should be hidden with display: none, and you will likely want to define some background-*, height, padding etc. CSS properties in your stylesheets.

  • For convenience you can use the BadgrSet.addStyles function, which will append some basic styles to the head of the page:

    .badgrset {
      background-size: cover;
      background-position: center center;
      background-repeat: no-repeat;
     }
    .badgrset .badgrset-image {
      display: none !important;
    }
  • This package is designed to be used with webpack and babel. The main entry in package json points to the ES6 module in /src. If this causes problems with your build process you can find the transpiled and bundled code in the /dist folder.