【发布时间】:2012-05-14 00:07:23
【问题描述】:
我正在尝试调整字体大小以填充容器。仅这一点显然不是问题。但是,容器也不是静态大小,它的大小设置为浏览器窗口的百分比,我希望字体大小与容器一起在浏览器调整大小时动态更新。
我一直在使用我发现的修改后的脚本,该脚本将字体大小调整为浏览器高度和宽度的 %。
$( document ).ready( function() {
var $body = $('body'); //Cache this for performance
var setBodyScale = function() {
var scaleFactor = 0.0001,
scaleSource = $(window).height(),
scaleSource2 = $(window).width(),
maxScale = 200,
minScale = 10;
var fontSize = (scaleSource * scaleSource2) * scaleFactor; //Multiply the width of the body by the scaling factor:
if (fontSize > maxScale) fontSize = maxScale;
if (fontSize < minScale) fontSize = minScale; //Enforce the minimum and maximums
$('body').css('font-size', fontSize + '%');
}
$(window).resize(function(){
setBodyScale();
});
//Fire it when the page first loads:
setBodyScale();
});
我没有得到想要的结果,所以尝试改变它,使缩放源是容器而不是窗口。这有点工作,但它没有动态更新,它需要刷新页面。
简而言之,我找不到调整字体大小、填充大小由浏览器窗口百分比决定的容器的方法。
【问题讨论】:
标签: javascript html css dynamic font-size