1

Нужно с помощью js сделать обратный отсчет числа.

Верстка прилагается.

Есть идея перебрать дивы через querySelectorAll, отобрать там только числа и начать вычитать с помощью setInterval, но когда я это делаю, сбрасываются все стили.

let interval = setInterval(function() {
  let cp2 = parseInt(document.querySelector('.countdown-box').textContent);
  if (cp2 > 1) {
    document.querySelector('.countdown-box').textContent = cp2 - 1;
    console.log(cp2);
  } else {
    clearInterval(interval);
  }
}, 1000);
.countdown-box {
  width: 200px;
  height: 70px;
  display: inline-flex;
}

.num-first,
.num-second,
.num-third {
  font-size: 36px;
  border: 2px solid red;
  border-radius: 10px;
  background-color: #fff;
  width: 83.333333px;
  height: 70px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  color: red;
}

.num-first,
.num-second {
  margin-right: 10px;
}
<div class="countdown-box">
  <div class="num-first">9</div>
  <div class="num-second">9</div>
  <div class="num-third">9</div>
</div>

2
  • добавь свой вариант с setInterval
    – Grundy
    4 дек 2020 в 20:01
  • <script> let interval = setInterval(function() { let cp2 = parseInt(document.querySelector('.countdown-box').textContent); if (cp2 > 1) { document.querySelector('.countdown-box').textContent = cp2 - 1; console.log(cp2); } else { clearInterval(interval); } }, 1000); </script> вот мой вариант , но как только отсчет начинается , все стили слетают 4 дек 2020 в 20:06

2 ответа 2

2

let n1 = document.querySelector('.num-first')
let n2 = document.querySelector('.num-second')
let n3 = document.querySelector('.num-third')

let counter = 111

timer_id = setInterval(() => {
  counter--
  n1.innerText = Math.floor(counter/100)
  n2.innerText = Math.floor(counter%100/10)
  n3.innerText = counter%10
  if (counter == 0) clearInterval(timer_id)
}, 1000)
.countdown-box {
  width: 200px;
  height: 70px;
  display: inline-flex;
}

.num-first,
.num-second,
.num-third {
  font-size: 36px;
  border: 2px solid red;
  border-radius: 10px;
  background-color: #fff;
  width: 83.333333px;
  height: 70px;
  display: flex;
  justify-content: space-around;
  align-items: center;
  color: red;
}

.num-first,
.num-second {
  margin-right: 10px;
}
<div class="countdown-box">
  <div class="num-first">1</div>
  <div class="num-second">1</div>
  <div class="num-third">1</div>
</div>

1
  • Спасибо большое ! Это решение прямо в точку . 4 дек 2020 в 20:21
0

Что-то вроде такого?

    function startNumericCountdown(tick) {
        var counter = Tick.count.down(999, 'seconds');
        counter.onupdate = function(value) {
            // value will contain amount of seconds left
            tick.value = value;
        };
        counter.onended = function() {
            // reached zero!
        }
    }
.tick {
  font-size: 5rem;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
}

.tick-label {
  font-size: .375em;
  text-align: center;
}

.tick-group {
  margin: 0 .25em;
  text-align: center;
}

.tick {
  box-sizing: border-box;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  cursor: default;
  position: relative;
  z-index: 1;
  line-height: 1.4
}

.tick * {
  box-sizing: inherit
}

.tick [data-view] {
  max-width: 100%
}

.tick span[data-view] {
  display: inline-block
}

.tick [data-layout~=pad] {
  margin: -.25em
}

.tick [data-layout~=pad]>* {
  margin: .25em
}

.tick [data-layout~=horizontal] {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: row;
  flex-direction: row;
  -ms-flex-pack: center;
  justify-content: center
}

.tick [data-layout~=horizontal][data-layout~=baseline] {
  -ms-flex-align: baseline;
  align-items: baseline
}

.tick [data-layout~=horizontal][data-layout~=center] {
  -ms-flex-pack: center;
  justify-content: center
}

.tick [data-layout~=horizontal][data-layout~=right] {
  -ms-flex-pack: end;
  justify-content: flex-end
}

.tick [data-layout~=horizontal][data-layout~=left] {
  -ms-flex-pack: start;
  justify-content: flex-start
}

.tick [data-layout~=horizontal][data-layout~=fill],
.tick [data-layout~=horizontal][data-layout~=stretch] {
  -ms-flex-line-pack: stretch;
  align-content: stretch;
  -ms-flex-wrap: nowrap;
  flex-wrap: nowrap
}

.tick [data-layout~=horizontal][data-layout~=fill]>*,
.tick [data-layout~=horizontal][data-layout~=stretch]>* {
  -ms-flex: 1 0 0px;
  flex: 1 0 0;
  width: 100%
}

.tick [data-layout~=horizontal][data-layout~=multi-line] {
  -ms-flex-wrap: wrap;
  flex-wrap: wrap
}

.tick [data-layout~=horizontal][data-layout~=fit] {
  display: -ms-inline-flexbox;
  display: inline-flex;
  -ms-flex-wrap: nowrap;
  flex-wrap: nowrap;
  -ms-flex-line-pack: center;
  align-content: center;
  white-space: nowrap;
  -ms-flex-pack: start;
  justify-content: flex-start
}

.tick [data-layout~=vertical] {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-direction: column;
  flex-direction: column;
  -ms-flex-align: center;
  align-items: center
}

.tick [data-layout~=vertical][data-layout~=top] {
  -ms-flex-pack: start;
  justify-content: flex-start
}

.tick [data-layout~=vertical][data-layout~=bottom] {
  -ms-flex-pack: end;
  justify-content: flex-end;
  min-height: 100%
}

.tick [data-layout~=vertical][data-layout~=middle] {
  -ms-flex-pack: center;
  justify-content: center;
  min-height: 100%
}

.tick [data-layout~=vertical][data-layout~=left] {
  -ms-flex-align: start;
  align-items: flex-start
}

.tick [data-layout~=vertical][data-layout~=right] {
  -ms-flex-align: end;
  align-items: flex-end
}

.tick [data-layout~=vertical][data-layout~=center] {
  text-align: center
}

.tick [data-layout~=vertical][data-layout~=fill],
.tick [data-layout~=vertical][data-layout~=stretch] {
  -ms-flex-align: stretch;
  align-items: stretch;
  min-height: 100%
}

.tick [data-layout~=vertical][data-layout~=fill]>*,
.tick [data-layout~=vertical][data-layout~=stretch]>* {
  -ms-flex: 1 0 0px;
  flex: 1 0 0
}

.tick [data-layout~=vertical]>*+* {
  margin-top: .5em
}

.tick [data-layout~=overlay] {
  position: relative
}

.tick [data-layout~=overlay]>* {
  margin: 0
}

.tick [data-layout~=overlay][data-layout~=center] {
  text-align: center
}

.tick [data-layout~=overlay][data-layout~=left] {
  text-align: left
}

.tick [data-layout~=overlay][data-layout~=right] {
  text-align: right
}

.tick [data-layout~=overlay]>[data-overlay=fill],
.tick [data-layout~=overlay]>[data-overlay=stretch] {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0
}

.tick [data-layout~=overlay]>[data-overlay=center] {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-pack: center;
  justify-content: center;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0
}

.tick-flip {
  position: relative;
  text-align: center
}

.tick-flip * {
  border-radius: inherit;
  white-space: pre;
  letter-spacing: inherit;
  text-indent: inherit
}

.tick-flip-front {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0
}

.tick-flip-back {
  border-top-left-radius: 0;
  border-top-right-radius: 0
}

.tick-flip-spacer {
  display: block;
  visibility: hidden
}

.tick-flip-shadow {
  position: absolute;
  left: 1px;
  right: 1px;
  top: 1px;
  bottom: 1px;
  color: transparent!important;
  background: transparent!important
}

.tick-flip-shadow-top {
  bottom: calc(50% - 1px)
}

.tick-flip-shadow-bottom {
  top: calc(50% + 1px)
}

.tick-flip-card-shadow {
  position: absolute;
  left: .15em;
  right: .15em;
  bottom: .125em;
  height: .5em;
  background-color: transparent;
  border-radius: 0;
  opacity: 0;
  transform-origin: 0 100%;
  box-shadow: 0 .125em .25em rgba(0, 0, 0, .5), 0 .125em .5em rgba(0, 0, 0, .75);
  z-index: 0
}

.tick-flip-card {
  position: absolute;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  perspective: 4em
}

.tick-flip-panel-back,
.tick-flip-panel-front {
  position: absolute;
  left: 0;
  width: 100%;
  height: 51%;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  transform-style: preserve-3d
}

.tick-flip-panel-back-text,
.tick-flip-panel-front-text {
  position: absolute;
  left: -1px;
  top: 0;
  right: -1px;
  height: 100%;
  overflow: hidden
}

.tick-flip-panel-text-wrapper {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  height: 100%
}

.tick-flip-panel-back-text .tick-flip-panel-text-wrapper {
  height: 200%;
  top: -100%
}

.tick-flip-panel-front {
  transform-origin: center bottom;
  top: 0;
  z-index: 2;
  box-shadow: inset 0 1px hsla(0, 0%, 100%, .05)
}

.tick-flip-panel-back {
  transform-origin: center top;
  top: 50%;
  z-index: 1;
  box-shadow: inset 0 -1px rgba(0, 0, 0, .1)
}

.tick-flip-panel-back:after {
  z-index: 1;
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(180deg, rgba(0, 0, 0, .3) 1px, rgba(0, 0, 0, .15) 0, transparent 30%)
}

.tick-flip-panel-back-shadow {
  z-index: 2
}

.tick-flip-panel-back-highlight {
  z-index: 3
}

.tick-flip-panel-back-highlight,
.tick-flip-panel-back-shadow {
  border-bottom-left-radius: inherit;
  border-bottom-right-radius: inherit
}

.tick-flip-panel-back-highlight,
.tick-flip-panel-back-shadow,
.tick-flip-panel-front-shadow {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  opacity: 0
}

.tick-flip-panel-front-shadow {
  background-image: linear-gradient(0deg, rgba(0, 0, 0, .8), rgba(0, 0, 0, .3))
}

.tick-flip-panel-back-shadow {
  background-image: linear-gradient(180deg, rgba(0, 0, 0, .7), rgba(0, 0, 0, .5))
}

.tick-flip-panel-back-highlight {
  background-image: linear-gradient(180deg, hsla(0, 0%, 100%, .15), hsla(0, 0%, 100%, .3))
}

.tick [data-style*="shadow:inner"],
.tick [data-style*="shadow:inner"] .tick-flip-card-shadow,
.tick [data-style*="shadow:none"] .tick-flip-card-shadow,
.tick [data-style*="shadow:none"] .tick-flip-panel-back,
.tick [data-style*="shadow:none"] .tick-flip-panel-front,
.tick [data-style*="shadow:none"] .tick-flip-shadow {
  box-shadow: none
}

.tick [data-style*="shadow:none"] .tick-flip-back:after,
.tick [data-style*="shadow:none"] .tick-flip-panel-back-shadow,
.tick [data-style*="shadow:none"] .tick-flip-panel-back-text:after,
.tick [data-style*="shadow:none"] .tick-flip-panel-front-shadow {
  background-image: none
}

.tick [data-style*="rounded:none"] {
  border-radius: 0
}

.tick [data-style*="rounded:panels"] .tick-flip-front,
.tick [data-style*="rounded:panels"] .tick-flip-shadow-bottom {
  border-bottom-left-radius: inherit;
  border-bottom-right-radius: inherit
}

.tick [data-style*="rounded:panels"] .tick-flip-back,
.tick [data-style*="rounded:panels"] .tick-flip-panel-back:after,
.tick [data-style*="rounded:panels"] .tick-flip-shadow-top {
  border-top-left-radius: inherit;
  border-top-right-radius: inherit
}

.tick-flip {
  margin-left: .0625em;
  margin-right: .0625em;
  min-width: 1.125em;
  border-radius: .125em;
  letter-spacing: .25em;
  text-indent: .25em
}

.tick-flip-panel {
  color: #edebeb;
  background-color: #333232
}

.tick-flip-shadow {
  box-shadow: 0 .125em .3125em rgba(0, 0, 0, .25), 0 .02125em .06125em rgba(0, 0, 0, .25)
}
<script src="https://www.cssscript.com/demo/flip-clock-countdown-counter/flip/flip.min.js"></script>



<div class="tick"
     data-did-init="startNumericCountdown">
    <div class="tick-group">
    <div data-key="value" data-repeat="true" data-transform="pad(000) -> split -> delay">
    <span data-view="flip"></span>
    </div>
    </div>
</div>

Подробнее о счетчиках тут: https://www.cssscript.com/flip-clock-countdown-counter/

1
  • Спасибо большое , похоже на то что я искал , разве что стили поправить ! Дай Бог тебе здоровья , добрый человек ))) 4 дек 2020 в 20:08

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.