2012-01-27 43 views
0

我有黑白圖像顯示。當你將鼠標懸停在圖像上時,它會變成彩色圖像,這是一個單獨的文件(img-1.png和hover-img-1.png等)。 還有一個標題,應該總是顯示在圖像的頂部作爲絕對定位的div。jquery在懸停的交換圖像WITH標題

我的問題是,像做掉,但是當我把鼠標放在標題,圖像會回到B/W,而不是停留在顏色。

我錯過了什麼?

這是我目前擁有的代碼:

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 

<style> 
    .galleryImg { 
     float:left; 
     margin: 10px 4px; 
     position:relative; 
    } 
    .caption { 
     position: absolute; 
     bottom: 10px; 
     right: 15px; 
     text-transform:uppercase; 
     font-size:14px; 
    } 
</style> 
<script type='text/javascript' src='jquery.js'></script> 
<script type="text/javascript"> 
<!-- 

    // wrap as a jQuery plugin and pass jQuery in to our anoymous function 
    (function ($) { 
     $.fn.cross = function (options) { 
      return this.each(function (i) { 
       // cache the copy of jQuery(this) - the start image 
       var $$ = $(this); 

       // get the target from the backgroundImage + regexp 
       var target = $$.css('backgroundImage').replace(/^url|[\(\)'"]/g, ''); 

       // nice long chain: wrap img element in span 
       $$.wrap('<span style="position: relative;"></span>') 
        // change selector to parent - i.e. newly created span 
        .parent() 
        // prepend a new image inside the span 
        .prepend('<img>') 
        // change the selector to the newly created image 
        .find(':first-child') 
        // set the image to the target 
        .attr('src', target); 

       // the CSS styling of the start image needs to be handled 
       // differently for different browsers 
       if ($.browser.msie || $.browser.mozilla) { 
        $$.css({ 
         'position' : 'absolute', 
         'left' : 0, 
         'background' : '', 
         'top' : this.offsetTop 
        }); 
       } else if ($.browser.opera && $.browser.version < 9.5) { 
        // Browser sniffing is bad - however opera < 9.5 has a render bug 
        // so this is required to get around it we can't apply the 'top' : 0 
        // separately because Mozilla strips the style set originally somehow...      
        $$.css({ 
         'position' : 'absolute', 
         'left' : 0, 
         'background' : '', 
         'top' : "0" 
        }); 
       } else { // Safari 
        $$.css({ 
         'position' : 'absolute', 
         'left' : 0, 
         'background' : '' 
        }); 
       } 

       // similar effect as single image technique, except using .animate 
       // which will handle the fading up from the right opacity for us 
       $$.hover(function() { 
        $$.stop().animate({ 
         opacity: 0 
        }, 250); 
       }, function() { 
        $$.stop().animate({ 
         opacity: 1 
        }, 250); 
       }); 
      }); 
     }; 

    })(jQuery); 

    // note that this uses the .bind('load') on the window object, rather than $(document).ready() 
    // because .ready() fires before the images have loaded, but we need to fire *after* because 
    // our code relies on the dimensions of the images already in place. 
    jQuery(window).bind('load', function() { 
     jQuery('img.fade').cross(); 
    }); 

    //--> 
</script>  
</head> 

<body> 
<div class="galleryImg"> 
    <a href="some_link"> 
     <img src="images/img-1.png" alt="" title="" class="fade" style="background: url(images/hover-img-1.png);" /> 
     <div class="caption">This is Image 1</div> 
    </a> 
</div> 
<div class="galleryImg"> 
    <a href="some_link"> 
     <img src="images/img-2.png" alt="" title="" class="fade" style="background: url(images/hover-img-2.png);" /> 
     <div class="caption">This is Image 2</div> 
    </a> 
</div> 
<div class="galleryImg"> 
    <a href="some_link"> 
     <img src="images/img-2.png" alt="" title="" class="fade" style="background: url(images/hover-img-2.png);" /> 
     <div class="caption">This is Image 3</div> 
    </a> 
</div> 
</body> 

回答

2

大概是因爲div.caption被覆蓋的圖像,而不是在其上懸停事件處理程序註冊圖像的孩子。

裹在div.Somethingimgdiv.caption並登記在div.Something而不是圖像懸停處理程序。或者,實際上,您可以在已經包裝它們的<a>上註冊處理程序;)

不知道如何「更正」它是修改插件來做到這一點,但在這種情況下最快的解決方案會是改變

 $$.hover(function() { 
      $$.stop().animate({ 
       opacity: 0 
      }, 250); 
     }, function() { 
      $$.stop().animate({ 
       opacity: 1 
      }, 250); 
     }); 

 $$.closest('.galleryImg').hover(function() { 
      $$.stop().animate({ 
       opacity: 0 
      }, 250); 
     }, function() { 
      $$.stop().animate({ 
       opacity: 1 
      }, 250); 
     }); 

更新:使用.closest('.galleryImg')代替.parent()固定。

+0

對不起,但我是一個小白......我沒有寫代碼,我只是C/P-ED它...任何機會,你可以張貼整個代碼?因爲我按照你所說的嘗試替換,但沒有運氣。謝謝。 – user1078494 2012-01-27 00:34:42

+0

「不運氣」意味着什麼?同一行爲,其他不正確的行爲?錯誤(S)?這實際上只是添加'.parent()'。 – Supr 2012-01-27 11:26:34

+0

謝謝你回到我身邊。沒有運氣意味着我得到同樣的行爲。我按照你的建議添加了.parent(),但它仍然是一樣的 - 當我將鼠標懸停在文本上時,我的圖像又回到了b&w。我是瘋了還是什麼? – user1078494 2012-01-27 17:47:09