JavaScript Change line-clamp style

I'm creating a testimonials page for my site, and some of the testimonials I have are rather long. As such I want to cut out anything longer than 3 lines.

To read the rest, the user clicks a button and the text expands to reveal the rest.

I've managed to do the above with line-clamp, however I'd like to have the ... clickable and styled with a different text. Is this possible?

I couldn't find a way to do that, so I tried a workaround. First I find out which elements have overflow so the [Read More] can be inserted. However it gets inserted at the end of the text, and not before the overflow.

I'm clueless as to how to solve this.

Desired result

Current result (jsfiddle)

HTML

<div class="testimonial container-fluid">
    <div class="col-xs-3 rating-container">
        <div>
            <div class="star"></div>
            <div class="star"></div>
            <div class="star"></div>
            <div class="star"></div>
        </div>
    </div>
    <div class="col-xs-9">
        <div class="title">Title</div>
        <div class="content">Lorem ipsum dolor sit amet, vero essent gubergren ad pro, regione epicuri contentiones ea mea. Decore omnium id vim, pro et malis molestie, et porro nostro vis. Ei libris debitis reprehendunt est. Te per delenit scaevola scripserit. Partem erroribus rationibus ea vel, nihil euismod ei vim.

            His sonet antiopam cotidieque ea, eu unum melius conclusionemque his. Ferri iisque sanctus pri ei. Ut ius tantas nonumy intellegam. Et per solum aliquam, melius elaboraret at qui.</div>
        <div class="name_loc" id="demo">Name, London</div>
    </div>
</div>

CSS

.testimonial {
    width: 920px;
    display: -webkit-box;
    background-color: #EFEFEF;
    color: #333;
    padding: 8px;
    margin: 0 0 20px;
    border: 3px solid #506790;
}

.testimonial:nth-of-type(even) {
    background-color: #EFEFEF;
}

.testimonial .rating-container {
    height: 144px;
    display: table;
}

.testimonial .rating-container > div {
    display: table-cell;
    vertical-align: middle;
}

.testimonial .star {
    width: 32px;
    height: 32px;
    display: inline-block;
    background-repeat: no-repeat;
    background-image: url('http://www.timelinecoverbanner.com/cliparts/wp-content/digital-scrapbooking/lemon-star-md1.png');
    background-size: cover;
}

.testimonial .title {
    font-size: 18pt;
    text-align: left;
    font-weight: bold;
}

.testimonial .content {
    -webkit-box-orient: vertical;
    display: -webkit-box;
    margin: 5px 0 10px;
    font-size: 12pt;
    text-align: left;
    overflow: hidden;
    height: 68px;
}

.testimonial .name_loc {
    font-style: italic;
    font-size: 12pt;
    text-align: right;
}

JS

$('#demo').on('click', function() {
    var t = $(this).closest('.testimonial');
    var c = t.find('.content');         
    var h3 = c[0].scrollHeight;             
    c.animate({'height':h3},500);
});

$('.testimonial').each(function() {
    var c = $(this).find('.content');       
    if (c.height() < c[0].scrollHeight) {
        c.css('text-overflow', 'ellipsis');
        //also tried:
        c.css('line-clamp', '3');
    }
});
Answer:1

You could do this with the ::after pseudo-element. I was playing with your fiddle and it seemed to work.

First the .content class needs to be positioned relative. Then add two new CSS classes:

.testimonial .content.collapsed {
/* styles removed from .content */
    overflow: hidden;
    height: 68px;
}
.testimonial .content.collapsed::after {
    display: inline-block;
    content: "...";
    position: absolute;
    bottom: 0;
    right: 5px;
}

You could add the click handler to the pseudo element in the JS (I didn't try this part), and then remove the "collapsed" class after the click. You could also style the pseudo element any way you want, or change the content to "Read more..." or whatever.

Answer:2

Try fiddling with your revised JsFiddle. It uses:

$('.testimonial').on( 
    'click', 
    '.content',
    function(e) {
      if (this.scrollHeight > this.offsetHeight) {
          $(this).removeClass('clipped')
                 .addClass('unclipped')
                 .css('height', this.scrollHeight+'px');
      } else {
          $(this).removeClass('unclipped')
                 .addClass('clipped')
                 .css('height', '');
      }
    }
);

and 2 extra css-classes: clipped and unclipped:

.clipped:after {
    position: absolute;
    content: '>';
    top: 2.9em;
    color: red;
    font-weight: bold;
    cursor: pointer;
}

.unclipped:after {
    position: absolute;
    content: '<';
    top: 2.9em;
    color: green;
    font-weight: bold;
    cursor: pointer;
}
Answer:3

I am currently building a web application for my employer, but I have some restrictions. It has to be in HTML, CSS, and Javascript for the front end, and I have to use a lot of different pages for ...

I am currently building a web application for my employer, but I have some restrictions. It has to be in HTML, CSS, and Javascript for the front end, and I have to use a lot of different pages for ...

I'm looking for a solution in Polymer that includes the following features: sign in sign up forgot password reset password change password social sign-in via Facebook social sign-in via Google secure ...

I'm looking for a solution in Polymer that includes the following features: sign in sign up forgot password reset password change password social sign-in via Facebook social sign-in via Google secure ...

  1. what is similar solution
  2. what is self similar solution
  3. what's similar to contact solution
  4. what is similar between solution and colloid

I am very new to angular 2 I am stuck here where i have to display an image for which I'm using relative path­­­­­­­­­­­­­­­­­­­­ <img src="./../images/publicVideo1.PNG"> but getting the ...

I am very new to angular 2 I am stuck here where i have to display an image for which I'm using relative path­­­­­­­­­­­­­­­­­­­­ <img src="./../images/publicVideo1.PNG"> but getting the ...

  1. display image using matplotlib
  2. display image using javascript
  3. display image using css
  4. display image using pillow
  5. display image using php
  6. display image using opencv
  7. display image using json
  8. display image using cv2
  9. display image using jquery ajax
  10. display image using pil python
  11. display image using applet
  12. display image using jquery
  13. display image using pil
  14. display image using html
  15. display image using opencv python
  16. display image using python
  17. display image using flask
  18. display image using tkinter
  19. display image using ajax
  20. display image using java

I am trying to send an by the user dropped file to the php upload script using ajax. I don't know why it doesn't work, nothing happens if I drop an image. (Why's no one answering?) So this is the ...

I am trying to send an by the user dropped file to the php upload script using ajax. I don't know why it doesn't work, nothing happens if I drop an image. (Why's no one answering?) So this is the ...

  1. sending file upload via ajax
  2. file upload sending
  3. send file upload via ajax jquery