2012-08-12 111 views
0

我有一個圖像,我正在使用圖像映射。當根據地圖懸停在圖像的一部分上時,我希望圖像更改,但我不確定如何使用jQuery執行此操作。這裏是我試圖代碼:使用jQuery和圖像映射關於懸停更改圖像

HTML:

<div class="img-center"> 
    <img src="img-src_off.jpg" usemap="#Map" class="feature-image" border="0" /> 
    <map name="Map" id="Map"> 
     <area shape="rect" coords="77,461,391,492" href="#" target="_blank" 
      class="link-1" /> 
     <area shape="rect" coords="557,461,855,490" href="#" target="_blank" 
      class="link2" /> 
    </map> 
</div> 

JS:

注:這是JS我已經嘗試,但錯誤拋出(在下面列出的錯誤)和我現在不知道該怎麼做。如果我可以找到沒有問題的代碼,則不必使用此代碼。

var $j = jQuery.noConflict(); 
    $j(document).ready(function() { 
    $j.preLoadImages("img_src_01.jpg", 
        "img_src_02.jpg" 
    ); 

    $j(".link1").hover(function(){ 
     this.src = this.src.replace("_off","_01"); 
     }, 
     function(){ 
     this.src = this.src.replace("_01","_off"); 
     } 
    ); 

      $j(".link2").hover(function(){ 
     this.src = this.src.replace("_off","_02"); 
     }, 
     function(){ 
     this.src = this.src.replace("_02","_off"); 
     } 
    ); 
    }); 

    (function(j$) { 
    var cache = []; 
    $j.preLoadImages = function() { 
     var args_len = arguments.length; 
     for (var i = args_len; i--;) { 
     var cacheImage = document.createElement('img'); 
     cacheImage.src = arguments[i]; 
     cache.push(cacheImage); 
     } 
    } 
    }); 

我看到的錯誤如下:

TypeError: this.src is undefined 
[Break On This Error] 

this.src = this.src.replace("_off","_01"); 

(this error repeats itself on every hover) 

而這個錯誤:

TypeError: $j.preLoadImages is not a function 
[Break On This Error] 

"img_src_02.jpg" 

請回答的時候,因爲我不是在Javascript很流利提供範例。

回答

1

To write a jQuery plugin, start by adding a new function property to the jQuery.fn object where the name of the property is the name of your plugin:

嘗試以下操作:

(function($j) { 
    $j.fn.preLoadImages = function() { 
     //... 
    } 
})(jQuery); 
+0

這仍然拋出了同樣的錯誤。 – L84 2012-08-12 19:25:10

+0

@Lynda因爲它是jQuery對象的方法之一,所以您應該選擇一個元素並製作一個jQuery對象,我會看到'$(selector).preLoadImages()' – undefined 2012-08-12 19:27:28

+0

。我認爲整個腳本需要重新編寫,但由於缺乏知識,我自己無法做到這一點。 – L84 2012-08-12 19:30:26