2010-03-12 63 views
0

我做了一個非常簡單的jQueryimg翻轉功能靈感來源於此: http://www.smileycat.com/miaow/archives/000224.php非常簡單的jQuery翻滾

它只是檢查名稱中包含_off的所有img,並將它們替換爲img,其名稱與「_on」相同,但不是「_off」。

因爲我不能在我的佈局中使用背景img,所以我覺得這是一個方便的解決方案。 但我感覺交換不順暢,就像函數運行緩慢一樣。 你覺得呢? 有沒有方法來優化它?

下面是代碼:

function roll_over() { 
     $("img[src*='_off']").hover(
      function() { 
       var stringa = $(this).attr("src"); 
       var stringa = stringa.replace("_off", "_on"); 
       $(this).attr("src", stringa); 
      }, 
      function() { 
       var stringa = $(this).attr("src"); 
       var stringa = stringa.replace("_on", "_off"); 
       $(this).attr("src", stringa); 
      } 
     ); 
    } 
+0

你什麼時候調用這個函數? – Buggabill 2010-03-12 14:16:37

+0

我在document.ready上調用函數 – Gusepo 2010-03-12 20:43:14

回答

0

反覆做$("img[src*='_off']")是低效的。這意味着瀏覽器在翻頁時不得不搜索頁面上的所有內容。你應該包括jQuery Lint關於優化的信息...

+0

我同意在mouseover事件中反覆調用這個函數將是不必要的,效率低下,並且不建議。如果他們在頁面加載時調用它,則完全是另一回事。 – Buggabill 2010-03-12 14:22:16

+0

還定義了兩次'stringa'變量? – 2010-03-12 14:25:44

+0

是的,這是真的 – Ross 2010-03-12 14:27:06

2

你的代碼是壞的。爲什麼?

  1. 當你有這樣的形象將失敗:...src="/images/my_office.png"...
  2. 您使用JS的東西是那麼原始,可以在純CSS來實現
  3. *_on圖像將在mouseover事件被加載所以你暫時不會看到任何圖像。

如何解決所有這些問題?使用CSS Sprites

  1. 這樣一個創建圖像:http://www.digart.pl/gfx/ico/s/fb.gif
  2. HTML代碼:<a href="..." id="myId">blah</a>(五言的,你不必使用A元素)。
  3. CSS代碼:

    #myId { 
        display: inline-block; /* or block, or even inline with correct line-height */ 
        width: 33px; 
        height: 20px; 
        background: url(/path/to/img) 0 0 no-repeat; 
    } 
    #myId:hover { 
        background-position: 50% 0; 
    } 
    

如果你仍然想使自動化整個過程,然後用JS只有改變背景位置,而不是圖像。

+0

+1 CSS sprites的確會使這更容易和更快。 – ryanulit 2010-03-12 14:38:07

+0

我不知道爲什麼,但是OP在他們的問題中這樣說:「......我的佈局不能使用背景圖片。」這可能是他們試圖這樣做的原因。 – Buggabill 2010-03-12 14:43:15

+0

@Buggabill:Ops ...我的錯。我沒有注意到這一點。 – Crozin 2010-03-12 14:51:48

0

的ID或類如何像:

// Add an image to your page as: 
// <img src="button.png" class="rollover"> 

<script type='text/javascript'> 
$(document).ready(function(){ 
    initRollovers(); 
}); 
function initRollovers() { 
    // Assign a class to the images you want to have as roll-over enabled. 
    // Example: 
    // <img src="button.png" class="rollover"> 
    // NOTE: No "dot" when setting the class on the image... But jQuery needs the .dot in order to search for classes in this script: 
    var classIdentifier = ".rollover"; 

    // This example assumes that only the "on" state has a suffix: 
    // button.png 
    // button_on.png 

    // If you have a suffix for both states, edit them here: 
    // button_off.png 
    // button_on.png 
    // ... would be edited as: 
    // var offSuffix = "_off.png"; 
    // var onSuffix = "_on.png"; 

    var offSuffix = ".png"; 
    var onSuffix = "_on.png"; 

    $(classIdentifier).each(function() { 

     var obj = $(this); 
     var mySrcOff = obj.attr("src"); 
     var mySrcOn = mySrcOff.replace(offSuffix, onSuffix); 
     obj.mouseout(function() { 
      $(this).attr("src", mySrcOff); 
     }); 
     obj.mouseover(function() { 
      $(this).attr("src", mySrcOn); 
     }); 

     // Preload Off State 
     $('<img />').attr('src', mySrcOn).appendTo('body').css('display','none'); 

    }); 
} 

</script>