javascript - 当倒计时到达一位数字时对齐数字元素

标签 javascript html css

这是一个简单的数字倒计时代码。除了一个烦人的小问题外,代码工作正常。

问题是当我们达到一位数字时,数字和秒字之间会出现额外的空格,例如

9 Seconds 而不是 9 Seconds 的预期结果。

我已经尝试在 CSS 和 HTML 代码中设置文本对齐方式,但似乎无法解决问题!

// get HTML ids we need
let counterUpperText = document.getElementById("counterUpperText");
let secondsUpperText = document.getElementById("secondsUpperText");


// create countdown number
let timeleft = 14;
  counterUpperText.innerHTML = `${timeleft}`;
  upperTextAnimeShow(counterUpperText);
  
let savedTimer = setInterval(function(){
  //console.log(timeleft);
  timeleft--;
  counterUpperText.innerHTML = `${timeleft}`;
  
  if(timeleft <= 0){
    clearInterval(savedTimer);
    console.log("done");
    secondsUpperText.innerHTML = ``;
    counterUpperText.innerHTML = `Done!`;
 
    return;
  }
  
}, 1000);

// create animations

    upperTextAnimeShow(counterUpperText);

    upperTextAnimeHide(secondsUpperText);
    upperTextAnimeShow(secondsUpperText);
    
    secondsUpperText.innerHTML = `Seconds`;



// animation functions
function upperTextAnimeShow(el){
     el.classList.add("upperTextAnimeShow");
     el.classList.remove("upperTextAnimeHide");
};

function upperTextAnimeHide(el){
     el.classList.remove("upperTextAnimeShow");
     el.classList.add("upperTextAnimeHide");
};
.upperTextContainer{
    position: absolute;
    overflow: hidden;
    left: 10vw;
    top: 40vh;
    height: 16vh;
    width: 29vw;   
    /*outline: 0.1vw dashed orange;*/
}

.upperTexts{
    position: absolute;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    font-size: 2vw;
    color: rgb(215, 215, 215);
    left: 0.4vw;
    opacity: 0;
   
}

#secondsUpperText{
    position: absolute;
    font-family: 'Open Sans', sans-serif;
    font-weight: normal;
    font-size: 2vw;
    color: rgb(215, 215, 215);
    left: 3.4vw;
    opacity: 0;

}

.upperTextAnimeShow {
    animation: upperTextShow 0.3s ease-in-out;
    animation-delay: 0.5s;
    animation-fill-mode: forwards ;
}

.upperTextAnimeHide {
    animation: upperTextHide 0.3s ease-in-out;  
    animation-fill-mode: forwards ;
}

@-webkit-keyframes upperTextShow {
  0%    { opacity: 0; top: 10vh }
  100%  { opacity: 1; top: -8vh }
}

@-webkit-keyframes upperTextHide {
   from { opacity: 1; top: -8vh }
   to   { opacity: 0; top:  10vh }
}
<div class = "upperTextContainer">
 <p id="counterUpperText" class="upperTexts" style="text-align: right;"></p>
 <p id="secondsUpperText" class="upperTexts"></p>
</div>

最佳答案

尝试添加这个..它的工作

CSS

.upperTextContainer{
    position: absolute;
    overflow: hidden;
    left: 10vw;
    top: 40vh;
    height: 16vh;
    width: 29vw;   
    display:flex;
    justify-content:center;
    /*outline: 0.1vw dashed orange;*/
}

.upperTexts{
    position: static;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    font-size: 2vw;
    color: rgb(215, 215, 215);
    left: 0.4vw;
    opacity: 0;

}

#secondsUpperText{
    position: static;
    font-family: 'Open Sans', sans-serif;
    font-weight: normal;
    font-size: 2vw;
    color: rgb(215, 215, 215);
    left: 3.4vw;
    opacity: 0;
    margin-left:3px;

}

.upperTextAnimeShow {
    animation: upperTextShow 0.3s ease-in-out;
    animation-delay: 0.5s;
    animation-fill-mode: forwards ;
}

.upperTextAnimeHide {
    animation: upperTextHide 0.3s ease-in-out;  
    animation-fill-mode: forwards ;
}

@-webkit-keyframes upperTextShow {
  0%    { opacity: 0; top: 10vh }
  100%  { opacity: 1; top: -8vh }
}

@-webkit-keyframes upperTextHide {
   from { opacity: 1; top: -8vh }
   to   { opacity: 0; top:  10vh }
}

关于javascript - 当倒计时到达一位数字时对齐数字元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59191375/

相关文章:

html - 使用 <h :outputStylesheet> makes <p:outputLabel> component invisible

html - 在 IE6/7 中使用 CSS 将表格列中的内容居中

html - 为什么我在这个网站上的移动菜单在浏览器上有效,但在实际移动设备上却无效?

html - 想要 CSS 属性 : line separator above heading

javascript - 我如何使用 Mongoose 和 Koa.js

javascript - 当我在 Javascript 中创建元素时,边距不适用

javascript - JavaScript 中的国际象棋游戏

javascript - 如何从另一个对象调用一个对象的方法?

javascript - 当我增加字体大小时 div 边框大小也在增加

css - 修复了 100% 宽度的位置问题