JavaScript显示div并在倒计时结束时添加类

问题描述 投票:0回答:2

我有一些带有倒数计时器的JavaScript,当它达到0时,它显示文本“已结束”。

当发生这种情况时,我需要在两个div上显示块并将一个类添加到另一个div。

这是JS代码:

function countdown( elementName, minutes, seconds )
{
    var element, endTime, hours, mins, msLeft, time;

    function twoDigits( n )
    {
        return (n <= 9 ? "0" + n : n);
    }

    function updateTimer()
    {
        msLeft = endTime - (+new Date);
        if ( msLeft < 1000 ) {
            element.innerHTML = "Ended";
        } else {
            time = new Date( msLeft );
            hours = time.getUTCHours();
            mins = time.getUTCMinutes();
            element.innerHTML = (hours ? hours + 'h ' + twoDigits( mins ) : mins) + 'm ' + twoDigits( time.getUTCSeconds() + 's' );
            setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
        }
    }

    element = document.getElementById( elementName );
    endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
    updateTimer();
}

countdown( "countdown", 1, 5 );
countdown( "countdown2", 100, 0 );

HTML:

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

这是一个小提琴:https://jsfiddle.net/vek5808u/8/

任何帮助都会很棒

谢谢

javascript html css
2个回答
0
投票

在if块内

 if ( msLeft < 1000 ) {

     document.querySelector(".messaging").style.display="block";
     document.querySelector(".img-message").style.display="block";

     document.querySelector(".img-container").className += " opacity-overlay";

     element.innerHTML = "Ended";
 }

https://jsfiddle.net/vek5808u/36/


0
投票

时间开始和显示时间之间有一点延迟。您可能需要使用它,但程序非常简单。

我将Countdown课程建立在某人在另一个问题上创建的Repeater课程中。

我甚至将它包装在一个jQuery插件中以方便使用。

const twoDigits = n => n <= 9 ? '0' + n : n;
const dispTime = t => {
  var h = t.getUTCHours(), m = t.getUTCMinutes();
  return (h ? h + 'h ' + twoDigits(m) : m) + 'm ' + twoDigits(t.getUTCSeconds() + 's')
};

function Countdown(minutes, seconds, callbackFn, successFn, scope) {
  var self = this;
  this.delay = 1000;
  this.timer = setTimeout(function() { self.run(); }, this.delay);
  this.callbackFn = callbackFn;
  this.successFn = successFn;
  this.endTime = new Date().getTime() + 1000 * (60 * minutes + seconds);
  this.scope = scope || self;
}
Countdown.prototype.run = function() {
  var self = this, timeRemaining = this.timeRemaining();
  this.callbackFn.call(this.scope, timeRemaining);
  if (timeRemaining < this.delay - 1) {
    this.successFn.call(this.scope);
  } else {
    this.timer = setTimeout(function() { self.run(); }, this.delay);
  }
};
Countdown.prototype.timeRemaining = function() {
  return this.endTime - new Date().getTime();
};

(function($) {
  $.fn.countdown = function(minutes, seconds, callbackFn, successFn) {
    new Countdown(minutes, seconds, callbackFn, successFn, this);
    return this;
  };
})(jQuery);

$('#countdown').countdown(0, 10, function(msRemaining) {
  this.html(dispTime(new Date(msRemaining)));
}, function() {
  this.html("Ended");
  $('.img-message').show();
});
.messaging,
.img-message {
  display: none;
}

.img-container.opacity-overlay {
  opacity: 0.6;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="countdown"></div>
<div class="messaging">
  <p>messaging here</p>
</div>
<div class="img-container">
  <div class="img-message">image message here</div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.