2012-11-13 100 views
1

我有一個很好的動畫,但現在我試圖通過淡出文本然後運行另一個函數來反轉這個動畫。 在此頁面加載以下運行......settimeout之後的jquery運行函數

$(document).ready(function() { 

     dream(); 
     setTimeout(runul(), 8000, function() {showVideo()}); 

    }); 

夢想函數創建對身體的背景氣泡。

function dream() { 
     //calculating random color of dream 
     if (animation_stopped) return; 
     var color = 'rgb(' + Math.floor(Math.random() * 255) + ',' + Math.floor(Math.random() * 255) + ',' + Math.floor(Math.random() * 255) + ')'; 

     //calculating random X position 
     var x = Math.floor(Math.random() * $(window).width()); 

     //calculating random Y position 
     var y = Math.floor(Math.random() * $(window).height()); 

     //creating the dream and hide 
     drawingpix = $('<span>').attr({ class: 'drawingpix' }).hide(); 

     //appending it to body 
     $(document.body).append(drawingpix); 

     //styling dream.. filling colors.. positioning.. showing.. growing..fading 
     drawingpix.css({ 
      'background-color': color, 
      'border-radius': '100px', 
      '-moz-border-radius': '100px', 
      '-webkit-border-radius': '100px', 
      top: y - 14, //offsets 
      left: x - 14 //offsets 
     }).show().animate({ 
      height: '500px', 
      width: '500px', 
      'border-radius': '500px', 
      '-moz-border-radius': '500px', 
      '-webkit-border-radius': '500px', 
      opacity: 0.1, 
      top: y - 250, //offsets 
      left: x - 250 
     }, 3000).fadeOut(2000); 

     //Every dream's end starts a new dream 
     window.setTimeout('dream()', 200); 
    } 

當夢想函數運行時,我運行runul()函數,它開始輸入文本。

function runul() { 
     jQuery("#ticker").ticker({ 
      cursorList: " ", 
      rate: 50, 
      delay: 18000 
     }).trigger("play").trigger("stop"); 

     // Trigger events 
     jQuery(".stop").click(function() { 
      jQuery("#ticker").trigger("stop"); 
      return false; 
     }); 

     jQuery(".play").click(function() { 
      jQuery("#ticker").trigger("play"); 
      return false; 
     }); 

    } 

當runul()動畫完成後,我想運行showVideo函數。我想要這個函數淡出輸入的文本,然後淡化包裝在div中的iframe。

function showVideo() { 
     $('#divFade').fadeOut(3000); 
     animation_stopped = true; 
     $(typetext).css('color', 'black'); 
     $("body").animate({ backgroundColor: "#ffffff" }, 3000); 
     $('#divVideos').fadeIn('slow'); 
     // Animation complete. 
     $("#iVideos").prop('src', 'Videos/Quick Start Intro_player.html'); 
     return false; 
     }; 

如何在runul()完成後運行showVideo?

非常感謝您的幫助

+0

不會。 ticker()提供任何回調參數,這些參數可以幫助您在某些股票行爲完成後開始某些操作? – devnull69

+0

我不這麼認爲。 –

+0

這意味着.trigger(「play」)和.trigger(「stop」)會在ticker上觸發一些異步操作,但在此操作完成之前,下面的代碼(包括由@ ftom2提出的回調)已經運行... – devnull69

回答

4

你應該通過「showVideo」作爲一個回調函數:

setTimeout(function() {runul(showVideo)},8000); 

function runul(callback) { 
     jQuery("#ticker").ticker({ 
      cursorList: " ", 
      rate: 50, 
      delay: 18000 
     }).trigger("play").trigger("stop"); 

     // Trigger events 
     jQuery(".stop").click(function() { 
      jQuery("#ticker").trigger("stop"); 
      return false; 
     }); 

     jQuery(".play").click(function() { 
      jQuery("#ticker").trigger("play"); 
      return false; 
     }); 

     callback(); 

    } 

您還可以使用jQuery defferred和具體的when功能

+0

我試過回調,但show run在runul()運行時運行。 –

+0

@SheriTrager - 更新了我的答案,我改變了setTimeout。 – Tomer

+0

我只是在學習jQuery,你有使用defferred的例子嗎?謝謝 –

相關問題