Skip to content

Instantly share code, notes, and snippets.

@suhaotian
Created November 29, 2016 06:57
Show Gist options
  • Save suhaotian/5dca230e7a4912d73b07dea72777575e to your computer and use it in GitHub Desktop.
Save suhaotian/5dca230e7a4912d73b07dea72777575e to your computer and use it in GitHub Desktop.
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