2011-02-08 55 views
11

我想知道是否有任何方式可以调用和使用什么overflow:hidden已隐藏。知道什么溢出:隐藏已隐藏

为了澄清我的意思,在this example我想知道“这是隐藏的”是div的隐藏部分。

这甚至可能吗?你会如何处理它?

我已经标记了jQuery的问题,但当然不管做什么工作都很好,纯粹的CSS或Javascript将会很好。

在此先感谢!

+0

@ gms8994对不起这个错字!感谢编辑! – Trufa 2011-02-08 15:17:55

+0

它总是很容易,没有风格的文本?具有`overflow:hidden` **总是**的元素是否具有`width`和`height`?换句话说,你的榜样是如何做作的? – thirtydot 2011-02-08 15:29:42

+0

@thirtydot那实际上是一个很好的问题!我不知道该如何回答,因为我现在需要的是具有固定高度和宽度的不带风格的文本,但会发现它非常有趣,其答案也涵盖其他情况,而不会使问题如此开放以至于无法回答。我解释了我自己吗? – Trufa 2011-02-08 15:33:54

回答

5

试试这个:

CSS:

.text{ 


    outline:1px solid orange; 

    width:100px; 
    height:20px; 
    overflow:hidden; 

} 

HTML:

<div class="text">This is shown. This is hidden</div> 

<div id="result"></div> 

<div id="container" style="visibility:hidden;"></div> 

JS:

$("<span />").text($(".text").text()).appendTo("#container"); 

$("#result").append("<span>"+$(".text").width()+"</span><br />"); 
$("#result").append("<span>"+$("#container span").width()+"</span><br />"); 

$("#result").append("<span>Overflow: "+ (($("#container span").width() > $(".text").width()) ? "yes" : "no")+"</span><br />"); 

Example

编辑

试试这个:

based on this plugin

New Example

CSS:

.text{ 
    outline:1px solid orange; 
    width:100px; 
    height:20px; 
    overflow:hidden; 
} 

HTML:

<br/> 
<br/> 
<div class="text" id="test1">This is shown. This is hidden</div> 
<div class="text" id="test2">No hidden</div> 
<br/> 
<br/> 
<div id="result"></div> 

JS:

(function($) { 

    $.fn.noOverflow = function(){ 

     return this.each(function() { 

      var el = $(this); 

      var originalText = el.text(); 
      var w = el.width(); 

      var t = $(this.cloneNode(true)).hide().css({ 
       'position': 'absolute', 
       'width': 'auto', 
       'overflow': 'visible', 
       'max-width': 'inherit' 
      }); 
      el.after(t); 

      var text = originalText; 
      while(text.length > 0 && t.width() > el.width()){ 
       text = text.substr(0, text.length - 1); 
       t.text(text + ""); 
      } 
      el.text(t.text()); 

      /* 
      var oldW = el.width(); 
      setInterval(function(){ 
       if(el.width() != oldW){ 
        oldW = el.width(); 
        el.html(originalText); 
        el.ellipsis(); 
       } 
      }, 200); 
      */ 

      this["overflow_text"] = { 
       hasOverflow: originalText != el.text() ? true : false, 
       texOverflow: originalText.substr(el.text().length) 
      }; 

      t.remove(); 

     }); 

    }; 

})(jQuery); 

$("#test1").noOverflow(); 

$("#result").append("<span>Test 1</span><br />"); 

$("#result").append("<span>Has Overflow: "+ (($("#test1")[0].overflow_text.hasOverflow) ? "yes" : "no")+"</span><br />"); 

$("#result").append("<span>Text Overflow: "+ $("#test1")[0].overflow_text.texOverflow+"</span><br />"); 

$("#test2").noOverflow(); 

$("#result").append("<br /><span>Test 2</span><br />"); 
$("#result").append("<span>Has Overflow: "+ (($("#test2")[0].overflow_text.hasOverflow) ? "yes" : "no")+"</span><br />"); 
$("#result").append("<span>Text Overflow: "+ $("#test2")[0].overflow_text.texOverflow+"</span><br />"); 
2

这可以给出在div中的文本可以换行的特定情况下的隐藏文本的估计值。

<div class="text"><div id="inner">This is shown. This is hidden</div></div> 

添加到的.text CSS类:

line-height: 20px; 

在运行时,您可以使用.height()jQuery函数来获取内部div的计算高度。将它除以行高,可以得到文本被包装的行数,只显示第一行。然后,您可以猜出显示/未显示的最后一个单词,并在那里对文本进行细分。

var text = $("#inner").html(); 
var height = $("#inner").height(); 
var lineheight = $("div.text").height(); 
var rows = Math.round(height/lineheight); 
alert("computed inner height: " 
    + $("#inner").height() 
    + " | rows: " + rows); 
alert("Hidden text: " 
    + text.substring(
     text.indexOf(" ",Math.round(text.length/rows)))); 
2

这里是我的解决方案(使用jQuery)。

标记:

<div class="text">This is shown. This is hidden</div> 

CSS:

.text{ 


    outline:1px solid orange; 

    width:200px; 
    height:20px; 
    overflow:hidden; 

} 

(只有溢出:其实隐藏着重要的,代码将仍然高度和宽度不同的价值观工作)

JS:

$('.text').wrapInner('<div/>'); 
var originaltext = $('.text div').text(); 

var t = $('.text div').text(); 

while(true) 
{ 
    if ($('.text div').height() <= $('.text').height()) { break; } 

    $('.text div').text(t.substring(0, t.lastIndexOf(" "))); 
    t = $('.text div').text(); 
} 

$('.text div').text(originaltext); 

alert("shown: " + originaltext.substring(0, t.length)); 
alert("hidden: " + originaltext.substring(t.length)); 

下面是它在做什么:

我们保存原始文本到一个变量,所以我们可以在以后恢复它。

然后我们一次删除一个单词,直到内部div降到与容器相同的高度(隐藏溢出)。仍然在div中的所有内容都是可见的,我们必须删除的所有内容都是隐藏的。

限制

我的解决方案假定DIV只包含文本。它主要适用于像跨度这样的内联元素,但目前在这个过程中将它们去掉。它可以很容易地被固定来保存跨度,但是处理图像或其他复杂问题(如定位元素)则涉及更多。

0

试试这个solution使用jQuery

JQuery的

$t = $('#text'); 

var shown, all, hiddenWidth; 

// we start with the shown width set (via css) at 100px 
shown = $t.width(); 

// we change the css to overflow:visible, float:left, and width:auto 
$t.css({'overflow': 'visible', 'float': 'left', 'width': 'auto'}); 

// and get the total width of the text 
all = $t.width(); 

// then we set the css back to the way it was 
$t.css({'overflow': 'hidden', 'float': '', 'width': '100px'}); 

// and retrieve the hiddenWidth 
hiddenWidth = all - shown; 

HTML

<div id="text">This is shown. This is hidden</div> 

CSS

#text{ 
    outline:1px solid orange; 
    width:100px; 
    height:20px; 
    overflow:hidden; 
}