Norvell's Public Machinations
Miss any of our Open RFC calls?Watch the recordings here! »


0.3.3 • Public • Published

Hover Image

npm version

A generic library to swap out an image on hover.

Photos by Daniel Brubaker on Unsplash


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


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.

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">

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

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

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

let destroyHoverListeners = initializeHoverImage();

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=""></script>
var destroyHoverListeners = window.hoverImage({
    // Options here...



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'.


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'.


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'.


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

Defaults to 'is-hovered'.


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




Matt Wade


npm i @designory/hover-image

DownloadsWeekly Downloads






Unpacked Size

51.8 kB

Total Files


Last publish


  • avatar
  • avatar