javascript - 如果文本比其容器长,则降低字体大小直到文本适合

标签 javascript html css

我们有一个包含动画文本的容器。

我们为文本定义了一个默认字体大小,但如果文本超过容器的宽度,我们希望减小字体大小,以便文本无论多长都可以在一行中适合容器。

到目前为止,这是我的代码:

//The Text That would be fit into the container
let mytext = 'I was sent to earth to protect you!';

//The function to determine mytext width
function getTextWidth(text, font) {
      var canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
      var context = canvas.getContext("2d");
      context.font = font;
      var metrics = context.measureText(text);
      return metrics.width;
      };

      //width of the upperGuide container
      let width = document.getElementsByClassName("upperGuideContainer")[0].clientWidth;

      let text_Length;
      let default_size = 5.6;
   
      /* Keep lowering the default font size till the text_Length is smaller than
        its container width So that the text can fit in the container */
      do {
          text_Length = getTextWidth(mytext, `bold ${default_size}vw Open Sans`).toFixed(2);
          //console.log("length inside: " + text_Length);
          default_size -= 0.01;
          //console.log("default_size inside: " + default_size);

      }
      while (text_Length > width);
      
  setTimeout(function(){
      //modify the font size based on text length
      upperGuideText.style.fontSize = `${default_size}vw`;

      //Append and animate the mytext
      upperGuideText.innerHTML = `${mytext}`;
      upperGuideText.classList.add("upperGuideAnimeShow");
      upperGuideText.classList.remove("upperGuideAnimeHide");
      }, 500);      
.upperGuideContainer {
    position: absolute;
    overflow: hidden;
    left: 10vw;
    top: 48.5vh;
    height: 26vh;
    width: 82vw;   
    outline: 0.1vw dashed orange;
}

.upperGuide {
    position: absolute;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    color: rgb(128, 128, 128);
    left: 0.5vw;
    opacity: 0;
    margin: 0;
    top: 0;
    bottom: -16vh;
    display: flex;
    align-items: center;
}

/*Animations*/

.upperGuideAnimeShow {
    animation: upperGuideShow 0.3s ease-in-out;
    animation-delay: 0.1s;
    animation-fill-mode: forwards ;
}

.upperGuideAnimeHide {
    animation: upperGuideHide 0.3s ease-in-out;  
    animation-fill-mode: forwards ;
}

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

@-webkit-keyframes upperGuideHide {
   from { opacity: 1; top: -16vh }
   to   { opacity: 0; top:  10vh }
}
<div class = "upperGuideContainer">
 <p id="upperGuideText" class="upperGuide"></p>
</div>

代码有效,当 mytext 比容器长时,我似乎可以减小字体大小,但由于某种原因,它不能充分减小字体大小,我们有两行句子不适合容器。

注意:我想要容器中可以容纳的最大可能字体大小...所以我需要完整的default_size -= 0.01;

最佳答案

我更改了 default_size -= 1.0;

//The Text That would be fit into the container
let mytext = 'I was sent to earth to protect you!';

//The function to determine mytext width
function getTextWidth(text, font) {
      var canvas = getTextWidth.canvas || (getTextWidth.canvas = document.createElement("canvas"));
      var context = canvas.getContext("2d");
      context.font = font;
      var metrics = context.measureText(text);
      return metrics.width;
      };

      //width of the upperGuide container
      let width = document.getElementsByClassName("upperGuideContainer")[0].clientWidth;

      let text_Length;
      let default_size = 5.6;
   
      /* Keep lowering the default font size till the text_Length is smaller than
        its container width So that the text can fit in the container */
      do {
          text_Length = getTextWidth(mytext, `bold ${default_size}vw Open Sans`).toFixed(2);
          //console.log("length inside: " + text_Length);
          default_size -= 1.0;
          //console.log("default_size inside: " + default_size);

      }
      while (text_Length > width);
      
  setTimeout(function(){
      //modify the font size based on text length
      upperGuideText.style.fontSize = `${default_size}vw`;

      //Append and animate the mytext
      upperGuideText.innerHTML = `${mytext}`;
      upperGuideText.classList.add("upperGuideAnimeShow");
      upperGuideText.classList.remove("upperGuideAnimeHide");
      }, 500);      
.upperGuideContainer {
    position: absolute;
    overflow: hidden;
    left: 10vw;
    top: 48.5vh;
    height: 26vh;
    width: 82vw;   
    outline: 0.1vw dashed orange;
}

.upperGuide {
    position: absolute;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    color: rgb(128, 128, 128);
    left: 0.5vw;
    opacity: 0;
    margin: 0;
    top: 0;
    bottom: -16vh;
    display: flex;
    align-items: center;
}

/*Animations*/

.upperGuideAnimeShow {
    animation: upperGuideShow 0.3s ease-in-out;
    animation-delay: 0.1s;
    animation-fill-mode: forwards ;
}

.upperGuideAnimeHide {
    animation: upperGuideHide 0.3s ease-in-out;  
    animation-fill-mode: forwards ;
}

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

@-webkit-keyframes upperGuideHide {
   from { opacity: 1; top: -16vh }
   to   { opacity: 0; top:  10vh }
}
<div class = "upperGuideContainer">
 <p id="upperGuideText" class="upperGuide"></p>
</div>

关于javascript - 如果文本比其容器长,则降低字体大小直到文本适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59230640/

相关文章:

html - 水平显示元素而不是列

javascript - jquery addclass 和 hasclass 与 animate.css

javascript - Ember CLI : Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode

javascript - 是否可以严格在 Visual Studio 中使用 PhoneGap 构建 android 应用程序?

javascript - 根据在其中单击的链接查找嵌套 ul 的子项

javascript - 具有复制到剪贴板纯 JS 的自定义选择功能

javascript - 用于事件的参数是什么?我怎么知道?

javascript - 使用javascript在浏览器调整大小时更改背景css

html - CSS - 页面自适应内容高度

css - 将一个 div 从固定宽度的 div 中分离出来,变成 100% 宽