检测CSS文本溢出:Firefox中的省略号

时间:2013-02-01 07:28:45

标签: javascript css firefox ellipsis

我正试图在文本溢出生效时检测(通过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。

我能找到最接近解决方案的是“Why are IE and Firefox returning different overflow dimensions for a 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处理为代价。

2 个答案:

答案 0 :(得分:7)

你需要在每个td中添加div以使其在firefox中运行,

<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%;
}

的jsfiddle

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

答案 1 :(得分: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();

这已经在制作中使用,并且没有注意到页面上有数百个目标元素的任何不良影响。