UNPKG

zent

Version:

一套前端设计语言和基于React的实现

66 lines (65 loc) 2.17 kB
import createElement from './createElement'; export function getLineHeight(node, chars) { if (chars === void 0) { chars = '&nbsp;'; } var lnHeightStr = computedStyle(node, 'line-height'); var lnHeight = parseFloat(lnHeightStr); if (lnHeightStr === lnHeight + '') { var lnHeightStyle = node.style.lineHeight; node.style.lineHeight = lnHeightStr + 'em'; lnHeightStr = computedStyle(node, 'line-height'); lnHeight = parseFloat(lnHeightStr); if (lnHeightStyle) { node.style.lineHeight = lnHeightStyle; } else { delete node.style.lineHeight; } } if (lnHeightStr.indexOf('pt') !== -1) { lnHeight *= 4; lnHeight /= 3; } else if (lnHeightStr.indexOf('mm') !== -1) { lnHeight *= 96; lnHeight /= 25.4; } else if (lnHeightStr.indexOf('cm') !== -1) { lnHeight *= 96; lnHeight /= 2.54; } else if (lnHeightStr.indexOf('in') !== -1) { lnHeight *= 96; } else if (lnHeightStr.indexOf('pc') !== -1) { lnHeight *= 16; } lnHeight = Math.round(lnHeight); if (lnHeightStr === 'normal') { var nodeName = node.nodeName; var offScreenNode = createElement(nodeName); offScreenNode.innerHTML = "<span>" + chars + "</span>"; if (nodeName.toUpperCase() === 'TEXTAREA') { offScreenNode.setAttribute('rows', '1'); } var fontSizeStr = computedStyle(node, 'font-size'); var style = offScreenNode.style; style.fontSize = fontSizeStr; style.padding = '0px'; style.border = '0px'; style.color = 'transparent'; style.background = 'transparent'; style.position = 'absolute'; style.top = '-10000px'; style.left = '-10000px'; var body = document.body; body.appendChild(offScreenNode); var height = offScreenNode.offsetHeight; lnHeight = height; body.removeChild(offScreenNode); } return lnHeight; } function computedStyle(el, prop) { var style = getComputedStyle(el); return style.getPropertyValue(prop); }