2011-04-30 23 views
0

當前點擊會在我的代碼中啓動序列。我想改變它,以便「重新開始」淡入,出現5秒鐘,然後淡出,剩下的序列運行。runIt之前等5秒鐘(而不是點擊)?

完全初學者!我不知道該怎麼做。任何幫助?

$(document).ready(runIt); 

}); 


function runIt(){ 
$('#myText').hover(function(){ 
     $(this).clearQueue().html('Start Again'); 
}) 
    .click(function(){ 
    runIt(); 
    }) 
    .html('text 1') 
    .fadeIn(1000) 
    .delay(5000) 
    .fadeOut(1000,function(){ 
    $(this).html('text 2'); 
    }) 
    .fadeIn(1000) 
    .delay(5000) 
    .fadeOut(1000,function(){ 
    $(this).html('text 3').unbind('mouseenter mouseleave'); 
    }) 
    .fadeIn(1000); 
}; 

回答

0
$(function(){ 
    $(window).mousemove(function(){ 
    runIt(); 
    }); 
    runIt();  
}) 

function runIt() { 
    var it = $('#myText'); 
    it.stop(true,true).clearQueue().fadeOut(1).animate({left:0},500).queue(function(){ 
    it.html('Start Again'); 
    it.dequeue(); 
    }) 
    it.fadeIn(500).animate({left:0},5000).fadeOut(1000).queue(function(){ 
    it.html('test 1'); 
    it.dequeue(); 
    }) 
    it.fadeIn(1000).animate({left:0},5000).fadeOut(1000).queue(function(){ 
    it.html('test 2'); 
    it.dequeue(); 
    }) 
    it.fadeIn(1000).animate({left:0},5000).fadeOut(1000).queue(function(){ 
    it.html('test 3'); 
    $(window).unbind('mousemove'); 
    it.dequeue(); 
    }) 
    it.fadeIn(1000); 
} 

延遲功能不能得到適當的清除,所以我用一個靜態動畫假延遲(在這種情況下有生命值)。

+0

你好,謝謝你!我正在測試它,但還沒有設法讓它工作出於某種原因......就我想要發生的事情而言:我希望有一系列文本自動淡入淡出,但如果用戶移動鼠標我想要序列與「重新開始」中斷。如果他們把它放到最後的文本中,我希望它保持在屏幕上,不會受到鼠標移動的影響。 – Sarah 2011-04-30 18:08:11

+0

當你說移動鼠標時,你的意思是屏幕上的任何地方,或只是在動畫文本所在的元素上?另外,當你說中斷與「再次開始」,你的意思是動畫得到重置 – TheBentArrow 2011-04-30 20:02:59

+0

@TheBeArArrow是在屏幕上的任何地方,如果可能的話還有任何擊鍵。是的,動畫重置。基本上我想要這個網站上的東西:donothingfor2minutes.com ..除了一個計時器,而不是一些消息淡入淡出。 – Sarah 2011-04-30 22:10:25

0

我沒有經過測試,但這更接近你想要的...注意runIt()已經被移動到了外部函數。

$(function() { 
    $('#myText').hover(function(){ 
     $(this).clearQueue().html('Start Again'); 
    }).click(function(){ runIt(); }); 
    }); 


function runIt() { 
    $(this) 
    .html('text 1') 
    .fadeIn(1000) 
    .delay(5000) 
    .fadeOut(1000,function(){ 
     $(this).html('text 2'); 
    }) 
    .fadeIn(1000) 
    .delay(5000) 
    .fadeOut(1000,function(){ 
     $(this).html('text 3').unbind('mouseenter mouseleave'); 
    }) 
    .fadeIn(1000); 
}; 
0
$(window).ready(function() 
{ 
    setTimeout("runIt();", 5000); 
}); 
0

這裏是一個全面的測試和運行的代碼有一些變化,你可以進行更改,以任何你想要的東西。

希望這有助於。

編輯:如果你想停止的事件,你應該使用.stop(),而不是.clearQueue()

(function($) { 

    jQuery.fn.idle = function(time) { 
     $(this).animate({ 
      opacity: '+=0' 
     }, time); 
     return this; 
    }; 
})(jQuery); 



$(document).ready(function() { 
    runIt(); 
    function runIt() { 
    $('#myText').idle(5000).text('Start Again').fadeOut(5000, function() { 
     $(this).text('text 1'); 
     $(this).fadeIn(1000, function() { 
      $(this).fadeOut(5000, function() { 
       $(this).text('text 2'); 
       $(this).fadeIn(1000, function() { 
        $(this).fadeOut(5000, function() { 
         $(this).text('text 3'); 
         $(this).fadeIn(1000, function() { 
          $(this).fadeOut(5000, function() { 
           //runIt(); 
           //uncomment the above if you want a loop. 
          }); 
         }); 
        }); 
       }); 
      }); 
     }); 
    }); 
    }; 
});