我无法让这个javascript为我的网站工作,我不知道为什么。它应该根据窗口大小更改字体大小。如果您调整浏览器窗口的大小,字体应更改大小。相反,它会大幅缩小页面加载时第一行文本的字体大小。它没有做任何其他事情。它与我网站上的其他代码混合在一起。
<script type="text/javascript" charset="utf-8">
$( document ).ready( function() {
var $body = $('body'); //Cache this for performance
var setBodyScale = function() {
var scaleSource = $body.width(),
scaleFactor = 0.35,
maxScale = 600,
minScale = 30; //Tweak these values to taste
var fontSize = scaleSource * 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();
});
</script>