Created
October 4, 2019 15:03
-
-
Save alexsexton/8b1680cdae36f12e01416bb2bf483cf2 to your computer and use it in GitHub Desktop.
Swap Image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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