댓글 목록

응용 - 카운트다운 적용하기

페이지 정보

작성자 운영자 작성일 17-10-29 19:03 조회 1,819 댓글 0

동영상 강좌는 유튜브 채널 '웹학교'를 이용하시기 바랍니다.


운영중인 홈페이지에 카운트다운을 쉽게 달 수 있습니다.


기념일을 카운트한다든지, 공사중 기간을 카운트한다든지 할 때 화면에 재미있게 구현할 수 있습니다.


구글에서 검색하면 많은 공개 소스들이 나옵니다. 그 중에서 마음에 드는 것을 사용할 수도 있고,


제가 유튜브 '웹학교'를 통해 https://www.w3schools.com 공개강좌를 진행하고 있으니까 그곳에 있는 소스를 사용해 보도록 하겠습니다.


https://www.w3schools.com/howto/howto_js_countdown.asp 


적용방법은 간단합니다.


새 페이지를 만들고 적용할 곳에 소스를 그대로 적용하면 됩니다.

 

그 다음 CSS를 사용하여 화면에 맞게 디자인을 해 주면 됩니다.

<!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Sep 5, 2018 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now an the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>


위 24~25 라인을 아래와 같이 고쳐 주면 한글로 카운트됩니다.


document.getElementById("demo").innerHTML = days + "일 " + hours + "시간 "
+ minutes + "분 " + seconds + "초 남았습니다. ";


댓글목록 0

등록된 댓글이 없습니다.