0

If you use javascript to set when the article paragraph exceeds two lines, the showMore button will appear.
If there are no more than two lines, showMore will not be displayed. At present, I use the word count method to set a problem, that is, it has more than two lines. It's OK, but the number of words has not reached 40, so the showMore button will not appear. I want to use the number of lines to judge, but how should I write it?

$(function(){
    let len = 40;
    $('.info_content').each(function(){ 
        if($(this).html().trim().length >len){   
            var str=$(this).html().substring(0,len-1)+"<button class='info-more'>...showMore</button>";
            $(this).html(str);
        } 
    });
});  
 .info_content{
    width: 250px;
    font-size: 15px;
    letter-spacing:1px;
    line-height: 1.5;
    margin-bottom: 8px;
    letter-spacing:0;
    overflow:hidden;
    text-overflow:clip;
    display:-webkit-box;
    -webkit-box-orient:vertical;  
    -webkit-line-clamp:2;
   }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3 class="info_content">
     asd asdasdasd asd asda sdasd asdasd asd asdsda sdasdasda sdasd asda sdasd asd a
</h3>

 <h3 class="info_content">
asdasdasddsasfsdfsdfsdfsdf
</h3>


<!-- 第三組 -->
 <h3 class="info_content">
asdasdasdasgasdasdas   asdasda
asdasdasdasdasdasdasdasdasdd    asdasdasdasdasdasdasdasda
asdasdasdasdasdasdas     asdasdaasd     asda
</h3>
ciekals11
  • 2,032
  • 1
  • 10
  • 24
AWEI
  • 417
  • 3
  • 9
  • 3
    You can check for paragraph height. Create an invisible element with exact same style (`font-size` and `line-height` that is) that is 2 lines long, save its height, remove this element (or keep it if you will) and check if other paragraphs have height that is greater than that. – ciekals11 Dec 01 '21 at 08:22
  • 2
    Please fix the formatting of your question. – CBroe Dec 01 '21 at 08:23

2 Answers2

1

Create dummy element with a<br>a to simulate element with 2 lines.
Get its height and compare with other element.
If given element is bigger than dummy element add button after it (in will not work because it will be placed at the end of string which is hidden)

I have added inserAfter method from this answer to make adding button cleaner.

document.onreadystatechange = () => {
    if (document.readyState === 'complete') {
        const maxHeight = getElementMaxHeight('info_content');
        document.querySelectorAll('.info_content').forEach(infoContent => {
            infoContent.style.display = 'block';
            if (infoContent.clientHeight > maxHeight) {
                const button = document.createElement('button');
                button.classList.add('info-more');
                button.innerText = '...showMore';
                infoContent.insertAfter(button);
            }
            infoContent.style.display = '-webkit-box';
        });
    }
};

const getElementMaxHeight = (className) => {
    const element = document.createElement('div');
    element.classList.add(className);
    element.innerHTML = 'a<br>a';
    element.style.display = 'block';
    element.style.position = 'absolute';

    document.body.append(element);

    const height = element.clientHeight;

    element.remove();

    return height;
}

Element.prototype.insertAfter = function (newElement) {
    this.parentNode.insertBefore(newElement, this.nextSibling);
}
.info_content {
    width: 250px;
    font-size: 15px;
    line-height: 1.5;
    margin-bottom: 8px;
    letter-spacing: 0;
    overflow: hidden;
    text-overflow: clip;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
}
<h3 class="info_content">
    asd asdasdasd asd asda sdasd asdasd asd asdsda sdasdasda sdasd asda sdasd asd a
</h3>

<h3 class="info_content">
    asdasdasddsasfsdfsdfsdfsdf
</h3>


<!-- 第三組 -->
<h3 class="info_content">
    asdasdasdasgasdasdas asdasda
    asdasdasdasdasdasdasdasdasdd asdasdasdasdasdasdasdasda
    asdasdasdasdasdasdas asdasdaasd asda
</h3>

<!-- 第四組 -->
<h3 class="info_content">
   qqq
</h3>

<!-- 第五組 -->
<h3 class="info_content">
  asdasdasdasgasdasdas asdasda s asdasda s asdasda 
</h3>
ciekals11
  • 2,032
  • 1
  • 10
  • 24
  • Hello, thank you for answering me! It is very close to the effect I want to achieve, but I did a test~ I hope that the button will appear only when the text length exceeds two lines, but after my test, it is found that the button appears when the last text length exceeds two lines! Would you like to ask if there is a solution to this problem? The following URL is my test example https://codepen.io/hong-wei/pen/zYExaae?editors=1010 – AWEI Dec 01 '21 at 15:17
  • https://upload.cc/i1/2021/12/01/Biwkqj.png – AWEI Dec 01 '21 at 15:17
  • 1
    remove `.` from `getElementMaxHeight('.info_content')`. It was causing an issue. I have updated my answer – ciekals11 Dec 01 '21 at 18:31
  • Hello~ I have chosen you as the best answer! But there is a place I don’t understand, element.id ='oyifysbfuisetfsiufgse'; what does this paragraph mean? – AWEI Dec 02 '21 at 01:46
  • @AWEI this line sets `id` parameter of new element. I have added this in order to find this element to remove it. After looking into it I have noticed that this line can be removed and so I did. – ciekals11 Dec 02 '21 at 07:10
  • Sorry~ I experimented with your code again and found that there are still some problems! – AWEI Dec 02 '21 at 09:24
  • Because I am a newbie in javascript and I don’t know how to change it, so I want to ask you~ The first question is, I want to do it when the text exceeds two lines Want to show more buttons, but can they appear in the last position of the second row? It will go to the position of the third row at present. – AWEI Dec 02 '21 at 09:24
  • The second question is that there is a problem if the user changes the line by himself, why would the display more buttons not appear? Because I hope that as long as there are more than two lines, the display more buttons need to be placed in the second Behind the line. Sorry to keep asking you, thank you for your help. – AWEI Dec 02 '21 at 09:24
  • https://codepen.io/hong-wei/pen/wvraMBN?editors=1010 – AWEI Dec 02 '21 at 09:24
  • https://upload.cc/i1/2021/12/02/M69VD2.jpg – AWEI Dec 02 '21 at 09:25
  • @AWEI First question) You can add `position: absolute;bottom:0;right:0;` to your button and `position: relative` to your `.info_content` It will place button in the bottom right corner but will not look good. To achieve it and make it look nicely, you will have to use other approach and truncate all extra characters for button to fit it. – ciekals11 Dec 02 '21 at 09:30
  • @AWEI Second question) move everything from `if (document.readyState === 'complete') {}` into some sort of function and execute in whenever user changes something. My code is performing only after page loads so it will not trigger again until you make so sort of event listener or call it manually – ciekals11 Dec 02 '21 at 09:32
1

I came up with this solution but rather than having a button it's easier to make it happen with those elements.

here is the solution I came up with. (click the text to show more, click again to hide)

$(this).on("click",function(e){
  const c =e.target.className.includes("c2");
  if(c){
  $(e.target).removeClass("c2");  
  }else{
  $(e.target).addClass("c2");}
});
 .info_content{
    width: 250px;
    font-size: 15px;
    letter-spacing:1px;
    line-height: 1.5;
    margin-bottom: 8px;
    letter-spacing:0;
    overflow:hidden;
    text-overflow:clip;
    display:-webkit-box;
    -webkit-box-orient:vertical;  
    -webkit-line-clamp:2;
   }
.c2{
  -webkit-line-clamp: unset;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <h3 class="info_content">
     Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ut ornare metus. Integer gravida sollicitudin metus, eu aliquet mi varius at. Suspendisse elementum ex erat, at iaculis augue aliquet in. Ut nunc eros, egestas aliquet blandit a, viverra sed tellus.
    </h3>

     <h3 class="info_content">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </h3>

     <h3 class="info_content">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque aliquam nunc ac viverra ullamcorper. Nam pellentesque aliquam enim, sit amet molestie enim iaculis non. Vestibulum sit amet diam. 
    </h3>
aekit
  • 405
  • 4
  • 14