10

テキストオーバーフローが有効になっているときに(JavaScriptを介して)検出しようとしています。多くの調査の結果、Firefox のすべてのバージョンを除いて、実用的な解決策があります。

http://jsfiddle.net/tonydew/mjnvk/

省略記号が適用されるようにブラウザを調整すると、Chrome、Safari、IE8+ でさえ省略記号がアクティブであることを警告します。Firefox (17 と 18 を含む、私が試したすべてのバージョン) ではそれほどではありません。Firefox は、省略記号がアクティブでないことを常に通知します。

console.log() の出力は、その理由を示しています。

Firefox (OS X):
116/115 - false
347/346 - false

Chrome (OS X):
116/115 - false
347/851 - true 

Firefox では、scrollWidth が offsetWidth より大きくなることはありません。

解決策に最も近いのは、「IE と Firefox が div に対して異なるオーバーフロー ディメンションを返すのはなぜですか?」ですが、提案された解決策を既に使用しています。

Firefoxでもこれを機能させる方法について、誰かが光を当てることができますか?


編集: 以下の @Cezary の回答に加えて、マークアップの変更を必要としない方法を見つけました。ただし、各要素を一時的に複製して次の測定を行うため、もう少し作業が必要です。

$(function() {
    $('.overflow').each(function(i, el) {
        var element = $(this)
                      .clone()
                      .css({display: 'inline', width: 'auto', visibility: 'hidden'})
                      .appendTo('body');

        if( element.width() > $(this).width() ) {
            $(this).tooltip({
                title: $(this).text(),
                delay: { show: 250, hide: 100 },
            });
        }
        element.remove();
    });
});

http://jsfiddle.net/tonydew/gCnXh/

これの効率について誰かコメントがありますか?潜在的なオーバーフロー要素が多数あるページがある場合、悪影響はありますか? 可能であれば、既存のマークアップを変更することは避けたいと思いますが、ページの読み込みごとに過剰な JS 処理を犠牲にすることは避けたいと考えています。

4

2 に答える 2

6

Firefox で動作させるには、各 td 内に div を追加する必要があります。

<td class="first"><div>Here is some text</div></td>
<td class="second">
     <div>Here is some more text. A lot more text than
     the first one. In fact there is so much text you'd think it was a 
     waste of time to type all ofit.
     </div>
</td>

CSS

td div {
   white-space: nowrap;
   text-overflow: ellipsis;
   overflow:hidden;
   width:100%;
}

ジャスフィドル

http://jsfiddle.net/mjnvk/7/

于 2013-02-01T09:22:03.680 に答える
3

私は実際にこれを行うためにjQueryプラグインを作成しました。切り捨てられた場合は、ターゲット要素のをテキスト全体に設定するだけtitleですが、正確なニーズに合わせて調整できます。

/**
 * @author ach
 *
 * Sets the CSS white-space, overflow, and text-overflow properties such that text in the selected block element will
 * be truncated and appended with an ellipsis (...) if overflowing.  If the text is truncated in such a way, the
 * selected element's 'title' will be set to its full text contents and the cursor will be set to 'default'.
 * For this plugin to work properly, it should be used on block elements (p, div, etc.).  If used on a th or td element,
 * the plugin will wrap the contents in a div -- in this case, the table's 'table-layout' CSS property should be set to 'fixed'.
 *
 * The default CSS property values set by this plugin are:
 *     white-space: nowrap;
 *     overflow: hidden;
 *     text-overflow: ellipsis
 *
 * @param cssMap A map of css properties that will be applied to the selected element.  The default white-space,
 * overflow, and text-overflow values set by this plugin can be overridden in this map.
 *
 * @return The selected elements, for chaining
 */

$.fn.truncateText = function(cssMap) {
    var css = $.extend({}, $.fn.truncateText.defaults, cssMap);

    return this.each(function() {
        var $this = $(this);
        //To detect overflow across all browsers, create an auto-width invisible element and compare its width to the actual element's
        var element = $this.clone().css({display: 'inline', width: 'auto', visibility: 'hidden'}).appendTo('body');
        if (element.width() > $this.width()) {
            //If a th or td was selected, wrap the content in a div and operate on that
            if ($this.is("th, td")) {
                $this = $this.wrapInner('<div></div>').find(":first");
            }
            $this.css(css);
            $this.attr("title", $.trim($this.text()));
            $this.css({"cursor": "default"});
        }
        element.remove();
    });
};
$.fn.truncateText.defaults = {
    "white-space"   : "nowrap",
    "overflow"      : "hidden",
    "text-overflow" : "ellipsis"
};

使用するには、jsを含めて次のように呼び出します。

$(".override").truncateText();

これは本番環境で使用されており、ページ上の何百ものターゲット要素による悪影響には気づいていません。

于 2013-02-01T20:42:57.233 に答える