您的位置:首页 > Web前端 > JavaScript

js实现计算文字宽度

2016-05-17 08:50 417 查看
coffeescript转化之后

_textSize = (fontSize, text) ->
span = document.createElement("span")
span.style.fontSize = fontSize
result = {}
result.width = span.offsetWidth
result.height = span.offsetWidth
span.style.visibility = "hidden"
document.body.appendChild(span)
if not (typeof span.textContent is "undefined")
span.textContent = text
else
span.innerText = text
result = span.offsetWidth - result.width
span.parentNode.removeChild(span)
return result
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  coffeescript