【问题标题】:Detecting CSS text-overflow: ellipsis in Firefox检测 CSS 文本溢出:Firefox 中的省略号
【发布时间】:2013-01-16 11:42:22
【问题描述】:

我正在尝试检测(通过 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 处理为代价。

【问题讨论】:

  • 您为什么要为这个问题创建一个新帐户?从您完美标记和格式化的帖子来看,您显然有另一个帐户可以用来回答问题。
  • 没有其他我知道的帐户!标签和格式是他们的方式,因为我不想成为那个问准备不足的问题的人。我不是什么好人;只是想问我能回答的最好的问题!
  • 谢谢你,顺便说一句...感谢您的称赞和 +1 :)

标签: javascript css firefox ellipsis


【解决方案1】:

我实际上编写了一个 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();

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

【讨论】:

  • 这验证了我在编辑的问题中使用的方法。很高兴听到您没有注意到页面上这么多元素的任何不良影响。
【解决方案2】:

您需要在每个 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/

【讨论】:

  • 嘿!谢谢你。这很好用,但我必须在任何地方更改标记。
  • 如果更改标记是个问题,那么我想既然您在 JS 中进行计算,您也可以随时让 JS 为您动态调整标记。
猜你喜欢
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
  • 2011-06-23
相关资源
最近更新 更多