知道什么溢出:隐藏有隐藏

时间:2011-02-08 15:14:52

标签: jquery html css overflow

我想知道您是否有任何方式可以致电并使用overflow:hidden隐藏的内容。

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

这甚至可能吗?你会怎么做?

我已经标记了jQuery这个问题,但当然无论做什么工作都很棒,纯CSS或Javascript也会很好。

提前致谢!

4 个答案:

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

答案 1 :(得分:3)

这是我的解决方案(使用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仅包含文本。它主要用于内联元素,如跨度,但目前在过程中将它们剥离。它可以很容易地修复以保持跨度,但是更多地涉及处理图像或定位元素等其他复杂问题。

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

答案 3 :(得分:0)

使用jQuery

尝试此solution

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