2009-07-24 6 views
2

내가 무슨 뜻인지 알 겠어? 그래서, 7px 또는 어떤 : 그 DIV 글꼴 크기를 갖고 싶은 경우에,텍스트가 오버플로되지 않도록 요소의 font-size를 자동으로 설정하는 Javascript?

<div style="width:100px;font-size:10px">Some Text</div> 

을하지만 우리는 또한 가능성이 사업부에서 더 이상 텍스트 문자열을 할 수 : 등의 우리가 있다고 가정 해 봅시다 전체 문자열이 넘치지 않고 맞습니다.

이미 작성된 것과 같은 것이 확실합니다. 다시 작성하고 싶지는 않습니다. 가급적 jQuery 플러그인을 사용 하시겠습니까?

의견을 보내 주시면 감사하겠습니다. 닉의 질문에 제안 된 답변을 바탕으로 감사

+0

도움이 될 수도 있습니다 : http://stackoverflow.com/questions/118241/calculate-text-width-with-javascript – Nosredna

+0

이 질문은 [이전에 요청한] (http://stackoverflow.com)과 매우 유사합니다./questions/1177320/programmatically-determine-font-size-for-single-line-display/1177753 # 1177753). –

답변

5

:

$(function() { 
    $(".shrinkByWidth").each(function() { 
     var targetWidth = $(this).parent(':first').width(); 

     if (isNaN(parseInt($(this).css("font-size")))) 
      $(this).css("font-size", '24px'); 

     while ($(this).width() > targetWidth) 
     { 
      $(this).css("font-size", (parseInt($(this).css("font-size")) - 1) + 'px'); 
     } 
    }); 
}); 

나를 위해 좋은 작품!

+0

수정 된 솔루션을 게시 주셔서 감사합니다 :) –

관련 문제