这有点像是一种hack,但可以实现你想要的功能。
<div id="hidden-resizer" style="visibility: hidden"></div>
将此代码放置在页面底部,以确保它不会影响页面上的其他元素。
然后执行以下操作:
var size;
var desired_width = 50;
var resizer = $("#hidden-resizer");
resizer.html("This is the text I want to resize.");
while(resizer.width() > desired_width) {
size = parseInt(resizer.css("font-size"), 10);
resizer.css("font-size", size - 1);
}
$("#target-location").css("font-size", size).html(resizer.html());
#hidden-resizer
div,以避免弄乱HTML的语义。$('<div />', {id: 'hidden-resizer'}).hide().appendTo(document.body);
- lonesomedayHTML:
<div class="box" style="width:700px">This is a sentence</div>
<div class="box" style="width:600px">This is a sentence</div>
<div class="box" style="width:500px">This is a sentence</div>
<div class="box" style="width:400px">This is a sentence</div>
JavaScript:
$( '.box' ).each(function ( i, box ) {
var width = $( box ).width(),
html = '<span style="white-space:nowrap"></span>',
line = $( box ).wrapInner( html ).children()[ 0 ],
n = 100;
$( box ).css( 'font-size', n );
while ( $( line ).width() > width ) {
$( box ).css( 'font-size', --n );
}
$( box ).text( $( line ).text() );
});
演示: http://jsfiddle.net/e8B9j/2/show/
从URL中删除“/show/”即可查看代码。
我编写了一个jQuery插件来完成这个任务:
http://michikono.github.com/boxfit
该插件会将文本在水平和垂直方向上缩放到盒子内的最大可用大小,然后居中显示。
你需要做的唯一一件事就是定义一个带有文本内容的div:
<div id="scale">some text</div>
然后调用:
$('#scale').boxfit()
该方法将接受一些参数来禁用/启用文本换行和居中对齐。
对于新的浏览器,您可以使用Inline-SVG。这可以像通过CSS缩放图像一样缩放。
.smallerVersion{
max-width: 50%
}
<!DOCTYPE html>
<html>
<head>
<title>Scale SVG example</title>
</head>
<body>
<h2>Default version:</h2>
<svg viewBox="0 0 240 80" xmlns="http://www.w3.org/2000/svg">
<style>
.small { font: italic 13px sans-serif; }
.heavy { font: bold 30px sans-serif; }
/* Note that the color of the text is set with the *
* fill property, the color property is for HTML only */
.Rrrrr { font: italic 40px serif; fill: red; }
</style>
<text x="20" y="35" class="small">My</text>
<text x="40" y="35" class="heavy">cat</text>
<text x="55" y="55" class="small">is</text>
<text x="65" y="55" class="Rrrrr">Grumpy!</text>
</svg>
<h2>Scaled version:</h2>
<svg class="smallerVersion" viewBox="0 0 240 80" xmlns="http://www.w3.org/2000/svg">
<style>
.small { font: italic 13px sans-serif; }
.heavy { font: bold 30px sans-serif; }
/* Note that the color of the text is set with the *
* fill property, the color property is for HTML only */
.Rrrrr { font: italic 40px serif; fill: red; }
</style>
<text x="20" y="35" class="small">My</text>
<text x="40" y="35" class="heavy">cat</text>
<text x="55" y="55" class="small">is</text>
<text x="65" y="55" class="Rrrrr">Grumpy!</text>
</svg>
</body>
</html>
(width: 100%;
)
https://github.com/DanielHoffmann/jquery-bigtext
简单地调用
$("#text").bigText();
它将完美地适配您的容器。
在此处查看其实际效果:
http://danielhoffmann.github.io/jquery-bigtext/
目前它有一些限制,div必须具有固定的高度和宽度,并且不支持将文本换行为多行。
编辑2:我现在已经解决了这些问题和限制,并添加了更多选项。您可以设置最大字体大小,还可以选择使用宽度、高度或两者(默认为两者)来限制字体大小。我将努力接受包装元素中的最大宽度和最大高度值。
var factor = 1/3; // approximate width-to-height ratio
var div = $('#mydiv');
div.css('font-size', div.width() / (div.text().length * factor) + 'px');
根据您所使用的字体,您需要调整factor
。对于 Times New Roman 字体,1/3 似乎可以正常工作。
支持目标类的修改版@sworoc
function resizeText(text, size, target) {
var fontSize = 0;
var resizer = $('<span>');
resizer.html(text).hide();
resizer.attr('class', target.attr('class'));
$('body').append(resizer);
while(resizer.width() < size) {
fontSize++
resizer.css("font-size", fontSize);
}
target.css('font-size', fontSize).html(text);
resizer.remove();
}
resizeText("@arunoda", 350, $('#username'));
在您的 div 中,将文本放在一个没有填充的 span 中。然后,span 的宽度将是文本的长度。
用于查找正确字体大小的未经测试的代码:
var objSpan = $('.spanThatHoldsText');
var intDivWidth = $('.divThatHasAFixedWidth').width();
var intResultSize;
for (var intFontSize = 1; intFontSize < 100; intFontSize++)
objSpan.css('font-size', intFontSize);
if (objSpan.width() > intDivWidth) {
intResultSize = intFontSize - 1;
break;
}
}
objSpan.css('font-size', intResultSize);
#
# Automagically resize fonts to fit the width of the
# container they are in as much as possible.
#
fixfonts = ->
scaler = 1.2
for element in $('.autofont')
size = 1
element = $(element)
desired_width = $(element).width()
resizer = $(element.clone())
resizer.css
'font-size': "#{size}em"
'max-width': desired_width
'display': 'inline'
'width': 'auto'
resizer.insertAfter(element)
while resizer.width() < desired_width
size = size * scaler
resizer.css
'font-size': "#{size}em"
$(element).css
'font-size': "#{size / scaler }em"
resizer.remove()
$(document).ready =>
fixfonts()
timeout = 0
doresize = ->
timeout--
if timeout == 0
fixfonts()
$(window).resize ->
timeout++
window.setTimeout doresize, 200
timeoutId = null; $(window).resize -> { window.clearTimeout(timeoutId); timeoutId = window.setTimeout fixfonts, 200; }
- Peter Taylor我不知道如何将这个添加到sworoc的帖子中,但我还是想分享一下: 如果您正在使用任何形式的AJAX导航,则Lonesomeday的解决方案会出现问题。 我稍微修改了一下:
if ($('#hidden-resizer').length == 0){
$('<div />', {id: 'hidden-resizer'}).hide().appendTo(document.body);
}