怎么获的字体的宽高

      function textSize(fontSize,fontFamily,text){
        let span = document.createElement("span");
        let result = {};
        result.width = span.offsetWidth;
        result.height = span.offsetHeight;
        span.style.visibility = "hidden";
        span.style.position = "absolute";
        span.style.top="-200px";
        span.style.fontSize = fontSize;
        span.style.fontFamily = fontFamily;
        span.style.display = "inline-block";
        document.body.appendChild(span);
        if(typeof span.textContent != "undefined"){
          span.textContent = text;
        }else{
          span.innerText = text;
        }
        result.width = parseFloat(window.getComputedStyle(span).width) - result.width;
        result.height = parseFloat(window.getComputedStyle(span).height) - result.height;
        span.parentNode.removeChild(span);
        return result;
      }

 

posted @ 2018-08-23 14:49  ypm_wbg  阅读(343)  评论(0编辑  收藏  举报