FreakyD96 / jquery-countdown

Automatically exported from code.google.com/p/jquery-countdown
0 stars 0 forks source link

This is a new version, with some fixes i hope they are good for somebody #67

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
-Added count down from date (function hoursTill(day,month,year))
-fixes issue to make the count down smaller ( "background-size": 
options.digitWidth+'px 100%',)

jQuery.fn.countdown = function(userOptions)
{
  // Default options
  var options = {
    stepTime: 60,
    // startTime and format MUST follow the same format.
    // also you cannot specify a format unordered (e.g. hh:ss:mm is wrong)
    format: "dd:hh:mm:ss",
    startTime: "01:12:32:55",
    digitImages: 6,
    digitWidth: 27,
    digitHeight: 40,
    timerEnd: function(){},
    image: "digits.png"
  };
  var digits = [], interval,intervalhide, contadorsegundos=0;

  var hoursTill =function (day,month,year) {
    date_future = new Date(year+"/"+month+"/"+day);
    date_now = new Date();

    seconds = Math.floor((date_future - (date_now))/1000);
    minutes = Math.floor(seconds/60);
    hours = Math.floor(minutes/60);
    days = Math.floor(hours/24);

    hours = hours-(days*24);
    minutes = minutes-(days*24*60)-(hours*60);
    seconds = seconds-(days*24*60*60)-(hours*60*60)-(minutes*60);
    if(seconds<10) seconds="0"+seconds;
    if(minutes<10) minutes="0"+minutes;
    if(hours<10) hours="0"+hours;
    return days+":"+hours+":"+minutes+":"+seconds;
  }
  // Draw digits in given container
  var createDigits = function(where) 
  {
    var c = 0;
    // Iterate each startTime digit, if it is not a digit
    // we'll asume that it's a separator
    for (var i = 0; i < options.startTime.length; i++)
    {
      if (parseInt(options.startTime[i]) >= 0) 
      {
        elem = $('<div id="cnt_' + i + '" class="cntDigit" />').css({
          height: options.digitHeight * options.digitImages * 10, 
          float: 'left',
          background: "url('" + options.image + "')",
          "background-size": options.digitWidth+'px 100%',
          width: options.digitWidth});
        digits.push(elem);
        margin(c, -((parseInt(options.startTime[i]) * options.digitHeight *
                              options.digitImages)));
        digits[c].__max = 9;
        // Add max digits, for example, first digit of minutes (mm) has 
        // a max of 5. Conditional max is used when the left digit has reach
        // the max. For example second "hours" digit has a conditional max of 4 
        switch (options.format[i]) {
          case 'h':
            digits[c].__max = (c % 2 == 0) ? 2: 9;
            if (c % 2 == 0)
              digits[c].__condmax = 4;
            break;
          case 'd': 
            digits[c].__max = 9;
            break;
          case 'm':
          case 's':
            digits[c].__max = (c % 2 == 0) ? 5: 9;
        }
        ++c;
      }
      else 
        elem = $('<div class="cntSeparator"/>').css({float: 'left'})
                .text(options.startTime[i]);

      where.append(elem)
    }
  };

  // Set or get element margin
  var margin = function(elem, val) 
  {
    if (val !== undefined)
      return digits[elem].css({'marginTop': val + 'px'});

    return parseInt(digits[elem].css('marginTop').replace('px', ''));
  };

  // Makes the movement. This is done by "digitImages" steps.
  var moveStep = function(elem){
    digits[elem]._digitInitial = -(digits[elem].__max * options.digitHeight * options.digitImages);

    return function _move() {
      mtop = margin(elem) + options.digitHeight;

      if (mtop == options.digitHeight) {
        margin(elem, digits[elem]._digitInitial);
        if (elem > 0){
            moveStep(elem - 1)();

        }
        else{
          clearInterval(interval);
          for (var i=0; i < digits.length; i++) margin(i, 0);
          options.timerEnd();
          return;
        }
        if ((elem > 0) && (digits[elem].__condmax !== undefined) &&  (digits[elem - 1]._digitInitial == margin(elem - 1))){
            margin(elem, -(digits[elem].__condmax * options.digitHeight * options.digitImages));

        }
        return;
      }

        margin(elem, mtop);
        if (margin(elem) / options.digitHeight % options.digitImages != 0){
            setTimeout(_move, options.stepTime);
        }
        hideSystem();
        if (mtop == 0) digits[elem].__ismax = true;
    }
  };
  var hideSystem = function(){
    if(contadorsegundos!=-1){
        contadorsegundos++;
    }

    if(contadorsegundos>=40){
        contadorsegundos=-1;
        if(positionTicker=="open"){
            positionTicker='closed';
            $('#vertical-ticker').animate({
        'marginRight' : "-="+($('#vertical-ticker').width()-barWidth)+"px"
            });
        }
    }
  }

  $.extend(options, userOptions);
  this.css({height: options.digitHeight, overflow: 'hidden'});
  createDigits(this);
  interval = setInterval(moveStep(digits.length - 1), 1000);
};

var barWidth=8;
var positionTicker='open';
function showTicker(){
    if(positionTicker=="closed"){
        contadorsegundos=0;
        positionTicker='open';
        $('#vertical-ticker').animate({
        'marginRight' : "+="+($('#vertical-ticker').width()-barWidth)+"px"
        });
    }
    else{
        positionTicker='closed';
        contadorsegundos=-1;
            $('#vertical-ticker').animate({
        'marginRight' : "-="+($('#vertical-ticker').width()-barWidth)+"px"
            });
    }
}

Original issue reported on code.google.com by qaseria...@gmail.com on 2 Aug 2013 at 4:59