Guest User

Untitled

a guest
Jan 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. new DottedText('.category-description p', {
  2. rows : 2,
  3. showMoreText: 'Mehr anzeigen',
  4. showLessText: 'Show Less'
  5. });
  6.  
  7. var DottedText = function(selector, options) {
  8. this.$element = $(selector);
  9. this.options = options;
  10. this.text = this.$element.text();
  11.  
  12. this.initialize();
  13. }
  14.  
  15. DottedText.prototype.initialize = function() {
  16. this.assingDynamicVars();
  17. this.assignEvents();
  18.  
  19. if (this.shouldLimit) {
  20. this.limitRows(0);
  21. }
  22. }
  23.  
  24. DottedText.prototype.assingDynamicVars = function() {
  25. this.elementLineHeight = parseInt(this.$element.css('line-height'));
  26. this.shouldLimit = this.$element.outerHeight() / this.elementLineHeight > this.options.rows;
  27. }
  28.  
  29. DottedText.prototype.limitRows = function(i) {
  30. this.updateHTML(i);
  31.  
  32. if (this.$element.outerHeight() / this.elementLineHeight > this.options.rows) {
  33. this.updateHTML(--i);
  34.  
  35. return;
  36. }
  37.  
  38. return this.limitRows(++i);
  39. }
  40.  
  41. DottedText.prototype.updateHTML = function(i) {
  42. this
  43. .$element
  44. .html(this.text.slice(0, i) + '... ' + '<span class="expand">' + this.options.showMoreText + '</span>');
  45. }
  46.  
  47. DottedText.prototype.assignEvents = function() {
  48. var _this = this;
  49.  
  50. $(window).on('resize', function() {
  51. _this
  52. .$element
  53. .html(_this.text);
  54.  
  55. _this.assingDynamicVars();
  56.  
  57. if (_this.shouldLimit) {
  58. _this.limitRows(0);
  59. }
  60. });
  61.  
  62. _this
  63. .$element
  64. .on('click', '.expand', function() {
  65. _this
  66. .$element
  67. .html(_this.text + (_this.options.showLessText ? ' <span class="collapse">' + _this.options.showLessText + '</span>' : ''));
  68. })
  69. .on('click', '.collapse', function() {
  70. _this.limitRows(0);
  71. });
  72. }
Add Comment
Please, Sign In to add comment