jquery.dotdotdot
by sgolemme
HTML
<script src="https://rawgit.com/BeSite/jQuery.dotdotdot/master/src/js/jquery.dotdotdot.min.js"></script>
<div>
<div>
<div>
<h4>Test1</h4>
<div class="securetext">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<h4>Test2</h4>
<div class="securetext">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>
<p>It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum</p>
</div>
</div>
</div>
</div>
<div>
<div>
<div>
<h4>Test3</h4>
<div class="securetext">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five...
CSS
.readless, .readmore {
display: none;
color:blue;
}
.securetext--is-truncated .readmore, .securetext--is-not-truncated .readless {
display: inline-block;
}
JavaScript
var bindReadMore = function () {
$('.readmore').on('click', function (e) {
e.preventDefault();
console.log(this);
var parent = $(this).closest(".securetext");
console.log(parent);
parent.trigger("destroy");
parent.removeClass('securetext--is-truncated');
parent.addClass('securetext--is-not-truncated');
bindReadLess(); // bind click on "less"
});
};
var bindReadLess = function () {
$('.readless').on('click', function (e) {
e.preventDefault();
var parent = $(this).closest(".securetext");
truncateIfNeeded(parent); // Re-initialize ellipsis
});
};
var truncateIfNeeded = function (jqueryTag) {
var $selectionToTruncate = jqueryTag || $('.securetext');
$selectionToTruncate.dotdotdot({
ellipsis: '... ',
watch: true,
//wrap : 'letter',
height: 20 * 3, // max number of lines
after: '.readmore',
callback: function (isTruncated, orgContent) {
var $currentReadMore = $(this).find('.readmore');
var $currentReadLess = $(this).find('.readless');
if (isTruncated) {
$(this).addClass('securetext--is-truncated');
$(this).removeClass('securetext--is-not-truncated');
}
bindReadMore(); // bind click on "read more"
}
});
};
$(function () {
var addLink = $('.securetext');
addLink.append('<span class="readmore trigger-js"> <a >Read more</a></span>');
for (i = 0; i < addLink.length; i++) {
if ($(addLink[i]).children("p").length > 0) {
$(addLink[i]).children("p").last().append('<span class="readless trigger-js"> <a >Read less</a></span>');
} else {
$(addLink[i]).append('<span class="readless trigger-js"> <a >Read less</a></span>');
}
}
truncateIfNeeded(); // Initialize ellipsis
});