我需要制作适合窗口大小的动态文本,但我有问题,关键是例如 xxx xxx xxx xxx xxx 大于 iii iii iii iii iii,我不知道该怎么做,这是我的代码
var p = document.getElementById('ptext');
var h1 = document.getElementById('h1text');
h1.setFont = function (font) {
var size = this.offsetWidth,
font_size = size * font;
this.style.fontSize = font_size + '%';
return this
};
p.setFont = function (font) {
var size = this.offsetWidth,
font_size = size * font;
this.style.fontSize = font_size + '%';
return this
};
let h1length = h1.innerText.length;
let plength = p.innerText.length;
let h1Size = 10 / h1length
let pSize = 10 / plength
h1.setFont(h1Size);
p.setFont(pSize)
window.onresize = function () {
h1.setFont(h1Size);
p.setFont(pSize)
}