Skip to content

Instantly share code, notes, and snippets.

@ysugimoto
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysugimoto/ce0004b8948712f3ef87 to your computer and use it in GitHub Desktop.
Save ysugimoto/ce0004b8948712f3ef87 to your computer and use it in GitHub Desktop.
複数行をellipsisする君
(function(global) {
'use strict';
function Ellipsis(element, trail) {
this.element = element;
this.exactHeight = element.offsetHeight;
this.text = element.textContent;
this.textSize = this.text.length;
this.trail = trail || '...';
}
Ellipsis.prototype.run = function() {
while ( this.isOverflow() ) {
this.addEllipsis();
}
this.element.style.height = this.exactHeight + 'px';
this.text = null;
};
Ellipsis.prototype.isOverflow = function() {
var originalHeight;
this.element.style.height = 'auto';
originalHeight = this.element.offsetHeight;
return originalHeight > this.exactHeight;
};
Ellipsis.prototype.addEllipsis = function() {
this.text = this.text.slice(0, this.textSize- this.trail.length);
this.element.textContent = this.text + this.trail;
this.textSize--;
};
global.Ellipsis = Ellipsis;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment