Created
December 12, 2016 21:18
-
-
Save cezarsmpio/204516deb0fdf677bbe09ab5234294c2 to your computer and use it in GitHub Desktop.
Overflow Text JS
This file contains 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
<p data-overflow>Olá Daniel Nass, como está você meu filho?</p> |
This file contains 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
String.prototype.overflow = function (text = '...', start = 10, end = 4) { | |
let startText = this.substr(0, start); | |
let endText = this.substr(-Math.abs(end)); | |
if (this.length <= (parseInt(start) + parseInt(end) + text.length)) { | |
return this; | |
} | |
return `${startText.trim()}${text}${endText.trim()}`; | |
} | |
let overflowTexts = document.querySelectorAll('[data-overflow]'); | |
[].slice.call(overflowTexts).forEach(el => { | |
let text = el.getAttribute('data-overflow-text') || '...'; | |
let start = el.getAttribute('data-overflow-start') || 10; | |
let end = el.getAttribute('data-overflow-end') || 4; | |
el.innerText = el.innerText.overflow(text, start, end); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Demo Online: http://codepen.io/CezarLuiz0/pen/dOqgJO?editors=1010