Created
February 17, 2021 16:11
-
-
Save IgorGavrilenko/5863f2abc43246374b22e2484d8aa1f9 to your computer and use it in GitHub Desktop.
обрезка текста + кнопка еще
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// !!!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