【问题标题】:Animate container height, add ... to last word in paragraph动画容器高度,添加 ... 到段落的最后一个单词
【发布时间】:2012-01-20 22:38:22
【问题描述】:

我有一个 div.infotext 里面有一个段落。我将该 div 的高度设置为 120 像素。所以下面还有更多的文字,没有显示出来,因为高度把它截断了。

当我将鼠标悬停在该 div 上时,会出现整个文本。

我现在遇到的问题是,没有迹象表明本段中有更多文本。我想在显示的最后一个单词的末尾添加三个点 ( ... ),并在我悬停 div 时消失。

有什么想法吗?

更新:添加了代码。

function enableHoverQuotes(){

var height3 = $('.c2244').height();
$('.introtext').css({ height: '107px', overflow: 'hidden' });

$(".introtext").hoverIntent(showInfoText,hideInfoText);

function showInfoText(){ $(this).animate({'height':height3}, 600); }
function hideInfoText(){ $(this).animate({'height':'107'}, 600); }

}

【问题讨论】:

  • 我们需要代码...我们需要代码...我们需要代码..

标签: jquery css height jquery-animate


【解决方案1】:

CSS3 提供 text-overflow 属性,您可以在其中定义当文本溢出包含元素时要发生的情况。所有主流浏览器都支持浏览器——你可以在这里阅读更多信息:https://developer.mozilla.org/en/CSS/text-overflow

你应该做的是在你的 introtext css 中添加 text-overflow 属性

【讨论】:

    【解决方案2】:

    添加这个 css...

    div.infotext
    {
        overflow: hidden;
        text-overflow: ellipsis;
    }
    

    这将在 div 的末尾添加 ...,如果还有更多文本。您之前的显示/隐藏代码仍然可以正常工作,而不会影响这一点。

    【讨论】:

    • 我喜欢这个,但据我所知浏览器对文本溢出的支持非常糟糕。
    • text-overflow 在大多数浏览器中都支持,只要它与剪辑或省略号一起使用,所以它很好。
    【解决方案3】:

    您可以将<p>...</p> 添加到具有绝对定位的div 的右下角,并在悬停时将其隐藏。这会让用户看起来像实际剪切的文本。示例:

    CSS:
    .more {
     position:absolute;
     bottom:0;
     right:0;
     background: #fff;
    }
    
    .container {
     position:relative;
     height:120px;
     overflow: hidden;
    }
    
    HTML:
    <div class="container">
     <p>Fill in a lot of text here</p>
     <p class="more">...</p>
    </div>
    

    【讨论】:

      【解决方案4】:

      如果 scrollHeight 大于高度,一种可能的方法是在父 div 的右下角附加一个浮动 div

      代码可能类似于:

      if ($(element)[0].scrollHeight > $(element).height()) {
        $(element).append('<div style="position:absolute;bottom:0;right:0" id="overflow">...</siv>');
      }
      

      然后在 id="overflow" 上进行切换以在 div 展开时将其隐藏。

      【讨论】:

        【解决方案5】:

        这个怎么样:http://jsfiddle.net/M5rzC/4/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-05-23
          • 2016-08-20
          • 2020-05-21
          • 2016-09-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多