Last active
August 12, 2016 08:41
-
-
Save eulereadgbe/cbf9abea7161eba032b6013504fb3453 to your computer and use it in GitHub Desktop.
Integration of jquery dotdotdot plugin to abstracts
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
$('.truncable-txt').append('<span class="read-more trigger-js"> <a class="read-more__txt small">[+] Show more</a></span>' + | |
'<span class="read-less trigger-js"> <a class="read-less__txt small">[-] Show less</a></span>'); | |
var bindReadMore = function(){ | |
$('.read-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(){ | |
$('.read-less').on('click', function(e) { | |
e.preventDefault(); | |
var parent = $(this).parent(); | |
var divPosition = parent.offset(); | |
if (parent.length) { | |
var scrollToPosition = parseInt(divPosition.top) - parseInt($('.navbar').outerHeight()); //needed for fixed navbar | |
$('html, body').animate({ | |
scrollTop: scrollToPosition | |
}, 1000);} | |
truncateIfNeeded(parent); // Re-initialize ellipsis | |
}); | |
}; | |
var truncateIfNeeded = function(jqueryTag){ | |
var $selectionToTruncate = jqueryTag || $('.truncable-txt'); | |
$selectionToTruncate.dotdotdot({ | |
ellipsis: '...', | |
watch : true, | |
wrap : 'letter', | |
height : 20 * 3, // max number of lines | |
after : '.read-more', | |
callback: function( isTruncated, orgContent ) { | |
var $currentReadMore = $(this).find('.read-more'); | |
var $currentReadLess = $(this).find('.read-less'); | |
if( isTruncated ){ | |
$(this).addClass('truncable-txt--is-truncated'); | |
} | |
bindReadMore(); // bind click on "read more" | |
} | |
}); | |
}; | |
$(function() { | |
truncateIfNeeded(); // Initialize ellipsis | |
}); |
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
.trigger-js { | |
cursor: pointer; | |
cursor: hand; | |
} | |
.read-less, | |
.read-more { | |
display: none; | |
} | |
.read-more__txt, | |
.read-less__txt { | |
text-decoration: underline; | |
} | |
.truncable-txt--is-truncated .read-more, | |
.truncable-txt--is-not-truncated .read-less { | |
display: inline-block; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment