Skip to content

Instantly share code, notes, and snippets.

@s-shin
Created December 27, 2013 05:22
Show Gist options
  • Save s-shin/8142881 to your computer and use it in GitHub Desktop.
Save s-shin/8142881 to your computer and use it in GitHub Desktop.
昔片手間に作った画像入れ替えjQueryプラグイン。偶然発掘したので記録。ただ今はCSSで背景を変えるのが一般的か。
// jquery.swapimg
// (C) 2013 Shintaro Seki
// MIT License
$.fn.swapimg = function(option) {
var op = {
suffix: "_o",
preload: true,
swapped_file: null
};
$.extend(true, op, option);
this.each(function() {
var t = this.src.match(/(.*)\.(\w+)$/);
// get file name
var src = op.swapped_file || t[1] + op.suffix + "." + t[2];
// preload
if (op.preload)
(new Image()).src = src;
// register event
$(this).hover(
function() { this.src = src; },
function() { this.src = t[0]; }
);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment