JS 로 이미지 크기 알아내기

<img id='Img1' src='http://www.phpschool.com/images/LOGO.gif' width='100' onclick='AlertImgSize(this)'>
<script>
function AlertImgSize(ElemId) {
      var Size = GetImageSize(Img1);
      alert('Width: ' + Size.Width + ', Height: ' + Size.Height);
}
</script>

<script>
function GetImageSize(ElemId) {
      with(TmpImg = document.body.appendChild(document.createElement('img'))) {
            src = ElemId.src;
            var Width = offsetWidth;
            var Height = offsetHeight;
      }
      document.body.removeChild(TmpImg);
      return { Width : Width, Height : Height };
}
</script>


<img src="어쩌구저쩌구" width="100" name="asdasd"> 
<script>
var img=new image();
img.src=document.all.asdasd.src;
alert(img.width);
</script>

 


원리

이미지의 SRC를 가져와서 새 이미지객체를 만든다.

새로 만들어진 이미지객체는 본래 이미지의 크기를 가진다.

그 크기를 알아낸다.

이미지 객체는 지운다.

댓글
  • No Nickname
    No Comment
  • 권한이 없습니다.
    {{m_row.m_nick}}
    -
목록형 📅 달력형