Created
December 27, 2013 05:22
-
-
Save s-shin/8142881 to your computer and use it in GitHub Desktop.
昔片手間に作った画像入れ替えjQueryプラグイン。偶然発掘したので記録。ただ今はCSSで背景を変えるのが一般的か。
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
// 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