(function($){ /* smoothScroll マウスイベントによる画像変化を実装します。 =========================================== */ $.fn.imgSwitcher = function (options) { // デフォルト設定 var imgSETTINGS = { normal :[true, "_n."], // 通常時 mouseover :[true, "_o."], // マウスオーバー、マウスアップ active :[true, "_o."], // アクティブ mousedown :[false, "_d."] // マウスダウン } if(options) $.extend(imgSETTINGS, options); $(this).each(function(){ var $this = $(this); var n = this.src; // 1. preload images var obj = new Object(); if(!obj[n]){ for (var k in imgSETTINGS) { imgSETTINGS[k]; if(imgSETTINGS[k][0]){ var x = n.replace("_n.", imgSETTINGS[k][1]); obj[imgSETTINGS[k]] = new Image(); obj[imgSETTINGS[k]].src = x; } } } // 2. set event handler var notChange = "x_stay"; if(!($this.hasClass('x_escape') && this.src.lastIndexOf(imgSETTINGS.active) > -1)){ $this.on({ mouseover: function(){ if(imgSETTINGS.mouseover[0]&&!$this.hasClass(notChange)) this.src = obj[imgSETTINGS.mouseover].src; }, mouseout: function(){ if(imgSETTINGS.normal[0]&&!$this.hasClass(notChange)) this.src = obj[imgSETTINGS.normal].src; }, mousedown: function(){ if(imgSETTINGS.mousedown[0]&&!$this.hasClass(notChange)) this.src = obj[imgSETTINGS.mousedown].src; }, mouseup: function(){ if(imgSETTINGS.mouseover[0]&&!$this.hasClass(notChange)) this.src = obj[imgSETTINGS.mouseover].src; } }); } // 3. judge active page if(typeof activePageName != "undefined" && activePageName != "") { if(n.match(activePageName) != null){ $(this).attr("src", obj[imgSETTINGS.active].src).addClass(notChange); } } }); }; })(jQuery);