制作倒计时时钟

时间:2016-05-16 18:25:35

标签: javascript countdown

我不确定如何更改此字词的字体,大小和颜色。

<script type="text/javascript">
    var end = new Date('02/19/2012 10:1 AM');

    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour * 24;
    var timer;

    function showRemaining() {
        var now = new Date();
        var distance = end - now;
        if (distance < 0) {

            clearInterval(timer);
            document.getElementById('countdown').innerHTML = 'EXPIRED!';

            return;
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);

        document.getElementById('countdown').innerHTML = days + 'days ';
        document.getElementById('countdown').innerHTML += hours + 'hrs ';
        document.getElementById('countdown').innerHTML += minutes + 'mins ';
        document.getElementById('countdown').innerHTML += seconds + 'secs';
    }

    timer = setInterval(showRemaining, 1000);
</script>
<div id="countdown"></div>

1 个答案:

答案 0 :(得分:1)

试试这个样本:

只需在代码中添加一个新行:

document.getElementById('countdown').style.cssText = 'color: green; font-size: 40px;';

它可以帮助您定义所需的任何样式。

var end = new Date('02/19/2017 10:1 AM');

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;

function showRemaining() {
    var now = new Date();
    var distance = end - now;
    if (distance < 0) {

        clearInterval(timer);
        document.getElementById('countdown').innerHTML = 'EXPIRED!';

        return;
    }
    var days = Math.floor(distance / _day);
    var hours = Math.floor((distance % _day) / _hour);
    var minutes = Math.floor((distance % _hour) / _minute);
    var seconds = Math.floor((distance % _minute) / _second);

    document.getElementById('countdown').style.cssText = 'color: green; font-size: 40px;';
    document.getElementById('countdown').innerHTML = days + ' days ';
    document.getElementById('countdown').innerHTML += hours + ' hrs ';
    document.getElementById('countdown').innerHTML += minutes + ' mins ';
    document.getElementById('countdown').innerHTML += seconds + ' secs';
    
}

timer = setInterval(showRemaining, 1000);
<span id="countdown"></span>

注意到@andlrc,您可以在没有JavaScript的情况下执行此操作。使用CSS样式。在html页面中添加新样式

<style type="text/css">
    #countdown {

        color: green;
        font-size: 40px;
    }
</style>
<span id="countdown"></span>