【问题标题】:Change font size by container按容器更改字体大小
【发布时间】:2016-09-01 22:31:12
【问题描述】:

我尝试使用此站点上以前的帖子并在 google 中搜索,但我遗漏了一些东西。

var divContainer = ws_popupWindows.document.getElementById('sw_content');

divContainer.innerHTML = '<div id="sw_resize">' + msgTEXT.innerHTML + '</div>';

var spanContainer = ws_popupWindows.document.getElementById('sw_resize');
for (var newSize = 100; (newSize -= 10) && (divContainer.clientWidth < spanContainer.clientWidth);) {
  spanContainer.style.fontSize = newSize + '%';
}
body {
  width: 100%;
  direction: rtl;
  text-align: center;
  background-color: AntiqueWhite;
  font-size: 15vmin;
  margin: 0;
  padding: 0;
}
h1 {
  height: 20vh;
  margin: 1vh;
  text-align: right;
  font-size: 100%
}
div {
  max-height: 70vh;
  background-color: Snow;
  margin: 0 5vh;
  padding: 2vh;
  border-radius: 10vh;
  overflow: hidden;
  word-wrap: break-word;
}
<div id="sw_content">
  <div id="sw_resize">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
    in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
</div>

但它不会改变文本的大小。 (我不能使用 jQuery,但在纯 JS 中也不应该这么难)。

附注: 我知道Dynamically resize font size to fit container size之类的帖子,但它仍然不起作用。

【问题讨论】:

    标签: javascript css font-size viewport-units


    【解决方案1】:

    使用 spanContainer 的滚动高度与它的 clientHeight。

    for (var newSize = 100; spanContainer.scrollHeight > spanContainer.clientHeight; newSize -= 10)
    {
      spanContainer.style.fontSize = newSize + '%';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多