简体   繁体   English

Javascript:如何获得浏览器的最小字体大小?

[英]Javascript: How can I get a browser's minimum font size?

What is the best way to get a browser's minimum font size?获得浏览器最小字体大小的最佳方法是什么? I whipped up the following code, which does a binary search.我编写了以下代码,它进行了二进制搜索。 It works, but I would like to know if there is a standard or more efficient method.它有效,但我想知道是否有标准或更有效的方法。

function getMinimumFontSize() {
  var el = document.createElement('div');
  document.body.appendChild(el);
  el.innerHTML = "<div><p>a b c d e f g h i j k l m n o p q r s t u v w x y z</p></div>";
  el.style.fontSize = '1px';
  el.style.width = '64px';
  var minimumHeight = el.offsetHeight;
  var least = 0;
  var most = 64;
  var middle; 
  for (var i = 0; i < 32; ++i) {
    middle = (least + most)/2;
    el.style.fontSize = middle + 'px';
    if (e.offsetHeight === minimumHeight) {
      least = middle;
    } else {
      most = middle;
    }
  }
  return middle;
}

Just setting the style and reading it back does not work, because it will return the setting rather than what the browser is actually using.仅仅设置样式并读取它是行不通的,因为它将返回设置而不是浏览器实际使用的内容。

I need the minimum font size so that I can dynamically re-size content to keep everything on one page, without scrolling.我需要最小字体大小,以便我可以动态地重新调整内容大小以将所有内容保留在一页上,而无需滚动。 That is hard to do if the browser ignores my font size changes because I have gone below the minimum.如果浏览器忽略我的字体大小更改,这很难做到,因为我已经低于最小值。 So for that situation, I will shrink other content instead, such as graphics.因此,对于这种情况,我将缩小其他内容,例如图形。

I did it this way: 我是这样做的:

var ta = document.createElement('textarea');
ta.style.display = 'none';
ta.style.fontSize = '6px'; // I think this is the smallest possible, if not put smaller value
document.body.appendChild(ta); // needs to be part of DOM to be able to calculate
var minimumFontSize = window.getComputedStyle(ta, null).getPropertyValue('font-size'); // returns '20px' for me when I set minimum font size to 20px

EDIT: this doesn't work on firefox, so I created a new, slightly different method: 编辑:这不适用于Firefox,所以我创建了一个新的,略有不同的方法:

var ta = document.createElement('div');
ta.style.fontSize = '6px';
ta.style.lineHeight = '1';
ta.innerHTML = 'a';
document.body.appendChild(ta);
var minimumFontSize = ta.clientHeight;

6 years later, and a minimum font-size of zero is allowed in both Chrome and Firefox. 6年后,Chrome和Firefox都允许最小字体大小为零。 This is my basic approach, similar to the first example in the first answer here: 这是我的基本方法,类似于第一个答案中的第一个例子:

element.style.fontSize = 0;
let actualMinSize = getComputedStyle(element).fontSize;

You must strip the "px" off the computed result to get a number, and the element must be displayed, but it can be off screen via position:absolute; 您必须从计算结果中删除“px”以获取数字,并且必须显示该元素,但它可以通过position:absolute; This seems to work in both Chrome and Firefox now, so there is no need for the clientHeight workaround AFAICT. 这似乎适用于Chrome和Firefox,因此不需要clientHeight解决方法AFAICT。

Earlier this year, in Chrome 73, there was an issue with root em space sizing and the minimum font size setting in Chrome. 今年早些时候,在Chrome 73中,root em空间大小和Chrome中的最小字体大小设置存在问题。 Since then, Chrome changed their minimum font-size policy and user interface. 从那时起,Chrome改变了他们的最小字体大小政策和用户界面。 They now support a zero minimum value, as does Firefox. 他们现在支持零最小值,Firefox也是如此。

My app has a user-sizable window that scrolls text vertically, and the width of the element is set indirectly via the font-size, so that a wider element equals a bigger font size. 我的应用程序有一个用户大小的窗口,可以垂直滚动文本,元素的宽度通过font-size间接设置,因此更宽的元素等于更大的字体大小。 If a user tries to size the window below the configured min font size, I need to handle that as a minimum element width. 如果用户试图在配置的最小字体大小下面调整窗口大小,我需要将其作为最小元素宽度处理。

Though I can set the font-size to zero for an element, font sizes below 3px are problematic because the smaller the font size, the less proportional it is to the text width, among other reasons I haven't fully debugged yet. 虽然我可以为一个元素设置font-size为零,但是字体大小低于3px是有问题的,因为字体大小越小,它与文本宽度的比例越小,以及我尚未完全调试的其他原因。 At a certain point, a requested 1px reduction in width allows me to change the font size, but the <div> clientWidth freezes: changing the font-size has no effect on the width. 在某个时刻,请求的1px宽度减少允许我更改字体大小,但<div> clientWidth冻结:更改font-size对宽度没有影响。 A 2px reduction is OK, because the change is bigger, or so it seems. 减少2px是可以的,因为变化更大,或者看起来如此。 I can set the style.width to the desired value in the Watch pane in the debugger, and that gets me out of an endless loop. 我可以在调试器的Watch窗格中将style.width设置为所需的值,这使我无法循环。

The strangest part is that I can cause this problem at different font sizes/widths, there is no precise tipping point. 最奇怪的是我可以在不同的字体大小/宽度下引起这个问题,没有精确的引爆点。 It's more of a range in which small changes are problematic. 它更像是一个小变化有问题的范围。 It might be a Chrome bug, but sizes below 3px seem unlikely to me anyway. 它可能是一个Chrome错误,但无论如何我看起来不太可能是3px以下。 I might enforce my own minimum of 3px in this element, to simply sidestep these problems. 我可以在这个元素中强制执行我自己的最小3px,以简单地回避这些问题。

My problem occurs in the following browsers: QQBrowser *¹ and Sogou Explorer *².我的问题出现在以下浏览器中:QQBrowser *¹ 和搜狗资源管理器 *²。

If you insert or change any element to a font below 9px it ignores and keeps the 9px.如果您将任何元素插入或更改为 9px 以下的字体,它会忽略并保留 9px。

Maybe my problem is a little different from the ones mentioned, but I solved it with this polyfill:也许我的问题与上面提到的有点不同,但我用这个 polyfill 解决了它:

// get-min-visible-font-size.js v1
function getMinVisibleFontSize() {
    var spanFS = document.createElement('span');
    spanFS.setAttribute('style', 'font-size:1px!important;display:none!important;');
    document.body.appendChild(spanFS);
    var minFontSize = parseInt(getComputedStyle(spanFS).fontSize);
    document.body.removeChild(spanFS);
    return minFontSize;
};

I chose to return an integer value because it makes testing later easier:我选择返回一个整数值,因为它使以后的测试更容易:

if( getMinVisibleFontSize() > 6){
    [...]
};

But all returned values ​​are actually in pixels.但是所有的返回值实际上都是以像素为单位的。

How it works : simply spam it with font-size = 1px, add it to the body and check the font-size size.它是如何工作的:只需使用 font-size = 1px 发送垃圾邮件,将其添加到正文并检查字体大小。 If it's larger than 1px then you're in a browser that limits the minimum font size (9px OR 12px in these cases).如果它大于 1px,那么您所在的浏览器会限制最小字体大小(在这些情况下为 9px 或 12px)。 It is important that the value is not zero because if you check zero in all cases the text disappears and you get zero even in these browsers.重要的是该值不为零,因为如果在所有情况下都检查零,则文本消失并且即使在这些浏览器中也会得到零。

It may be that in the future there is some CSS to define min-font-size as well as min-width, so I put the function name as minVisibleFontSize.可能是以后有一些CSS定义了min-font-size和min-width,所以我把函数名设为minVisibleFontSize。

Works on IE-11 but I didn't do it for old browsers...适用于 IE-11,但我没有为旧浏览器执行此操作...

¹ = QQBrowser v10.8.3, based on Chromium 70.0.3538.25. ¹ = QQBrowser v10.8.3,基于 Chromium 70.0.3538.25。 (9px) ² = Sogou Explorer v11.0.1.34700, based on Chromium 80.0.3987.87 I think because it's in userAgent. (9px) ² = Sogou Explorer v11.0.1.34700,基于 Chromium 80.0.3987.87 我认为是因为它在 userAgent 中。 (12px) (12 像素)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM