自动调整具有固定宽度的div的字体大小

时间:2013-01-01 07:40:54

标签: javascript jquery html font-size

我有一个具有以下属性的div

 background: none repeat scroll 0 0 #999999;
border: 1px solid #639BBE;
height: 62px;
margin-top: 13px;
width: 135px;

这个div的宽度和高度是固定的我不能改变它们,现在如果我在那个div中输入一些大文本它就在div之外,我想要这样的东西我可以处理这个,意思是如果这个文本div增加它会自动减小字体大小以调整文本。没问题,如果我需要添加一些JavaScript没有任何限制。

在我输入大文字时,它显示为这样!

enter image description here

提前致谢

3 个答案:

答案 0 :(得分:0)

$.fn.HasScrollBar = function() {
//note: clientHeight= height of holder
//scrollHeight= we have content till this height
var _elm = $(this)[0];
var _hasScrollBar = false; 
if ((_elm.clientHeight < _elm.scrollHeight) || (_elm.clientWidth < _elm.scrollWidth)) {
    _hasScrollBar = true;
}
return _hasScrollBar;
}

使用此函数查找溢出流并使用jquery

更改字体大小

答案 1 :(得分:0)

试用这个J查询示例它会帮助你https://github.com/davatron5000/FitText.js

答案 2 :(得分:0)

这个链接解决了我的问题,这个插件非常好

Jquery Textfill