jquery-read-more
Version:
jQuery plugin for easy read more text animation
9 lines (8 loc) • 1.5 kB
JavaScript
/*!
Read more / read less jQuery plugin.
https://github.com/AndreF010203/jQuery-Read-More.git
version 1.0.0 (Nov, 2020)
Copyright (c) 2020 Andrea Facchini
Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
!function(k){k.fn.readMore=function(e){var u,x,g,b,v;"destroy"!==e?(u=parseInt(e.lines),x=e.readMoreLabel||"Read more",g=e.readLessLabel||"Read less",b=e.ellipsis||"...",v=e.splitOn||" ",u&&!isNaN(u)?k(this).each(function(e,a){var t,i,n,s,l=k(a).html(),c=l.split(v),o=k("<div/>"),d=k("<div/>"),p=k(k(a).data().controller),h=0,r=0,m=!1,y=c.length;for(d.html(l),k(a).html(o).append(d),t=0;t<y;t++){o.append(c[t]+" ");var f=o.height();if(h<f&&(h=f,u<++r)){m=!0,o.html(c.slice(0,t).join(" "));break}}m?(p.text(x).css("display","block"),o.append(b),i=o.height(),n=d.height(),s=o.css("display"),p.on("click",function(){k(a).data().expanded?(d.animate({height:i+"px"},1e3,function(){o.css("display",s),d.css("display","none")}),p.animate({opacity:0},500,function(){p.text(x)}).animate({opacity:1},500),k(a).data("expanded",!1)):(o.css("display","none"),d.css("display",s),d.css("height",i+"px"),d.animate({height:n+"px"},1e3),p.animate({opacity:0},500,function(){p.text(g)}).animate({opacity:1},500),k(a).data("expanded",!0))})):p.css("display","none"),d.css("display","none")}):console.error("lines must be an integer")):k(this).each(function(e,a){k(k(a).data().controller).off("click"),k(a).html(k(a).children().last().html())})}}(jQuery);