根据容器宽度动态调整文本大小

发布于 2025-01-03 18:19:47 字数 777 浏览 5 评论 0原文

我正在开发响应式设计,并且有一些显示

标签,我希望在浏览器窗口宽度减小时缩小这些标签。

初始设置,基于 960px 的宽度:

  1. 主体字体大小设置为 14px
  2. h3 标签的字体大小为 40px
  3. 包含的 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:

  1. The body font-size is set to 14px
  2. The font-size of the h3 tag is 40px
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗喜 2025-01-10 18:19:47

window.resize 是正确的事件,但它不会在页面加载时触发。不过,您可以将 .trigger('resize') 添加到您的代码中,使其在页面加载时触发:

$(window).bind('resize', function(){
    var containerSize  = $('.container').width(),
        textPercentage = 0.17391304347826086956521739130435, /* 40/230 */
        textRatio      = containerSize * textPercentage,
        textEms        = textRatio / 14;

    $('.container h3').css(fontSize, textEms+"em");
}).trigger('resize');

您需要在 document.ready 之后运行此代码> 确保您获得的容器宽度是正确的。您还可以将此代码放在 HTML 文档的底部(无论有或没有 document.ready 事件处理程序都应该这样做),这将确保运行此代码时元素可用

//wait for `document.ready` to fire
$(function () {

    //cache the .container and H3 elements
    var $container = $('.container'),
        $h3        = $container.find('h3');

    //bind event handler to `window.resize`
    $(window).bind('resize', function(){

        //get the width of the container
        var containerSize  = $container.width(),
            textPercentage = 0.17391304347826086956521739130435, /* 40/230 */
            textRatio      = containerSize * textPercentage,
            textEms        = textRatio / 14;

        $h3.css('fontSize', textEms+"em");
    }).trigger('resize');
});

:我缓存了 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:

$(window).bind('resize', function(){
    var containerSize  = $('.container').width(),
        textPercentage = 0.17391304347826086956521739130435, /* 40/230 */
        textRatio      = containerSize * textPercentage,
        textEms        = textRatio / 14;

    $('.container h3').css(fontSize, textEms+"em");
}).trigger('resize');

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 the document.ready event handler) which will make sure the elements are available when you run this code:

//wait for `document.ready` to fire
$(function () {

    //cache the .container and H3 elements
    var $container = $('.container'),
        $h3        = $container.find('h3');

    //bind event handler to `window.resize`
    $(window).bind('resize', function(){

        //get the width of the container
        var containerSize  = $container.width(),
            textPercentage = 0.17391304347826086956521739130435, /* 40/230 */
            textRatio      = containerSize * textPercentage,
            textEms        = textRatio / 14;

        $h3.css('fontSize', textEms+"em");
    }).trigger('resize');
});

Notice that I cached the H3 element(s) so it/then don't have to be selected every resize event, because when you actually re-size your browser there are tons of these events that fire.

惜醉颜 2025-01-10 18:19:47

你可以试试这个。

$(window).resize(function(){
    var containerSize = $('.container').width();
    var textPercentage = 0.17391304347826086956521739130435; /* 40/230 */
    var textRatio = containerSize * textPercentage;
    var textEms = textRatio / 14;

    //fontSize should be enclosed in quotes
    $('.container h3').css('fontSize', textEms+"em");
})
.resize();//Triggers the resize on page load to set the font size

You could try this.

$(window).resize(function(){
    var containerSize = $('.container').width();
    var textPercentage = 0.17391304347826086956521739130435; /* 40/230 */
    var textRatio = containerSize * textPercentage;
    var textEms = textRatio / 14;

    //fontSize should be enclosed in quotes
    $('.container h3').css('fontSize', textEms+"em");
})
.resize();//Triggers the resize on page load to set the font size
破晓 2025-01-10 18:19:47

对于通过谷歌搜索“响应式字体大小”等内容找到此内容的人,这可能会有所帮助:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文