2012-03-14 17 views
1

我想盡可能地將舊網站的功能複製爲精簡版,並且我遇到了.hover.each的問題。此時,它將在mouseover上交換圖像,但出於某種原因不會在mouseout上交換。我知道這是與語法有關的東西,但是當我漸漸感冒時,我的大腦並沒有抓住它。將鼠標懸停在div上並使用jQuery更改兩個圖像?

相關代碼:

<script> 
(function($) { 
    $(".home-center").hover(function(){ 
     $("img", this).each(
      function() { 
       $(this).attr("src", $(this).attr("src").replace(/-off.png/, "-on.png")); 
      }, 
      function() { 
       $(this).attr("src", $(this).attr("src").replace(/-on.png/, "-off.png")); 
      } 
     ); 
    }); 
})(jQuery); 
<script> 
<div class="home-center"> 
    <div class="home-right"> 
     <img class="home-icon" src="/images/homepage/home-icon-off.png" /> 
    </div> 
    <div class="home-link"> 
     <img class="home-img" src="/images/homepage/home-downloads-off.png" /> 
    </div> 
</div> 
+0

你也可以檢查一下 - > http://jqueryfordesigners.com/image-cross-fade-transition/ – SpYk3HH 2012-03-14 21:14:46

回答

3

重複這種最瘦的方法是使用CSS,而不訴諸JavaScript的根本:

<style> 
    .home-center .off { 
    display: inline; 
    } 
    .home-center:hover .off { 
    display: none; 
    } 
    .home-center .on { 
    display: none; 
    } 
    .home-center:hover .on { 
    display: inline; 
    } 
</style> 
<div class="home-center"> 
    <div class="home-right"> 
    <img class="home-icon off" src="/images/homepage/home-icon-off.png" /> 
    <img class="home-icon on" src="/images/homepage/home-icon-on.png" /> 
    </div> 
    <div class="home-link"> 
    <img class="home-img off" src="/images/homepage/home-downloads-off.png" /> 
    <img class="home-img on" src="/images/homepage/home-downloads-on.png" /> 
    </div> 
</div> 

我沒有測試此所以它可能是錯的,但你的想法。

我認爲這至少在IE7 +中可用,但很可能在IE6甚至IE5中。

+0

嗯,這是美好的,但最終沒用,因爲我沒有使用背景圖像。除了跨瀏覽器兼容性和空divs的問題之外,所選的實際圖像都會被選中,並用Google圖片搜索進行alt標記,而使用CSS將完全丟失。不過,我正在使用CSS解決方案來更改div中的文本,並且真的希望有一個可以爲此工作的CSS解決方案。 – SickHippie 2012-03-14 21:25:38

+0

夠公平的,我會更新我的例子:) – Thomas 2012-03-14 21:27:35

+0

完成。實際上現在好多了。 – Thomas 2012-03-14 21:33:33

2

變化以這樣的:

$(document).ready(function() { 
    $(".home-center").each(
     $(this).hover(function(){ 
      var img = $(this).children('img'); 
      function() { 
       img.attr("src", img.attr("src").replace(/-off.png/, "-on.png")); 
      }, 
      function() { 
       img.attr("src", img.attr("src").replace(/-on.png/, "-off.png")); 
      } 
     ); 
    }); 
}); 
+0

是的,他會說同樣的事情,將懸停事件應用到每個元素。不是每個元素的懸停事件。 – 2012-03-14 21:13:07

+0

現在你想着與門戶:) – Kristian 2012-03-14 21:14:39

+0

img.home-center?這是對的嗎? img.home-center將尋找類.home-center右側的圖像?糾正我,如果我錯了。 – 2012-03-14 21:14:44

0

您需要申請的每個函數中..或懸停之外。現在你只提供一個鼠標懸停參數(每個參數),沒有鼠標懸停。

0

關閉!看起來像一個嵌套問題。這是hover有兩個參數,而不是each

(function($) { 
$(".home-center").hover(function(){ 
    $("img", this).each(
     function() { 
      $(this).attr("src", $(this).attr("src").replace(/-off.png/, "-on.png")); 
     } 
    ), 
    $("img", this).each(
     function() { 
      $(this).attr("src", $(this).attr("src").replace(/-on.png/, "-off.png")); 
     } 
    ); 
}); 
})(jQuery); 
+0

之後的行上有一個名字',它看起來應該可以工作,但它實際上什麼也不做。不太清楚爲什麼。 – SickHippie 2012-03-14 21:29:25

2

.hover需要2個ARGS(的mouseenter,鼠標離開)。你只有一個參數。

嘗試類似如下,(請參見下面我替代的解決方案)

(function($) { 
    $(".home-center").hover( 
     function() { //mouseenter 
      $("img", this).each(function() { 
       $(this).attr("src", $(this).attr("src").replace(/-off.png/, "-on.png")); 
      }); 
     }, 
     function() { //mouseleave 
      $("img", this).each(function() { 
        $(this).attr("src", $(this).attr("src").replace(/-on.png/, "-off.png")); 
      }); 
     } 
    ); 
})(jQuery); 

或者我只想實現.mouseenter.mouseleave來選擇呼叫的簡單數量。

(function($) { 
    $("img", ".home-center").mouseenter(
      function() { //mouseenter 
       $(this).each(function() { 
        $(this).attr("src", $(this).attr("src").replace(/-off.png/, "-on.png")); 
       }); 
      }) 
      .mouseleave(function() { //mouseleave 
       $(this).each(
        function() { 
        $(this).attr("src", $(this).attr("src").replace(/-on.png/, "-off.png")); 
       }); 
      } 
     ); 
})(jQuery); 
+0

第一個例子在$('img',this).each(',')後面的行中拋出'function statement require a name'的錯誤,第二個例子只能在圖片上懸停,而不是包含div。 – SickHippie 2012-03-14 21:33:01

+0

@ SickHippie第一個函數出錯了..現在修正了..立即嘗試,讓我知道它是怎麼回事。 – 2012-03-14 21:37:47

+0

不得不去掉其中一個'}',但它確實有效。 CSS解決方案,但它 - 第一次鼠標懸停沒有任何延遲的額外好處,因爲圖像已經加載,我將把這個文件提交給以後使用,但 - 謝謝你的幫助! – SickHippie 2012-03-14 21:45:25

相關問題