[英]Set font size according to container Width and Height
I'm trying to write a script so that font-size
can be set according to container's width, height and the length of the text. 我正在尝试编写一个脚本,以便可以根据容器的宽度,高度和文本的长度来设置
font-size
。
That's what I've done so far. 到目前为止,这就是我所做的。
window.onload = function () {
var defaultDimensions = 300;
var boxWidth = window.innerWidth / Math.floor(window.innerWidth / defaultDimensions);
var boxes = document.getElementsByClassName('feed');
for (var i = 0; i < boxes.length; i++) {
boxes[i].style.width = boxWidth + 'px';
boxes[i].style.height = boxWidth + 'px';
}
};
var getFontSizeStatus = function (text) {
var chars = 250; // Maximum Characters
var height = 300; // Default Height
if (text.length < chars) {
chars = text.length;
}
if ((window.innerWidth / Math.floor(window.innerWidth / height)) < height) {
height = window.innerWidth / Math.floor(window.innerWidth / height);
}
height = (height / 10) * 8; // .feed-text height is 80% of .feed's height
var size = height / chars;
return size + 'em';
}
Here is the complete code, fiddle. 这是完整的代码, 小提琴。
Current code is working but font-size to container's height, width and text's lengths ratio needs to be fixed. 当前代码正在运行,但是字体大小与容器的高度,宽度和文本的长度之比需要固定。
I would try using a jQuery plugin like fitText 我会尝试使用像fitText这样的jQuery插件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.