Skip to content

Instantly share code, notes, and snippets.

@alexsexton
Created October 4, 2019 15:03
Show Gist options
  • Save alexsexton/8b1680cdae36f12e01416bb2bf483cf2 to your computer and use it in GitHub Desktop.
Save alexsexton/8b1680cdae36f12e01416bb2bf483cf2 to your computer and use it in GitHub Desktop.
Swap Image
/* globals jQuery, Modernizr */
'use strict'
jQuery(function ($) {
var productImageHover = function () {
if (!Modernizr.touchevents) {
// Using mouseenter / mouserleave incase of ajaxy stuff
$(document).on('mouseenter mouseleave', '.c-image img', function () {
var swapImg = this.getAttribute('data-swap-src')
var img = this.getAttribute('src')
$(this).attr('src', swapImg)
$(this).attr('data-swap-src', img)
}, function () {
var swapImg = this.getAttribute('data-swap-src')
var img = this.getAttribute('src')
$(this).attr('src', swapImg)
$(this).attr('data-swap-src', img)
})
}
}
// Call on resize
$(window).resize(function () {
productImageHover()
})
// Call on ready
productImageHover()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment