Guest User

Untitled

a guest
Jul 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. (function($) {
  2. $.fn.truncate = function(options) {
  3. options = $.extend({ limit: 50, more: '...', words: true }, options);
  4. return this.each(function() {
  5. var el = $(this), original = el.html(), truncated;
  6. if (options.words) {
  7. truncated = el.text().substr(0, options.limit + 1);
  8. var pos = truncated.lastIndexOf(' ');
  9. if (pos === -1) {
  10. pos = options.limit;
  11. }
  12. truncated = truncated.substr(0, pos);
  13. } else {
  14. truncated = el.text().substr(0, options.limit);
  15. }
  16. var link = $('<a href="#" class="more"></a>').html(options.more).click(function() {
  17. $(this).parent().html(original);
  18. return false;
  19. });
  20. el.html(truncated).append(link);
  21. });
  22. };
  23. })(jQuery);
Add Comment
Please, Sign In to add comment