根据容器宽度动态调整文本大小
我正在开发响应式设计,并且有一些显示
标签,我希望在浏览器窗口宽度减小时缩小这些标签。
初始设置,基于 960px 的宽度:
- 主体字体大小设置为 14px
- h3 标签的字体大小为 40px
- 包含的 div 的宽度为 230px
所以这是我为 javascript/jQuery 制定的:
$(window).resize(function(){
var containerSize = $('.container').width();
var textPercentage = 0.17391304347826086956521739130435; /* 40/230 */
var textRatio = containerSize * textPercentage;
var textEms = textRatio / 14;
$('.container h3').css(fontSize,textEms+"em");
});
我的 javscript 技能显然相当有限,所以我希望你能帮助我让这一切顺利进行。我认为 $(window).resize
函数是错误使用的事件,因为文本应该在页面加载时自动调整大小,而不仅仅是在窗口调整大小时自动调整大小。
预先感谢您的帮助!
注意:我不希望文本拉伸到容器的边缘,这就是我不使用 FitText.js 或 BigText.js 的原因。
I'm working on a responsive design and I have some display <h3>
tags that I would like to scale down when the width of the browser window is reduced.
The initial setup, based on a width of 960px:
- The body font-size is set to 14px
- The font-size of the h3 tag is 40px
- The width of the containing div is 230px
So here's what I worked out for the javascript/jQuery:
$(window).resize(function(){
var containerSize = $('.container').width();
var textPercentage = 0.17391304347826086956521739130435; /* 40/230 */
var textRatio = containerSize * textPercentage;
var textEms = textRatio / 14;
$('.container h3').css(fontSize,textEms+"em");
});
My javscript skills are obviously quite limited, so I was hoping you could help me get this all purdy and working right. I think the $(window).resize
function is the wrong event to use, since the text should resize automatically on page load, not just on window resize.
Thanks in advance for the help!
Note: I don't want the text to stretch to the edges of the containers, which is why I'm not using FitText.js or BigText.js.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
window.resize
是正确的事件,但它不会在页面加载时触发。不过,您可以将.trigger('resize')
添加到您的代码中,使其在页面加载时触发:您需要在
document.ready
之后运行此代码> 确保您获得的容器宽度是正确的。您还可以将此代码放在 HTML 文档的底部(无论有或没有document.ready
事件处理程序都应该这样做),这将确保运行此代码时元素可用:我缓存了
H3
元素,因此不必在每个resize
事件中都选择它,因为当您实际调整浏览器大小时,会有大量这些引发事件的事件。window.resize
is the correct event but it doesn't fire on page-load. You can however just add.trigger('resize')
to your code to make it fire on page-load:You are going to want to run this code after
document.ready
to make sure the width you are getting for the container is correct. You could also place this code at the bottom of your HTML document (which you should do with or without thedocument.ready
event handler) which will make sure the elements are available when you run this code:Notice that I cached the
H3
element(s) so it/then don't have to be selected everyresize
event, because when you actually re-size your browser there are tons of these events that fire.你可以试试这个。
You could try this.
对于通过谷歌搜索“响应式字体大小”等内容找到此内容的人,这可能会有所帮助:
https:// github.com/davatron5000/FitText.js
这是一个 jQuery 插件,可以根据容器的宽度设置字体大小。它还具有一些选项来获得所需的结果。
PS:当然,当窗口大小调整时,它也会缩放文本。
For people who find this by googling something like 'responsive font size', this might be of help:
https://github.com/davatron5000/FitText.js
It is a jQuery plugin that sets a font-size based on it's container's width. It also has some options to get the desired results.
PS: Of course it also scales your text when the window gets resized.