博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

智能文字大小

Posted on 2011-10-20 17:45  PHP-张工  阅读(272)  评论(0编辑  收藏  举报

根据页面的宽度自动调正字体大小

haha 中文测试

haha

代码:

<p id="pp1">haha 中文测试</p>
<p>haha</p>
<span id="span1"></span>
<script type="text/javascript">
function zoomFontSize()
{
var allWidth = document.documentElement.clientWidth;
var newFontSize = pp1.oldFontSize * allWidth / 1024;
newFontSize
= Math.min(100, newFontSize);
newFontSize
= Math.max(12, newFontSize);
pp1.style.fontSize
= newFontSize + 'px';
document.getElementById(
'span1').innerHTML = pp1.style.fontSize;
}

var pp1 = document.getElementById('pp1');
window.onresize
= zoomFontSize;
var fontSize = document.all?pp1.currentStyle['fontSize']:document.defaultView.getComputedStyle(pp1,false)['fontSize'];

pp1.oldFontSize
= parseInt(fontSize);
zoomFontSize();
alert(document.documentElement.offsetHeight);
</script>