Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: countup timer jquery
Forum PHP.pl > Forum > Po stronie przeglądarki
El Nino9
witam
zaczerpnąłem z pewnej strony (link w kodzie) kod do timera liczącego dni od jakiejś tam daty. Niestety, skrypt nie dziala dobrze, gdyż wysypuje się przy wpisaniu jakiejś konkretnej daty.

Kod
/**
* @name        jQuery Count-UP Plugin
* @author        Martin Angelov
* @version     1.0
* @url            http://tutorialzine.com/2012/09/count-up-jquery/
* @license        MIT License
*/

(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');
        
        (function tick(){
            
            passed = Math.floor((new Date() - options.start) / 1000);
            
            // Number of days passed
            d = Math.floor(passed / days);
            updateDuo(0, 1, d);
            passed -= d*days;
            
            // Number of hours left
            h = Math.floor(passed / hours);
            updateDuo(2, 3, h);
            passed -= h*hours;
            
            // Number of minutes left
            m = Math.floor(passed / minutes);
            updateDuo(4, 5, m);
            passed -= m*minutes;
            
            // Number of seconds left
            s = passed;
            updateDuo(6, 7, s);
            
            // Calling an optional user supplied callback
            options.callback(d, h, m, s);
            
            // Scheduling another call of this function in 1s
            setTimeout(tick, 1000);
        })();
        
        // This function updates two digit positions at once
        function updateDuo(minor,major,value){
            switchDigit(positions.eq(minor),Math.floor(value/10)%10);
            switchDigit(positions.eq(major),value%10);
        }
        
        return this;
    };


    function init(elem, options){
        elem.addClass('countdownHolder');

        // Creating the markup inside the container
        $.each(['Days','Hours','Minutes','Seconds'],function(i){
            $('<span class="count'+this+'">').html(
                '<span class="position">\
                    <span class="digit static">0</span>\
                </span>\
                <span class="position">\
                    <span class="digit static">0</span>\
                </span>
                '
            ).appendTo(elem);
            
            if(this!="Seconds"){
                elem.append('<span class="countDiv countDiv'+i+'"></span>');
            }
        });

    }

    // Creates an animated transition between the two numbers
    function switchDigit(position,number){
        
        var digit = position.find('.digit')
        
        if(digit.is(':animated')){
            return false;
        }
        
        if(position.data('digit') == number){
            // We are already showing this number
            return false;
        }
        
        position.data('digit', number);
        
        var replacement = $('<span>',{
            'class':'digit',
            css:{
                top:'-2.1em',
                opacity:0
            },
            html:number
        });
        
        // The .static class is added when the animation
        // completes. This makes it run smoother.
        
        digit
            .before(replacement)
            .removeClass('static')
            .animate({top:'2.5em',opacity:0},'fast',function(){
                digit.remove();
            })

        replacement
            .delay(100)
            .animate({top:0,opacity:1},'fast',function(){
                replacement.addClass('static');
            });
    }
})(jQuery);


tu podpinamy skrypt:

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


problem leży prawdopodobnie w tym, że wyświetlane są tylko dwie cyfry dni.
wie ktoś jak zmodyfikować kod żeby działał poprawnie ?
maniana
Taka przeróbka na szybko:
  1. (function($){
  2.  
  3. // Number of seconds in every time division
  4. var days = 24*60*60,
  5. hours = 60*60,
  6. minutes = 60;
  7.  
  8. // Creating the plugin
  9. $.fn.countup = function(prop){
  10.  
  11. var options = $.extend({
  12. callback : function(){},
  13. start : new Date()
  14. },prop);
  15.  
  16. var passed = 0, d, h, m, s,
  17. positions;
  18.  
  19. // Initialize the plugin
  20. positions = [];
  21. init(this, options);
  22.  
  23. positions = this.find('.position');
  24.  
  25. (function tick(){
  26.  
  27. passed = Math.floor((new Date() - options.start) / 1000);//
  28. // Number of days passed
  29. d = Math.floor(passed / days);
  30. updateDuo(0,d);
  31. passed -= d*days;
  32.  
  33. // Number of hours left
  34. h = Math.floor(passed / hours);
  35. updateDuo(3,h);
  36. passed -= h*hours;
  37.  
  38. // Number of minutes left
  39. m = Math.floor(passed / minutes);
  40. updateDuo(5,m);
  41. passed -= m*minutes;
  42.  
  43. // Number of seconds left
  44. s = passed;
  45. updateDuo(7,s);
  46.  
  47. // Calling an optional user supplied callback
  48. options.callback(d, h, m, s);
  49.  
  50. // Scheduling another call of this function in 1s
  51. setTimeout(tick, 1000);
  52. })();
  53.  
  54. // This function updates two digit positions at once
  55. function updateDuo(from,value){
  56. $.each( value.toString().split(''), function(i){
  57. switchDigit(positions.eq(i+from),parseInt(this));
  58. });
  59. }
  60.  
  61. return this;
  62. };
  63.  
  64.  
  65. function init(elem, options){
  66. elem.addClass('countdownHolder');
  67. // Creating the markup inside the container
  68. $.each(['Days','Hours','Minutes','Seconds'],function(i){
  69. $('<span class="count'+this+'">').html('<span class="position"><span class="digit static">0</span></span>'+(this=='Days'?'<span class="position"><span class="digit static">0</span></span>':'')+'<span class="position"><span class="digit static">0</span></span>').appendTo(elem);
  70.  
  71. if(this!="Seconds"){
  72. elem.append('<span class="countDiv countDiv'+i+'"></span>');
  73. }
  74.  
  75. });
  76.  
  77. }
  78.  
  79. // Creates an animated transition between the two numbers
  80. function switchDigit(position,number){
  81.  
  82. var digit = position.find('.digit')
  83.  
  84. if(digit.is(':animated')){
  85. return false;
  86. }
  87.  
  88. if(position.data('digit') == number){
  89. // We are already showing this number
  90. return false;
  91. }
  92.  
  93. position.data('digit', number);
  94.  
  95. var replacement = $('<span>',{
  96. 'class':'digit',
  97. css:{
  98. top:'-2.1em',
  99. opacity:0
  100. },
  101. html:number
  102. });
  103.  
  104. // The .static class is added when the animation
  105. // completes. This makes it run smoother.
  106.  
  107. digit
  108. .before(replacement)
  109. .removeClass('static')
  110. .animate({top:'2.5em',opacity:0},'fast',function(){
  111. digit.remove();
  112. })
  113.  
  114. replacement
  115. .delay(100)
  116. .animate({top:0,opacity:1},'fast',function(){
  117. replacement.addClass('static');
  118. });
  119. }
  120. })(jQuery);
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.