简体   繁体   English

如何将font-size设置为div大小的100%?

[英]How to set the font-size to 100% the size of a div?

I have a div with a static size. 我有一个静态尺寸的div。 Sometimes longer text than the div will be placed there. 有时,比div长的文本将被放置在那里。 Is there anyway to achieve the text fitting the div width at all times through JavaScript alone ? 无论如何,是否通过JavaScript就能始终实现适合div宽度的文本? I am aware there are jQuery fixes but need a pure JS solution in this case. 我知道有jQuery修复程序,但在这种情况下需要纯JS解决方案。

Even if you have a link to a demo/tutorial that would be helpful, thanks. 即使您有指向演示/教程的链接也可能会有所帮助,谢谢。

Here you go, this should do what you want: JSFiddle 到这里,这应该做您想要的: JSFiddle

Basically the key here is to check the output.scrollHeight against output.height . 基本上,这里的关键是对照output.height检查output.scrollHeight Consider the following setup: 请考虑以下设置:

HTML: HTML:

<button onclick="addText(); resizeFont()">Click Me to Add Some Text!</button>
<div id="output"></div>

CSS: CSS:

div {
    width: 160px;
    height: 160px;
}

This creates a square div and fills it with a randomly long string of text via the addText() method. 这将创建一个Square div,并通过addText()方法将其随机长字符串填充。

function addText() {
    var text   = "Lorem ipsum dolor amit",
        len    = Math.floor(Math.random() * 15),
        output = document.querySelector('#output'),
        str    = [];
    for (; --len;)
        str.push(text);

    output.innerHTML = str.join(', ');
}

The magic lies in the resizeFont() function. 魔术在于resizeFont()函数。 Basically what this does is, once the text has been added, it sets the fontSize of the div to be equal to its own height. 基本上,这样做是在添加文本之后,将divfontSize设置为其自己的高度。 This is the base case for when you have a string of 1 character (ie the fontSize will equal the height). 这是当您具有1个字符的字符串(即fontSize将等于高度)时的基本情况。 As the length of your string grows, the fontSize will need to be made smaller until the scrollHeight equals the height of the div 随着字符串长度的增加,需要将fontSize减小,直到scrollHeight等于div的高度。

function resizeFont() {
    var output   = document.querySelector('#output'),
        numRE    = /(\d+)px/,
        height   = numRE.exec(window.getComputedStyle(output).height)[1],
        fontSize = height;

    // allow div to be empty without entering infinite loop
    if (!output.innerHTML.length) return;

    // set the initial font size to the height of the div
    output.style.fontSize = fontSize + 'px';

    // decrease the font size until the scrollHeight == height
    while (output.scrollHeight > height)
      output.style.fontSize = --fontSize + 'px';
}

The nice thing about this method is that you can easily attach an event listener to the resize event of the window, making the text dynamically resize as the user changes the window size: http://jsfiddle.net/QvDy8/2/ 关于此方法的好处是,您可以轻松地将事件侦听器附加到窗口的resize事件,使文本随着用户更改窗口大小而动态地调整大小: http : //jsfiddle.net/QvDy8/2/

window.onload = function() {
  window.addEventListener('resize', resizeFont, false);
}

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

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