jquery.ellipsis.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*! jQuery ellipsis - v1.1.1 - 2014-02-23
  2. * https://github.com/STAR-ZERO/jquery-ellipsis
  3. * Copyright (c) 2014 Kenji Abe; Licensed MIT */
  4. (function($) {
  5. $.fn.ellipsis = function(options) {
  6. // default option
  7. var defaults = {
  8. 'row' : 1, // show rows
  9. 'onlyFullWords': false, // set to true to avoid cutting the text in the middle of a word
  10. 'char' : '...', // ellipsis
  11. 'callback': function() {},
  12. 'keyword':null,
  13. 'position': 'tail' // middle, tail
  14. };
  15. options = $.extend(defaults, options);
  16. this.each(function() {
  17. // get element text
  18. var $this = $(this);
  19. var text = $this.text();
  20. var origText = text;
  21. var origLength = origText.length;
  22. var origHeight = $this.height();
  23. // get height
  24. $this.text('a');
  25. var lineHeight = parseFloat($this.css("lineHeight"), 10);
  26. var rowHeight = $this.height();
  27. var gapHeight = lineHeight > rowHeight ? (lineHeight - rowHeight) : 0;
  28. var targetHeight = gapHeight * (options.row - 1) + Math.max(rowHeight,lineHeight) * options.row;
  29. if (origHeight <= targetHeight) {
  30. $this.text(text);
  31. options.callback.call(this);
  32. return;
  33. }
  34. var start = 1, length = 0;
  35. var end = text.length;
  36. if(options.position === 'tail') {
  37. while (start < end) { // Binary search for max length
  38. length = Math.ceil((start + end) / 2);
  39. $this.text(text.slice(0, length) + options['char']);
  40. if ($this.height() <= targetHeight) {
  41. start = length;
  42. } else {
  43. end = length - 1;
  44. }
  45. }
  46. text = text.slice(0, start);
  47. if (options.onlyFullWords) {
  48. text = text.replace(/[\u00AD\w\uac00-\ud7af]+$/, ''); // remove fragment of the last word together with possible soft-hyphen characters
  49. }
  50. text += options['char'];
  51. }else if(options.position === 'middle') {
  52. var sliceLength = 0;
  53. while (start < end) { // Binary search for max length
  54. length = Math.ceil((start + end) / 2);
  55. sliceLength = Math.max(origLength - length, 0);
  56. $this.text(
  57. origText.slice(0, Math.floor((origLength - sliceLength) / 2)) +
  58. options['char'] +
  59. origText.slice(Math.floor((origLength + sliceLength) / 2), origLength)
  60. );
  61. if ($this.height() <= targetHeight) {
  62. start = length;
  63. } else {
  64. end = length - 1;
  65. }
  66. }
  67. sliceLength = Math.max(origLength - start, 0);
  68. var head = origText.slice(0, Math.floor((origLength - sliceLength) / 2));
  69. var tail = origText.slice(Math.floor((origLength + sliceLength) / 2), origLength);
  70. if (options.onlyFullWords) {
  71. // remove fragment of the last or first word together with possible soft-hyphen characters
  72. head = head.replace(/[\u00AD\w\uac00-\ud7af]+$/, '');
  73. }
  74. text = head + options['char'] + tail;
  75. }
  76. $this.text(text);
  77. options.callback.call(this);
  78. });
  79. return this;
  80. };
  81. }) (jQuery);