jQuery 高度的 Font Face 问题 - 第二次迭代之前的值不正确

发布于 2024-12-19 22:32:59 字数 2448 浏览 1 评论 0原文

这是一个带有我的代码的jsFiddle

如您所见,返回了文本区域的高度完美。但这是因为文本区域不是字体模式。

在我的网站上,文本是由 font-face 生成的,但它在 font-face 加载之前获取高度,因为一旦您将 div 悬停一次并运行第二次迭代,它就会返回正确的高度。

所以 jQuery 高度才起作用,但只有在网站完全加载字体后才有效。

有解决这个问题的方法吗?

感谢您的指点。

简单标记

<div class="home-mod">    
    <div class="mod-center"><img ... /></div>    
    <div class="mod-info"> <!-- this is the slider, slides up when .mod-info is hovered -->                    
        <a class="mod-link" href="..." title="test">DYNAMIC FONT-FACE TEXT HERE</a>

        <div class="mod-excerpt">                        
            DYNAMIC NORMAL TEXT HERE                            
        </div>            
    </div>                           
</div>

当前脚本 - 当文本不是字体时完全可以完美工作

$(function() {
    // positioning the current div.mod-info inside current div.home-mod

    $(".home-mod").each(function() { 
        // this finds the div.mod-link height, and assigns var to div.mod-info top position
        var moduleLink = $(this).find(".mod-link").height(),
        modulePlus = moduleLink+20;

        $(this).find('.mod-info').css("top", "-" + modulePlus + "px");
    });        

    // animating current div.mod-info to new top position

    $("div.mod-info").hover(function() {
        // first iteration
        // getting dynamic overall height of div.mod-info and animate to 

        var moduleInfo = $(this).height();

        // this then animates to new position 
        $(this).animate({ top: "-" + moduleInfo + "px" });        
    }, function() {
        // second iteration
        // returns back to original postion

        var moduleLink = $(this).find(".mod-link").height(),
        modulePlus = moduleLink+20;

        $(this).animate({ top: "-" + modulePlus + "px" });        
    });

    // this justs finds the link and in .home-mod and make its clickable        
    $(".home-mod").click(function() {            
        window.location = $(this).find("a.mod-link").attr("href");
        return false;            
    });                    
});

更新 - 已解决

这对我有用,而不是准备好文档。负载略有滞后,但比不正确的值要好。

$(window).load(function(){  

    // my script here...

}); 

Here is a jsFiddle with my code

As you can see, the height of the text area is returned perfectly. But this is becuase the text area is not in font-face mode.

On my site, the text is font-face generated, but it's getting the height before font-face has loaded, because once you hover the div once and the second iteration runs, it returns the correct height.

So the jQuery height is working, but only once the site has fully loaded the font-face.

Is there a work around this?

Thanks for any pointers.

SIMPLE MARKUP

<div class="home-mod">    
    <div class="mod-center"><img ... /></div>    
    <div class="mod-info"> <!-- this is the slider, slides up when .mod-info is hovered -->                    
        <a class="mod-link" href="..." title="test">DYNAMIC FONT-FACE TEXT HERE</a>

        <div class="mod-excerpt">                        
            DYNAMIC NORMAL TEXT HERE                            
        </div>            
    </div>                           
</div>

CURRENT SCRIPT - FULLY WORKING PERFECT, WHEN TEXT IS NOT FONT-FACE

$(function() {
    // positioning the current div.mod-info inside current div.home-mod

    $(".home-mod").each(function() { 
        // this finds the div.mod-link height, and assigns var to div.mod-info top position
        var moduleLink = $(this).find(".mod-link").height(),
        modulePlus = moduleLink+20;

        $(this).find('.mod-info').css("top", "-" + modulePlus + "px");
    });        

    // animating current div.mod-info to new top position

    $("div.mod-info").hover(function() {
        // first iteration
        // getting dynamic overall height of div.mod-info and animate to 

        var moduleInfo = $(this).height();

        // this then animates to new position 
        $(this).animate({ top: "-" + moduleInfo + "px" });        
    }, function() {
        // second iteration
        // returns back to original postion

        var moduleLink = $(this).find(".mod-link").height(),
        modulePlus = moduleLink+20;

        $(this).animate({ top: "-" + modulePlus + "px" });        
    });

    // this justs finds the link and in .home-mod and make its clickable        
    $(".home-mod").click(function() {            
        window.location = $(this).find("a.mod-link").attr("href");
        return false;            
    });                    
});

UPDATE - SOLVED

Instead of document ready, this worked for me. Slight lag on load but better than incorrect values.

$(window).load(function(){  

    // my script here...

}); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

緦唸λ蓇 2024-12-26 22:32:59

是的。

  1. 最简单的解决方法 - 以像素为单位设置文本的行高(您想要获取高度的位置)。如果字体中字母的宽度相似 - 它将起作用。

  2. 创建 2 个隐藏的(不通过 display:none,使用 position:absolute + top:-5000px;)div 并带有文本 + <代码>空白:nowrap。例如,第一个使用 monospace,第二个使用 your_font,monospace font-family。然后创建setInterval(50ms延迟是正常的)并比较它们的高度。
    当高度变得不同时 - 您的字体已加载,您需要 clearInterval

  3. 最长的方法 - 使用 $(window).load

Yes.

  1. Simplest workaround - set line-height of text (where you want to get height) in pixels. If width of letters in fonts are similar - it will work.

  2. Create 2 hidden (not through display:none, use position:absolute + top:-5000px;) divs with text + white-space:nowrap. For example 1st with monospace, 2nd with your_font,monospace font-family. Then create setInterval (50ms delay is normal) with comparison of their heights.
    When heights become different - your font is loaded and you need to clearInterval.

  3. Longest way - use $(window).load

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