Skip to content

Instantly share code, notes, and snippets.

@cezarsmpio
Created December 12, 2016 21:18
Show Gist options
  • Save cezarsmpio/204516deb0fdf677bbe09ab5234294c2 to your computer and use it in GitHub Desktop.
Save cezarsmpio/204516deb0fdf677bbe09ab5234294c2 to your computer and use it in GitHub Desktop.
Overflow Text JS
<p data-overflow>Olá Daniel Nass, como está você meu filho?</p>
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);
});
@cezarsmpio
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment