Last active
August 29, 2015 14:07
-
-
Save ysugimoto/ce0004b8948712f3ef87 to your computer and use it in GitHub Desktop.
複数行をellipsisする君
This file contains hidden or 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
(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