简体   繁体   English

如何使jQuery“缩小文本”功能更有效? 使用二进制搜索?

[英]How can I make this jQuery “shrink text” function more efficient? With binary Search?

I've built jQuery function that takes a text string and a width as inputs, then shrinks that piece of text until it's no larger than the width, like so: 我已经构建了jQuery函数,该函数将文本字符串和宽度作为输入,然后将那段文本缩小到不大于宽度,如下所示:

function constrain(text, ideal_width){
    var temp = $('.temp_item');
    temp.html(text);
    var item_width = temp.width();
    var ideal = parseInt(ideal_width);
    var smaller_text = text;
    var original = text.length;

    while (item_width > ideal) {
        smaller_text = smaller_text.substr(0, (smaller_text.length-1));
        temp.html(smaller_text);
        item_width = temp.width();
    }

    var final_length = smaller_text.length;
    if (final_length != original) {
        return (smaller_text + '…');
    } else {
        return text;
    }
}

This works fine, but because I'm calling the function on many pieces of texts, on any browser except Safari 4 and Chrome, it's really slow. 效果很好,但是因为我要在许多文本上调用该函数,所以在除Safari 4和Chrome之外的任何浏览器上调用它的速度都非常慢。

I've tried using a binary search method to make this more efficient, but what I have so far brings up a slow script dialog in my browser: 我尝试使用二进制搜索方法来提高效率,但是到目前为止,我在浏览器中弹出了一个缓慢的脚本对话框:

function constrain(text, ideal_width){
    var temp = $('.temp_item');
    temp.html(text);
    var item_width = temp.width();
    var ideal = parseInt(ideal_width);

    var lower = 0;
    var original = text.length;
    var higher = text.length;

    while (item_width != ideal) {

        var mid = parseInt((lower + higher) / 2);
        var smaller_text = text.substr(0, mid);
        temp.html(smaller_text);
        item_width = temp.width();

        if (item_width > ideal) {

            // make smaller to the mean of "lower" and this
            higher = mid - 1;

        } else {

            // make larger to the mean of "higher" and this
            lower = mid + 1;

        }
    }

    var final_length = smaller_text.length;
    if (final_length != original) {
        return (smaller_text + '…');
    } else {
        return text;
    }
}

Does anyone have an idea of what I should be doing to make this function as efficient as possible? 有谁知道我应该怎么做才能使此功能尽可能高效?

Thanks! 谢谢! Simon 西蒙

I used 2 divs 我用了2格

<div class="englober">
<div class="title"></div>
</div>

the .englober has a fixed with and overflow:hidden, white-space:nowarp. .englober具有一个固定的,且带有-overflow:hidden,white-space:nowarp。

Then using jQuery, i resize the text from the .title to fit the .englober with : 然后使用jQuery,我调整.title中文本的大小以适合.englober:

while ($(".title").width() > $(".englober").width())
{
    var dFontsize = parseFloat($(".title").css("font-size"), 10);
    $(".title").css("font-size", dFontsize - 1);
}

The problem with your script is probably that the while condition (item_width != ideal) possibly will never abort the loop. 您的脚本的问题可能是while条件(item_width != ideal)可能永远不会终止循环。 It might not be possible to trim the input text to the exact width ideal . 可能无法将输入文本修整为ideal的精确宽度。 In this case your function will loop forever, which will trigger the slow script dialog. 在这种情况下,您的函数将永远循环,这将触发缓慢的脚本对话框。

To circumvent this you should stop looping if the displayed text is just small enough (aka. adding more characters would make it too big). 为了避免这种情况,如果显示的文本足够小(也就是添加更多字符会使它太大),则应该停止循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM