2017-04-02 108 views
0

我在我的主頁上使用了倒計時腳本,現在我想爲其添加翻頁動畫。爲倒計時腳本設置動畫

我不知道如何添加一個類,然後將其刪除。我嘗試了YouTube上的教程,但每次添加教程的代碼時,我的倒計時都停止運行。

function getTimeRemaining(endtime) { 
 
    var t = Date.parse(endtime) - Date.parse(new Date()); 
 
    var seconds = Math.floor((t/1000) % 60); 
 
    var minutes = Math.floor((t/1000/60) % 60); 
 
    var hours = Math.floor((t/(1000 * 60 * 60)) % 24); 
 
    var days = Math.floor(t/(1000 * 60 * 60 * 24)); 
 
    return { 
 
    'total': t, 
 
    'days': days, 
 
    'hours': hours, 
 
    'minutes': minutes, 
 
    'seconds': seconds 
 
    }; 
 
} 
 

 
function initializeClock(id, endtime) { 
 
    var clock = document.getElementById(id); 
 
    var daysSpan = clock.querySelector('.days'); 
 
    var hoursSpan = clock.querySelector('.hours'); 
 
    var minutesSpan = clock.querySelector('.minutes'); 
 
    var secondsSpan = clock.querySelector('.seconds'); 
 

 
    function updateClock() { 
 
    var t = getTimeRemaining(endtime); 
 

 
    daysSpan.innerHTML = t.days; 
 
    hoursSpan.innerHTML = ('0' + t.hours).slice(-2); 
 
    minutesSpan.innerHTML = ('0' + t.minutes).slice(-2); 
 
    secondsSpan.innerHTML = ('0' + t.seconds).slice(-2); 
 

 
    if (t.total <= 0) { 
 
     clearInterval(timeinterval); 
 
    } 
 
    } 
 

 
    updateClock(); 
 
    var timeinterval = setInterval(updateClock, 1000); 
 
} 
 

 
var deadline = new Date(Date.parse(new Date()) + 15 * 24 * 60 * 60 * 1000); 
 
initializeClock('clockdiv', deadline);
body{ 
 
\t text-align: center; 
 
\t background: #00ECB9; 
 
    font-family: sans-serif; 
 
    font-weight: 100; 
 
} 
 

 
h1{ 
 
    color: #396; 
 
    font-weight: 100; 
 
    font-size: 40px; 
 
    margin: 40px 0px 20px; 
 
} 
 

 
#clockdiv{ 
 
\t font-family: sans-serif; 
 
\t color: #fff; 
 
\t display: inline-block; 
 
\t font-weight: 100; 
 
\t text-align: center; 
 
\t font-size: 30px; 
 
} 
 

 
#clockdiv > div{ 
 
\t padding: 10px; 
 
\t border-radius: 3px; 
 
\t background: #00BF96; 
 
\t display: inline-block; 
 
} 
 

 
#clockdiv div > span{ 
 
\t padding: 15px; 
 
\t border-radius: 3px; 
 
\t background: #00816A; 
 
\t display: inline-block; 
 
} 
 

 
.smalltext{ 
 
\t padding-top: 5px; 
 
\t font-size: 16px; 
 
}
<h1>Countdown Clock</h1> 
 
<div id="clockdiv"> 
 
    <div> 
 
    <span class="days"></span> 
 
    <div class="smalltext">Days</div> 
 
    </div> 
 
    <div> 
 
    <span class="hours"></span> 
 
    <div class="smalltext">Hours</div> 
 
    </div> 
 
    <div> 
 
    <span class="minutes"></span> 
 
    <div class="smalltext">Minutes</div> 
 
    </div> 
 
    <div> 
 
    <span class="seconds"></span> 
 
    <div class="smalltext">Seconds</div> 
 
    </div> 
 
</div>

回答