2012-11-06 57 views
1

我在頁面上有三個圖像滑塊的實例,它們應該逐個淡入一個接一個。我正在使用innerfade.js推遲一個jQuery函數

我想讓他們在不同的時間開始,例如,第一個在2秒後開始,第二個在4秒後,第三個在6秒後。所以,fisrt轉換需要在2秒後發生,但是直到下一個衰落才需要6秒。


編輯: 他們應該在同一時間一起出現在頁面加載時,然後褪色一個接一個。


我不知道我是否可以做到這一點。我嘗試過使用setTimeout以及.delay,但我無法使它工作。原因可能是我對Javascript不太好。但我會很感激這裏的一些幫助。

這是我來自:

$(document).ready(
    function(){ 
     $('#header-img-1').innerfade2({ 
      animationtype: 'fade', 
      speed: 750, 
      timeout: 6000, 
      type: 'random_start', 
      containerheight: '1em' 
     }); 

     $('#header-img-2').innerfade2({ 
      animationtype: 'fade', 
      speed: 750, 
      timeout: 6000, 
      type: 'random_start', 
      containerheight: '1em' 
     }); 

     $('#header-img-3').innerfade2({ 
      animationtype: 'fade', 
      speed: 750, 
      timeout: 6000, 
      type: 'random_start', 
      containerheight: '1em' 
     }); 
    } 
); 

非常感謝。

+0

'setTimeout'應該可以正常工作。你嘗試了什麼? – j08691

+0

你可以嘗試http://benalman.com/projects/jquery-throttle-debounce-plugin/ – HungryCoder

+0

你的問題可能來自你的函數'innerfade2'。你能提供代碼嗎? – sdespont

回答

0

這裏是正在尋找如何使用的setTimeout來達到一種互動,你的一個簡單的例子:

http://jsfiddle.net/zFMjf/

HTML

<div id="example1" class="con orange" style="display:none;">Example 1</div> 
<div id="example2" class="con lime" style="display:none;">Example 2</div> 
<div id="example3" class="con yellow" style="display:none;">Example 3</div>​ 

CSS

.con { width:200px; line-height:60px; margin:0 0 20px 0; } 
.orange { background:orange; } 
.lime { background:lime ; } 
.yellow { background:yellow ; } 

jQUERY

setTimeout(function(){ 
    $('#example1').slideToggle(); 
},2000); 

setTimeout(function(){ 
    $('#example2').slideToggle(); 
},4000); 

setTimeout(function(){ 
    $('#example3').slideToggle(); 
},6000);​ 
0

你是對的,你可以使用setTimeoutdelay函數延遲腳本執行。

這裏是setTimeout使用的樣本:

setTimeout(function(){ 
     $('#header-img-2').innerfade2({ 
      animationtype: 'fade', 
      speed: 750, 
      timeout: 6000, 
      type: 'random_start', 
      containerheight: '1em' 
     }); 
    },2000 /*2seconds*/); 

關於.delay()你應該編寫這樣的:

$('#header-img-2').delay(2000).innerfade2({ 
    animationtype: 'fade', 
    speed: 750, 
    timeout: 6000, 
    type: 'random_start', 
    containerheight: '1em' 
}); 
0
$(document).ready(function() { 
    var numb = [1,2,3]; 

    $.each(numb, function(i,elm) { 
     setTimeout(function() { 
      $('#header-img-'+numb[i]).innerfade2({ 
       animationtype: 'fade', 
       speed: 750, 
       timeout: 6000, 
       type: 'random_start', 
       containerheight: '1em' 
      }); 
     },numb[i]*2000); 
    }); 
}); 

FIDDLE

+0

謝謝這似乎並沒有工作:http://jsfiddle.net/spirelli/xXSb5/ – user1154435

+0

@ user1154435 - 我的壞,忘了'document.ready'中的'function',是否意識到你把它放在單獨的行中? – adeneo

+0

太好了。不過,我之後的所有三個幻燈片都是在初始階段才顯示出來的,但是過渡階段卻是一個接一個地開始。有沒有簡單的調整呢? – user1154435

0

試試下面的方法,我已經優化了它有點基於少數假設..但是s會給你一個想法。讓我知道它是不同的。

$(document).ready(function() { 

     var innerFadeOpt = { 
      animationtype: 'fade', 
      speed: 750, 
      timeout: 6000, 
      type: 'random_start', 
      containerheight: '1em' 
     }; 

     var ptr = 0, headerImgs = ['#header-img-1', '#header-img-2', '#header-img-3']; 

     var timer = setInterval (function() { 
      if (ptr == headerImgs.length - 1) { 
       clearInterval(timer); 
       return; 
      } 

      $(headerImgs[ptr++]).innerfade2(innerFadeOpt); 
     }, 2000); 
    } 
); 
+0

嗨維加(和其他人),它看起來好像你的代碼最接近,即它實際上做了什麼。我已經在這裏生活了:http://jsfiddle.net/spirelli/Rca4d/我已經從腳本中刪除了「 - 1」,否則第三個ID不會迴應。麻煩的是,現在滑塊內的所有圖像都顯示最初只會在第一次事務之後消失。 ---我叮叮我需要進一步的幫助。 – user1154435