업데이트
죄송합니다는, 당신의 소스 첫에보고해야합니다. 다음은 코드를 변경해야하는 방법입니다. 중요한 점은 이미지가 완전히로드 될 때까지 너비를 얻을 수 없다는 것입니다.
// Create the img element, add a load handler, then append it to the body
var $img = $('<img class="zoomIcon" alt="zoom icon" />').load(function(e){
// get icon image properties for use with animation
var imageWidth = $(this).width();
var imageHeight = $(this).height();
// ... any code that uses these variables needs to go in here ...
}).attr('src', zoomImage).appendTo(document.body);
원래 답 : :이 일반적인 정보입니다 내가 너비를 검색 할 load
이벤트 콜백을 사용하는 이유입니다.
이 코드를 document.ready
에 넣으면 이미지가로드되기 전에 너비를 검색하려고합니다. 대신이 시도 :
$(window).load(function(){
var imageWidth = $(".zoomer").width();
alert(imageWidth); // Should be correct
});
자바 스크립트와 이미지의 문제들이 이미지 다운로드 될 때까지 경계를 알 수 없다는 것입니다. 일단 이미지로드가로드되면 알아낼 수있는 .load 함수를 사용할 수 있습니다. – amphetamachine