2014-04-15 7 views
-1

세로 및 가로로 두 개의 배경 이미지를 만들었습니다. 클라이언트가 화면을 회전 할 때 자동으로 브라우저 변경 배경 이미지를 원합니다. 불행히도, 내가 원하는대로 작동하지 않았다 ...브라우저가 자동으로 이미지를 변경하지 않습니다.

<script> 
    var stage = document.getElementById('stage'); 

    var width = screen.width; 
    var height = screen.height; 

    stage.style.width = width + 'px'; 
    stage.style.height = height + 'px'; 

    if (width < height) { 
     // portrait 
     stage.style.background = 'url(portrait.png) no-repeat'; 
    } else { 
     // landscape 
     stage.style.background = 'url(landscape.png) no-repeat'; 
    } 

    var supportsOrientationChange = "onorientationchange" in window, 
    orientationEvent = supportsOrientationChange ? "orientationchange" : "resize"; 

    window.addEventListener(orientationEvent, function() { 

     if (screen.width < screen.height) { 
      // portrait 
      stage.style.background = 'src(portrait.png) no-repeat'; 
      alert('Portrait'); 
     } else { 
      // landscape 
      stage.style.background = 'src(landscape.png) no-repeat'; 
      alert('Landscape'); 
     } 

    }, false); 

</script> 

당신이 그것을 개선하는 데 도움이 될 수 있습니까?

+0

콘솔에 오류 메시지가 있습니까? 또한, 왜 이것이 안드로이드에 태그되어 있습니까? 하이브리드 모바일 앱을 제작할 수는 있지만 그 태그는 거의 사용할 수 없습니다. –

답변

0

오리엔테이션 관련 스타일에 대해 CSS3 미디어 쿼리를 사용할 수 있습니다.

/* Portrait */ 
@media screen and (orientation:portrait) { 
    /* Portrait styles */ 
} 
/* Landscape */ 
@media screen and (orientation:landscape) { 
    /* Landscape styles */ 
} 
관련 문제