2012-05-28 7 views
0

나는 재미있는 문제에 직면합니다. 나는 그 이유가 무엇인지 모릅니다. 인터넷에서 검색하려고했지만 솔루션이 없습니다. offsetWidth가 Chrome에서 완벽하게 작동하지만 offsetHeight가 제대로 작동하지 않는 것이 문제입니다. 때로는 값을 반환하지만 언젠가는 반환하지 않습니다. 이 문제를 해결할 수있는 해결책이 있는지 알고 싶습니다.크롬에서 offsetHeight가 제대로 작동하지 않습니다.

참고 : id.height, id.clientHeight를 사용하려고했습니다. 그러나 모두 나에게 똑같은 문제를주고있다. 내 프로젝트에서는 Prototype과 JQuery도 사용한다.

프로토 타입 getHeight()도 사용했지만 크롬에서는 작동하지 않습니다. getWidth()가 완벽하게 작동합니다.

var img = $('leftImage_<?php echo $iExtLoop; ?>'); 
var div = $('leftImageDiv'); 
var strip = $('stripLeft_<?php echo $iExtLoop; ?>'); 

img.style.position = "relative"; 
img.style.left = (div.offsetWidth - img.offsetWidth)/2 + "px"; 
img.style.top = (div.offsetHeight - img.offsetHeight)/2 + "px"; 

if (strip) { 
    strip.style.position = "relative"; 
    strip.style.left = (div.offsetWidth - strip.offsetWidth) + "px"; 
    strip.style.top= (div.offsetHeight - strip.offsetHeight) + "px"; 
} 
+1

몇 가지 코드를 게시하십시오. – Cristy

+0

코드 블록이 추가되었습니다 ... 어떤 아이디어입니까? –

+0

'$'는 무엇입니까? 프로토 타입 또는 jQuery? –

답변

0

가입일 :하지만 이러한 모든

이것은 SIME VIDAS에 의해 추가 된 코드 블록

$('leftImage_<?php echo $iExtLoop; ?>').style.position="relative"; 
$('leftImage_<?php echo $iExtLoop; ?>').style.left= ($('leftImageDiv').offsetWidth - $('leftImage_<?php echo $iExtLoop; ?>').offsetWidth)/2 + "px"; 
$('leftImage_<?php echo $iExtLoop; ?>').style.top= ($('leftImageDiv').offsetHeight - $('leftImage_<?php echo $iExtLoop; ?>').offsetHeight)/2 + "px"; 

if($('stripLeft_<?php echo $iExtLoop; ?>')){ 
    $('stripLeft_<?php echo $iExtLoop; ?>').style.position="relative"; 
    $('stripLeft_<?php echo $iExtLoop; ?>').style.left= ($('leftImageDiv').offsetWidth - $('stripLeft_<?php echo $iExtLoop; ?>').offsetWidth) + "px"; 
    $('stripLeft_<?php echo $iExtLoop; ?>').style.top= ($('leftImageDiv').offsetHeight - $('stripLeft_<?php echo $iExtLoop; ?>').offsetHeight) + "px";          
} 

리팩토링 코드입니다 :(... 파이어 폭스에서 잘 작동 적어도 하나의 요소가 IMG 요소 인 것처럼 보입니다. 코드가 실행될 때까지 요소가 완료되지 않았을 것으로 추측합니다.

이미지는 브라우저에서 별도로 검색해야하므로로드가 완료된 후에 만 ​​높이를 읽어야합니다.

+0

혼란스러운 죄송합니다. onload가 의미에서 작동하지 않으면 이미지 너비와 높이가 올바르게 계산됩니다. 그러나 그 스타일은 그 이미지로 설정되지 않습니다. 의미 : strip.style.position = "relative"; strip.style.left = (div.offsetWidth - strip.offsetWidth) + "px"; strip.style.top = (div.offsetHeight - strip.offsetHeight) + "px"; 이 코드 블록 부분이 실행되지 않거나 무언가입니다. 그러나 이미지가 필요한 위치에 배치되지 않았습니다. –

+0

@FaizulHasan 해당 줄에 중단 점을 설정하고 실행되는지 확인합니다. 그렇다면 할당 된 값을 확인하십시오. –

관련 문제