2016-03-06 90 views
0

我正在嘗試從特定日期開始向上計時。我用這個(http://tutorialzine.com/2012/09/count-up-jquery/)教程做,但我不知道在哪裏放:如何在向上計時器上設置特定日期

$( '#倒計時')計數進位({ 開始:新的日期(2012,10,27,15, 58,21)//年,月,日,小時,分鐘,秒 });

如其在說明中所述。

代碼的開頭是這樣的:

(function($){ 

    // Number of seconds in every time division 
    var days = 24*60*60, 
     hours = 60*60, 
     minutes = 60; 

    // Creating the plugin 
    $.fn.countup = function(prop){ 

     var options = $.extend({ 
      callback : function(){}, 
      start  : new Date() 
     },prop);  

     var passed = 0, d, h, m, s, 
      positions; 

     // Initialize the plugin 
     init(this, options); 

     positions = this.find('.position'); 

回答

1

你必須調用使用

$('#countdown').countup({ start: new Date(2012, 10, 27, 15, 58, 21) //year, month, day, hour, min, sec }); 

是HTML將有一個元素與ID插件= 「倒計時」

<div id="countdown"></div> 

現在你可以把它放在你的JS的任何地方。比方說,你想調用這個插件時窗口負載,JS文件,你有可能碰到這樣的:

$(window).on('load',function(){ 
    $('#countdown').countup({ start: new Date(2012, 10, 27, 15, 58, 21)}); 
}) 

打破發生了什麼:

$("#countdown").countup()//calls a jQuery function obviously 

作爲參數要傳遞一個對象 {星:新的Date()}

這裏是接受的格式javascript日期的小細節()方法會接受:http://www.w3schools.com/js/js_date_formats.asp

現在,一旦你撥打.countup()函數,注意$ .extend()的一部分,它到第一個,即基本上覆制第二個對象,如果你傳遞

{start:new Date("2015-11-10")} 

這將覆蓋默認鍵啓動函數定義。如果你不把它作爲一個參數傳遞,它就會回落到新的Date()中,這實際上就是當前的日期時間。

+0

我投了你的答案,也張貼另一個例子作爲補充你的解釋。 – Roberto

1

這是一個完整的工作示例。運行該代碼段以查看其工作。

請參閱@Anubhab的答案(向上投票)瞭解其工作原理的詳細信息。此外,請確保您的頁面包含使其工作所需的所有必需的CSS和Javascript文件。

<!DOCTYPE HTML> 
 
<html> 
 
<head> 
 
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300" /> 
 
    <link rel="stylesheet" href="http://demo.tutorialzine.com/2012/09/count-up-jquery/assets/countup/jquery.countup.css" /> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
 
    <script src="http://demo.tutorialzine.com/2012/09/count-up-jquery/assets/countup/jquery.countup.js"></script> 
 
</head> 
 
<body> 
 
    <div id="countdown"></div> 
 
    <script> 
 
    $(function() { 
 
     // jQuery ready - create counter 
 
     $('#countdown').countup({ 
 
     start: new Date(2016, 0, 1, 0, 0, 0) 
 
     }); 
 
    }); 
 
    </script> 
 
</body> 
 
</html>

+0

謝謝你的回答!但根據設定的日期,櫃檯是不正確的,我該如何解決? –

+0

@RikardAbrahamsson - 看起來該插件最多有99天。您需要修改插件代碼以顯示更多數字或使用小於100天的開始日期。我已將示例中的開始日期更改爲1-1-2016,這似乎起作用。 – Roberto

相關問題