简体   繁体   English

扩展具有固定高度的内容div

[英]Expanding content div with fixed height

I have the following requirement for a component: 我对组件有以下要求:

Given a container that contains some html/text, if the content reaches a specified height (example: 175px) the following needs to happen: 给定一个包含一些html / text的容器,如果内容达到指定的高度(例如:175px),则需要进行以下操作:

  • The text is truncated to fit within the 175px height 截断文本以适应175px高度
  • at the truncation point a "... More" needs to be appended (this still needs to fit within the 175px height) 在截断点处需要附加“...更多”(这仍然需要在175px高度内)
  • When "more" is clicked the content needs to expand to its full size 单击“更多”时,内容需要扩展到其完整大小

And it needs to run in all browsers (including IE) 它需要在所有浏览器(包括IE)中运行

I've tried a number of libraries: 我尝试了很多库:

readmore.js ( http://jedfoster.com/Readmore.js/ ) - This one gets me close, but the "more" link is appended as an extra div after the main content div and not at the end of the breakpoint of the text. readmore.js( http://jedfoster.com/Readmore.js/ ) - 这个让我接近,但“更多”链接作为主要内容div之后的额外div而不是在断点的末尾附加文本。

clamp.js ( https://github.com/josephschmitt/Clamp.js/ ) - This adds the "..." at a specified height but doesn't add a clickable more link that can expand and has problems running in IE clamp.js( https://github.com/josephschmitt/Clamp.js/ ) - 这会在指定高度添加“...”,但不添加可点击的更多链接,可以扩展并在IE中运行时出现问题

dotdotdot jQuery plugin - Same problem as Clamp dotdotdot jQuery插件 - 和Clamp一样的问题

What are my options for doing something like this? 做这样的事情我有什么选择? Is there any way to avoid font/pixel math? 有没有办法避免字体/像素数学?

I created a working solution for what you are trying to accomplish. 我为您要完成的任务创建了一个可行的解决方案。 Check out my fiddle here: https://jsfiddle.net/akm1essL/5/ . 看看我的小提琴: https//jsfiddle.net/akm1essL/5/

JS JS

var container = document.getElementById('container');
var myText = container.getElementsByClassName('text-content')[0];
var spanEl = myText.childNodes[0];
var originalStr = myText.textContent.toString();
var truncatedStr;

// create the "More button" to be appended
var moreButton = document.createElement('span');
moreButton.setAttribute('class', 'readMore');
moreButton.setAttribute('id', 'moreButton');

checkHeight();

function checkHeight() {
    var maxHeight = 170;
    if (myText.clientHeight > maxHeight) {
        truncate();
    }
}

function truncate() {
    var str, hasButton;
    if (!hasButton) {
        myText.appendChild(moreButton);
        setButton("more");
        hasButton = true;
    }

    str = myText.textContent;
    truncatedStr = str.slice(0, str.lastIndexOf(' '));
    spanEl.textContent = truncatedStr;

    checkHeight(myText.clientHeight);
}

function showFull() {
    spanEl.textContent = originalStr;
    setButton();
}

function showTruncated() {
    spanEl.textContent = truncatedStr;
    setButton("more");
}

function setButton(display) {
    var btn = document.getElementById("moreButton");
    if (display === "more") {
        btn.textContent = "...More";
        btn.addEventListener("click", showFull);
        btn.removeEventListener("click", showTruncated);
    } else {
        btn.textContent = "...Less";
        btn.removeEventListener("click", showFull);
        btn.addEventListener("click", showTruncated);
        hasButton = false;
    }

}

To shorten the text, this solution checks the content height initially, then runs the truncate() function and shortens the text one word at a time from the end until it fits within the maximum height. 为了缩短文本,此解决方案最初检查内容高度,然后运行truncate()函数并从结尾一次缩短一个单词的文本,直到它适合最大高度。 If the text you have to shorten ends up being 10,000 words long, this will not be the best approach, but I am not sure of your use case. 如果您需要缩短的文本最终为10,000字,这不是最好的方法,但我不确定您的用例。

HTML HTML

<div id="container">
    <p class="text-content">
        <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nam suscipit, labore nemo distinctio incidunt laboriosam, est inventore totam voluptatem explicabo unde eius et. Provident harum, dolor tempora, aut consectetur excepturi. Dolorem aperiam distinctio ratione quam saepe eius ex, quasi, corporis molestias at laborum commodi, quis voluptatum possimus temporibus. Earum quis, et laudantium labore maxime fuga numquam explicabo!</span>
    </p>
</div>

Also I tested it in IE9, 10, Chrome, and FF and it works for all. 我还在IE9,10,Chrome和FF中进行了测试,它适用于所有人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM