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

@romellem/hover-image

v0.4.0

Published

A generic library to swap out an image on hover.

Downloads

4

Readme

Hover Image

npm version

A generic library to swap out an image on hover.

Photos by Daniel Brubaker on Unsplash

Install

yarn add @romellem/hover-image
# or npm install @romellem/hover-image

Usage

Place your configured hoverSrcAttribute (which defaults to data-hover-src) on an <img> tag.

<img src="original.jpg" data-hover-src="hover.jpg">

If you place the attribute on a tag that isn't an <img>, then we search for the first child <img> tag to have its source swapped.

<a href="#" data-hover-src="hover.png">
    <img src="original.png">
    When you hover over this link, the icon will change.
</a>

The child image is found using querySelector, which uses a "depth-first pre-order traversal" of the child nodes. If you have many child image nodes, and you don't want the first one, you can also put an additional data attribute to specify a selector.

This data attribute is configured by the hoverImageSelectorAttribute option, and defaults to 'data-image-selector'.

<a href="#" data-hover-src="hover.png" data-image-selector=".will-be-changed">
    <img src="remains-unchanged.png">
    When you hover over this link, the icon on the left will remain the same, the icon to the <em>right</em> instead will change.
    <img src="original.png" class="will-be-changed">
</a>

Once your HTML is configured, you need to initialize the mouse event listeners:

import initializeHoverImage from '@romellem/hover-image';
// Or, if non-transpiled:
// const initializeHoverImage = require('@romellem/hover-image');

initializeHoverImage({
    hoverSrcAttribute,
    originalSrcAttribute,
    hoverImageSelectorAttribute,
    classToggle,
    preloadHoverImages,
});

The initialize function returns a destroy function if you need to remove the event listeners that get added:

let destroyHoverListeners = initializeHoverImage();

destroyHoverListeners();

Additionally, hover-image is published as a UMD module and so can be used directly in a browser context. When loading the UMD module, it is exposed under window.hoverImage.

<script src="https://unpkg.com/@romellem/hover-image/lib/umd/hover-image.min.js"></script>
<script>
var destroyHoverListeners = window.hoverImage({
    // Options here...
});
</script>

Options

hoverSrcAttribute

The data attribute the library will attach its delegated events to.

This can be placed on any element, not just on an <img> tag. If this is placed on a non-image tag, then we look for the first child <img> of that element. If you need to target a child element other than the first matching img, then see the hoverImageSelectorAttribute option for more information on how to pass a custom selector.

Defaults to 'data-hover-src'.

originalSrcAttribute

The name of the data attribute the library will save the original source URL while the image is swapped out.

Defaults to 'data-original-src'.

hoverImageSelectorAttribute

When the hoverSrcAttribute is placed on a non-image element, this optional attribute allows for a selector to be passed for the child image that'll be swapped out. When this attribute is not present, the selector it uses is 'img'.

Defaults to 'data-image-selector'.

classToggle

The class that will get toggled while the image is swapped out on hover.

Defaults to 'is-hovered'.

preloadHoverImages

When true, will make a network request for all images specified within the hoverSrcAttribute before the initial mouseover event has fired. If the browser is caching network requests, this should help eliminate the slight empty flash the user sees when hovering over the image for the first time.

Defaults to true.

Browser Support

  • Internet Explorer (IE) 10
  • All modern browsers

License

MIT

Author

Matt Wade