2013-03-14 67 views
0

我像jQuery有一個onload事件功能:

$(function() { 
    $('.over').hover(function() { 

      //FIRST PARAMETER RIGT, LEFT, TOP, BOTTOM 
     if ($(this).attr('data-direction') == 'right') { 
      $(this).addClass('flipping-right'); 
     } 
     else if ($(this).attr('data-direction') == 'left') { 
      $(this).addClass('flipping-left'); 
     } 
     else if ($(this).attr('data-direction') == 'top') { 
      $(this).addClass('flipping-top'); 
     } 
       //ETC. 

    //SECOND gal1,gal2,gal3,gal4 
    if ($(this).attr('gal') == 'gal1'){  
     img = $(this).find('img'); 
     setTimeout(function() { 
      img.attr('src', arrFirstYellowCard[i]); 
      i++; 
      if(i > arrFirstYellowCard.length-1) 
       i=0; 
     }, 100); 
    } 

    else if ($(this).attr('gal') == 'gal2'){  
     img = $(this).find('img'); 
     setTimeout(function() { 
      img.attr('src', arrSecondPurpleCard[j]); 
      j++; 
      if(j > arrSecondPurpleCard.length-1) 
       j=0; 
     }, 100); 
    } 

我想一個函數來執行功能每秒,但與參數在像

var array_param = ["right,gal1","top,gal3","bottom,gal4","left,gal2","right,gal5"]; 

陣列被允許組合

我要像一個計時器,以便每個參數被稱爲每秒

var timer = $.timer($('.over').hover(array_param(0)), 1000); 
timer.play(); 

,但我不知道該如何實現這個功能,以及如何將參數添加到的onload分配的功能:

$(function() { $('.over').hover(function() {...

Please take a look at my jsfiddle

+0

而不是每秒鐘添加新的事件處理程序,您應該每秒更改一次數組,並引用來自一個處理程序的數組。 – Bergi 2013-03-14 16:31:32

+0

可以請你發表樣品嗎? – cMinor 2013-03-14 18:16:46

回答

1

不能再次觸發準備。你可以創建一個命名的函數並將其稱爲一堆。

$(function() { 
    index = 0; 
    window.setInterval(function() { 
     if (index > array_param.length) index = 0; 
     myFunction(array_param[index++]); 
    }, 1000); // call myFunction every second 
}); 

myFunction = function (image) { 
    $('.over').hover(function() { 
     // rest of your code goes here 
     ... 
}; 
+0

你不需要再次觸發'ready'。 – Bergi 2013-03-14 16:30:36

+0

我不確定你的評論意味着什麼。我說你不能再觸發準備。這不是一個不需要的問題,這是不可能的,它只能觸發一次。 – 2013-03-14 16:33:05

+0

如何使用您添加的代碼將'right()'賦值給'$(this).attr('data-direction')'並將gal1賦值給'$(this).attr('gal')'myFunction(array_param [索引++]);'在函數定義中:'myFunction = function(dir,gal),以簡化我直接傳遞值但沒有運氣的事情http://jsfiddle.net/fUsPh/45/ – cMinor 2013-03-14 16:45:25

相關問題