JavaScript를 사용하여 브라우저에서 Android 휴대폰 회전을 안정적으로 감지할 수 있는 방법은 무엇입니까?

Mary-Kate Olsen
풀어 주다: 2024-10-26 08:30:30
원래의
828명이 탐색했습니다.

How Can You Reliably Detect Android Phone Rotation in Browsers Using JavaScript?

JavaScript를 사용하여 브라우저에서 Android 휴대폰 회전 감지

JavaScript를 사용하여 iPhone에서 Safari의 화면 방향 및 변경 사항을 감지하는 것이 가능하지만 Android 휴대폰에서도 가능합니까?

Android의 반응형 회전 감지

Android 휴대폰에서 JavaScript를 사용한 회전 감지는 기기와 브라우저에 따라 동작이 다르기 때문에 복잡한 문제입니다. 크기 조정 또는 방향 변경 이벤트에만 의존하는 것은 신뢰할 수 없습니다.

신뢰할 수 있는 접근 방식

권장되는 접근 방식은 크기 조정 및 방향 변경 이벤트를 모두 모니터링하고 폴링 간격을 통해 방향 업데이트를 지속적으로 확인하는 것입니다. .

<code class="javascript">var previousOrientation = window.orientation;
var checkOrientation = function(){
    if(window.orientation !== previousOrientation){
        previousOrientation = window.orientation;
        // orientation changed, do your magic here
    }
};

window.addEventListener("resize", checkOrientation, false);
window.addEventListener("orientationchange", checkOrientation, false);

// (optional) Android doesn't always fire orientationChange on 180 degree turns
setInterval(checkOrientation, 2000);</code>
로그인 후 복사

기기별 동작

방향 변경에 대한 기기 반응은 크게 다릅니다. 다음은 네 가지 장치를 테스트하여 얻은 이벤트 순서 및 값에 대한 표입니다.

Device Events Fired orientation innerWidth screen.width
iPad 2 (to landscape) resize, orientationchange 0 1024 768
iPad 2 (to portrait) resize, orientationchange 90 768 768
iPhone 4 (to landscape) resize, orientationchange 0 480 320
iPhone 4 (to portrait) resize, orientationchange 90 320 320
Droid phone (to landscape) orientationchange, resize 90 320 320
Droid phone (to portrait) resize, orientationchange, resize 0 569 569
Samsung Galaxy Tablet (to landscape) orientationchange, orientationchange, resize 0 400 400
Samsung Galaxy Tablet (to portrait) orientationchange, orientationchange, orientationchange, resize 90 683 683

결론

방향 변경에 대한 이벤트 처리는 iOS와 Android 기기마다 다를 수 있지만 권장 접근 방식을 사용하면 JavaScript를 사용하는 Android 휴대폰에서 안정적인 방향 감지가 보장됩니다.

위 내용은 JavaScript를 사용하여 브라우저에서 Android 휴대폰 회전을 안정적으로 감지할 수 있는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!