2017-07-17 11 views
2

「Days」、「Hours」、「Minutes」、「Seconds」の複数のラベルを持つJavaScriptカウントダウンタイマーがあります。それぞれのタイマーが「1」に達すると、HTMLのラベルを単数バージョンに変更する必要があります。JavaScriptカウントダウン1日、1秒、1分になるとタイマーのテキストが変更されます

どうすればよいですか?前もって感謝します。

var eventDate = 'Dec 16 2017 9:24:18 GMT-0400'; 
 

 
function time_remaining(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 run_clock(id, endtime) { 
 
    var clock = document.getElementById(id); 
 

 
    // get spans where our clock numbers are held 
 
    var days_span = clock.querySelector('.days'); 
 
    var hours_span = clock.querySelector('.hours'); 
 
    var minutes_span = clock.querySelector('.minutes'); 
 
    var seconds_span = clock.querySelector('.seconds'); 
 

 
    function update_clock() { 
 
    var t = time_remaining(endtime); 
 

 
    // update the numbers in each part of the clock 
 
    days_span.innerHTML = t.days; 
 
    hours_span.innerHTML = ('0' + t.hours).slice(-2); 
 
    minutes_span.innerHTML = ('0' + t.minutes).slice(-2); 
 
    seconds_span.innerHTML = ('0' + t.seconds).slice(-2); 
 

 
    if (t.total <= 0) { 
 
     clearInterval(timeinterval); 
 
     document.getElementById("clockdiv").style.display = "none"; 
 
    } 
 

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

 
} 
 
run_clock('clockdiv', eventDate);
.clockWrap { 
 
    text-align: center; 
 
    font-weight: 700; 
 
    font-size: 50px; 
 
    max-width: 100%; 
 
} 
 

 
h1.clock { 
 
    color: #595959; 
 
    font-size: 30px; 
 
    line-height: 36px; 
 
    margin: 0; 
 
    padding: 50px 0 30px; 
 
} 
 

 
.day { 
 
    padding-right: 44px; 
 
} 
 

 
.colon { 
 
    font-size: 30px; 
 
    padding-right: 25px; 
 
    padding-left: 25px; 
 
    line-height: 60px; 
 
} 
 

 

 
/*--Colors--*/ 
 

 
.red { 
 
    color: #fc5852; 
 
} 
 

 
.blue { 
 
    color: #24a5f4; 
 
} 
 

 
.orange { 
 
    color: #ffaa00; 
 
} 
 

 
.grey { 
 
    color: #b3b3b3; 
 
} 
 

 
.green { 
 
    color: #35b134; 
 
} 
 

 
#clockdiv { 
 
    display: inline-block; 
 
    text-align: center; 
 
} 
 

 
#clockdiv>div { 
 
    padding-bottom: 50px; 
 
    display: inline-block; 
 
} 
 

 
.smalltext { 
 
    padding-top: 5px; 
 
    font-size: 18px; 
 
    line-height: 24px; 
 
    font-weight: 400; 
 
    text-transform: uppercase; 
 
} 
 

 
@media only screen and (max-width: 540px) { 
 
    .timeWrapper { 
 
    width: 167px; 
 
    margin: auto; 
 
    padding-bottom: 15px; 
 
    } 
 
    h1.clock { 
 
    font-size: 20px; 
 
    line-height: 36px; 
 
    padding: 18px 0 5px; 
 
    } 
 
    .day { 
 
    padding-right: 0; 
 
    } 
 
    .colon { 
 
    display: none !important; 
 
    } 
 
    #clockdiv { 
 
    text-align: left !important; 
 
    } 
 
    #clockdiv>div { 
 
    padding-bottom: 4px; 
 
    display: inline-block; 
 
    } 
 
    .smalltext { 
 
    padding-top: 19px; 
 
    padding-left: 15px; 
 
    font-size: 18px; 
 
    line-height: 24px; 
 
    float: right; 
 
    } 
 
}
<div class="clockWrap"> 
 
    <div class="timeWrapper"> 
 
    <div id="clockdiv"> 
 
     <h1 class="clock">Event will start in</h1> 
 
     <div class="day blue"> <span class="days"></span> 
 
     <div class="smalltext">Days</div> 
 
     </div> 
 
     <div class="orange"><span class="hours"></span> 
 
     <div class="smalltext">Hours</div> 
 
     </div> 
 
     <div class="colon grey"><span>:</span> 
 
     <div class="smalltext">&nbsp;</div> 
 
     </div> 
 
     <div class="green"><span class="minutes"></span> 
 
     <div class="smalltext">Minutes</div> 
 
     </div> 
 
     <div class="colon grey"> <span>:</span> 
 
     <div class="smalltext">&nbsp;</div> 
 
     </div> 
 
     <div class="red"><span class="seconds"></span> 
 
     <div class="smalltext">Seconds</div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

+0

は、なぜあなたは、このコードスニペット 'days_span.innerHTML = Tのようなアプローチを使用しないでください。日; '?ラベルのクラス名を指定し、t.days、t.hoursなどの値に基づいてinnerHTMLを設定します。 –

+0

代わりに、 'hours_span.setAttribute(" data-number "、( '0' + t)時間).slice(-2)); 'あなたのHTMLには'

Hour
'が含まれています。あなたのCSSは '.hours :: before {content:attr(data-number)} .hours〜.smalltext :: after {content: 's'} .hours [data-number = '1']〜.smalltext :: if(t.hours == 1){/ *後続の小文字にする* /} else {/ *何か他のことをする* /} ' –

答えて

2

あなたはこのような何かを行うことができます:

var eventDate = 'Jul 18 2017 20:24:18 GMT-0400'; 
 

 
function time_remaining(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 run_clock(id, endtime) { 
 
    var clock = document.getElementById(id); 
 

 
    // get spans where our clock numbers are held 
 
    var days_span = clock.querySelector('.days'); 
 
    var hours_span = clock.querySelector('.hours'); 
 
    var minutes_span = clock.querySelector('.minutes'); 
 
    var seconds_span = clock.querySelector('.seconds'); 
 

 
    var days_title = document.getElementById('days_title'); 
 
    var hours_title = document.getElementById('hours_title'); 
 
    var mins_title = document.getElementById('mins_title'); 
 
    var secs_title = document.getElementById('secs_title'); 
 

 
    function update_clock() { 
 
    var t = time_remaining(endtime); 
 

 
    // update the numbers in each part of the clock 
 
    days_span.innerHTML = t.days; 
 
    // Determine title for Day(s) 
 
    if (t.days === 1) { 
 
     days_title.innerHTML = 'Day'; 
 
    } else { 
 
     days_title.innerHTML = 'Days'; 
 
    } 
 
    // Determine title for Hour(s) 
 
    hours_span.innerHTML = ('0' + t.hours).slice(-2); 
 
    if (t.hours === 1) { 
 
     hours_title.innerHTML = 'Hour'; 
 
    } else { 
 
     hours_title.innerHTML = 'Hours'; 
 
    } 
 
    // Determine title for Min(s) 
 
    minutes_span.innerHTML = ('0' + t.minutes).slice(-2); 
 
    if (t.minutes === 1) { 
 
     mins_title.innerHTML = 'Minute'; 
 
    } else { 
 
     mins_title.innerHTML = 'Minutes'; 
 
    } 
 
    // Determine title for Sec(s) 
 
    seconds_span.innerHTML = ('0' + t.seconds).slice(-2); 
 
    if (t.seconds === 1) { 
 
     secs_title.innerHTML = 'Second'; 
 
    } else { 
 
     secs_title.innerHTML = 'Seconds'; 
 
    } 
 

 
    if (t.total <= 0) { 
 
     clearInterval(timeinterval); 
 
     document.getElementById("clockdiv").style.display = "none"; 
 
    } 
 

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

 
} 
 
run_clock('clockdiv', eventDate);
.clockWrap { 
 
    text-align: center; 
 
    font-weight: 700; 
 
    font-size: 50px; 
 
    max-width: 100%; 
 
} 
 

 
h1.clock { 
 
    color: #595959; 
 
    font-size: 30px; 
 
    line-height: 36px; 
 
    margin: 0; 
 
    padding: 50px 0 30px; 
 
} 
 

 
.day { 
 
    padding-right: 44px; 
 
} 
 

 
.colon { 
 
    font-size: 30px; 
 
    padding-right: 25px; 
 
    padding-left: 25px; 
 
    line-height: 60px; 
 
} 
 

 

 
/*--Colors--*/ 
 

 
.red { 
 
    color: #fc5852; 
 
} 
 

 
.blue { 
 
    color: #24a5f4; 
 
} 
 

 
.orange { 
 
    color: #ffaa00; 
 
} 
 

 
.grey { 
 
    color: #b3b3b3; 
 
} 
 

 
.green { 
 
    color: #35b134; 
 
} 
 

 
#clockdiv { 
 
    display: inline-block; 
 
    text-align: center; 
 
} 
 

 
#clockdiv>div { 
 
    padding-bottom: 50px; 
 
    display: inline-block; 
 
} 
 

 
.smalltext { 
 
    padding-top: 5px; 
 
    font-size: 18px; 
 
    line-height: 24px; 
 
    font-weight: 400; 
 
    text-transform: uppercase; 
 
} 
 

 
@media only screen and (max-width: 540px) { 
 
    .timeWrapper { 
 
    width: 167px; 
 
    margin: auto; 
 
    padding-bottom: 15px; 
 
    } 
 
    h1.clock { 
 
    font-size: 20px; 
 
    line-height: 36px; 
 
    padding: 18px 0 5px; 
 
    } 
 
    .day { 
 
    padding-right: 0; 
 
    } 
 
    .colon { 
 
    display: none !important; 
 
    } 
 
    #clockdiv { 
 
    text-align: left !important; 
 
    } 
 
    #clockdiv>div { 
 
    padding-bottom: 4px; 
 
    display: inline-block; 
 
    } 
 
    .smalltext { 
 
    padding-top: 19px; 
 
    padding-left: 15px; 
 
    font-size: 18px; 
 
    line-height: 24px; 
 
    float: right; 
 
    } 
 
}
<div class="clockWrap"> 
 
    <div class="timeWrapper"> 
 
    <div id="clockdiv"> 
 
     <h1 class="clock">Event will start in</h1> 
 
     <div class="day blue"> <span class="days"></span> 
 
     <div class="smalltext" id="days_title"></div> 
 
     </div> 
 
     <div class="orange"><span class="hours"></span> 
 
     <div class="smalltext" id="hours_title"></div> 
 
     </div> 
 
     <div class="colon grey"><span>:</span> 
 
     <div class="smalltext">&nbsp;</div> 
 
     </div> 
 
     <div class="green"><span class="minutes"></span> 
 
     <div class="smalltext" id="mins_title"></div> 
 
     </div> 
 
     <div class="colon grey"> <span>:</span> 
 
     <div class="smalltext">&nbsp;</div> 
 
     </div> 
 
     <div class="red"><span class="seconds"></span> 
 
     <div class="smalltext" id="secs_title"></div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

+3

" 0時間 "が間違っています。" 0時間 "にする必要があります。 –

+0

あなたは正しく編集されているので、単数対複数形を判別するために "1"をチェックします – Woodrow

+0

これは完璧です。まだ勉強してありがとう、助けに感謝します。 –

関連する問題