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

jquery-backgroundcover

v1.1.1

Published

jQuery plugin to make CSS3 'background-size: cover' even smarter.

Downloads

4

Readme

jquery.backgroundcover

This jQuery plugin is a smarter version of the CSS3 feature "background-size: cover", to cover the area of a HTML element with a background image. It adds two features:

  • Safe Area: it is possible to define a safe area of the image, that will never be cropped. See below for further information.
  • IE8 compatibility (or any other browser that does not support background-size): instead of using a CSS background image, an img element is added and positioned behind.

Safe Area

A sometimes problematic issue of the default CSS behaviour of "background-size: cover" is that it may cut of important parts of the image to fill up the space. Especially when the aspect ratio is very different (say an image in a landscape format viewed on a smartphone in portrait mode), substantial amounts of the image will be cropped. Not that good, if that happens to be the important, story telling parts of the image (maybe a face).

The solution is to tell the plugin about a so called safe area, that may not be cropped. The background image will then be scaled as much as the safe area allows to cover its element. That might however leed to the situation, that the image cannot be scaled as much to wholy cover its element. It works the better, the smaller the safe area is. If you define the whole image as the safe area, it will actually behave like "background-size: contain".

Have a look at the demo to see the effect!

Installation

Just include the script:

<script src="/path/to/jquery.backgroundcover.min.js"></script>

Usage

Fire up the plugin:

$("#bg").backgroundcover(options);

It will set the CSS properties background-image, background-size and background-position and update them whenever the window is resized. It will interfere with your document as little as possible, so it is up to you to set other CSS properties as required, for example "background-repeat: no-repeat". When you set up your CSS like the following, you will get a CSS only fallback in case JavaScript is not available and thus the plugin is not used (graceful degradation):

background-image: url(/path/to/image.jpg);
background-position: center center;
background-size: cover;
background-repeat: no-repeat;

Call methods on existing plugin:

$("#bg").data("backgroundcover").setSafearea("100,200,300,400");
$("#bg").data("backgroundcover").setImage(url);

Invoking the plugin on an element with existing plugin will just update its options. So the following code is equivalent to the above:

$("#bg").backgroundcover({
    image: url,
    safearea: "100,200,300,400"
});

Configuration

The following configuration options are available. They may be provided either with the options object when initializing the plugin, or as HTML5 data-* attributes on the HTML element. The following examples are equivalent.

Options on plugin initialization:

<div id="bg"></div>
<script type="text/javascript">
$("#bg").backgroundcover({
    safearea: "10%,30%,50%,80%"
});
</script>

Options taken form data-* attributes:

<div id="bg" data-safearea="10%,30%,50%,80%"></div>
<script type="text/javascript">
$("#bg").backgroundcover();
</script>

image

{
    image: "/path/to/image.jpg"
}

The URL to the image to use as the background. If none is provided, the plugin looks for the background-image CSS property and will take that image if available.

saferea

{
    safearea:"20%,10%,70%,80%"
}

The coordinates of the safe area (see above), as a comma seperated list of left, top, right and bottom coordinates, or in other words x1,y1,x2,y2, where (x1,y1) is the upper left point of the bounding box, and (x2,y2) the lower right. The values may be either percentages (relative to the image's width and height), or absolute pixel values (just numbers without unit, e.g. "50,100,380,495").

resizeInterval

{
    resizeInterval:100
}

Change the default interval setting of 250ms for how often to poll for changes of the elements width and height.

Authors

Simon Ihmig @ kaliber5