Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IgorGavrilenko/5863f2abc43246374b22e2484d8aa1f9 to your computer and use it in GitHub Desktop.
Save IgorGavrilenko/5863f2abc43246374b22e2484d8aa1f9 to your computer and use it in GitHub Desktop.
обрезка текста + кнопка еще
// !!!dotdotdot-js-2.0.1
/*
<div class="truncable-txt">
text, text, text
<span class="truncable-txt__more">more</span>
<span class="truncable-txt__less">less</span>
</div>
<style>
.truncable-txt--is-truncated .truncable-txt__more,
.truncable-txt--is-not-truncated .truncable-txt__less {
display: block;
}
</style>
*/
// truncateFuncInit
$(truncateFuncInit);
function truncateFuncInit(){
var bindReadMore = function(){
$('.truncable-txt__more').on('click', function(e) {
e.preventDefault();
var parent = $(this).parent();
parent.trigger("destroy");
parent.removeClass('truncable-txt--is-truncated');
parent.addClass('truncable-txt--is-not-truncated');
bindReadLess(); // bind click on "less"
});
};
var bindReadLess = function(){
$('.truncable-txt__less').on('click', function(e) {
e.preventDefault();
var parent = $(this).parent();
truncateIfNeeded(parent); // Re-initialize ellipsis
});
};
var truncateIfNeeded = function(jqueryTag){
var $selectionToTruncate = jqueryTag || $('.truncable-txt');
$selectionToTruncate.dotdotdot({
ellipsis: '...',
watch : true,
wrap : 'letter',
height : 10 * 3, // max number of lines
after : '.truncable-txt__more',
callback: function( isTruncated, orgContent ) {
var $currentReadMore = $(this).find('.truncable-txt__more');
var $currentReadLess = $(this).find('.truncable-txt__less');
if( isTruncated ){
$(this).addClass('truncable-txt--is-truncated');
}
bindReadMore(); // bind click on "read more"
},
});
};
truncateIfNeeded(); // Initialize ellipsis
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment