Created
November 29, 2016 06:57
-
-
Save suhaotian/5dca230e7a4912d73b07dea72777575e to your computer and use it in GitHub Desktop.
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 textHidden(el, x) { | |
var styles = window.getComputedStyle(el) | |
var h = parseFloat(styles.height.replace('px', '')) | |
var lh = styles.lineHeight | |
var fontSize = parseFloat(styles.fontSize.replace('px','')) | |
var text = el.innerText | |
var w = fontSize*0.625 | |
var ws = parseFloat(styles.width.replace('px', ''))/w | |
var lines = h/fontSize | |
if (x*fontSize >= h) return | |
console.log(ws, text) | |
var showText = text.slice(0,parseInt(ws*x) - 8) | |
el.innerHTML = showText + '... more' | |
el.onclick=function(){el.innerHTML = text} | |
} | |
textHidden(document.querySelector('p'), 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment