visual viewport : viewport 중에서 현재 보여지는 부분으로, layout viewport보다 작아질 수 있습니다.
사용자가 줌인, 줌아웃 등의 작업을 할 때, layout viewport 영역은 그대로 남아있지만, visual viewport는 작아질 수 있습니다.
let pendingUpdate = false;
function viewportHandler(event) {
if (pendingUpdate) return;
pendingUpdate = true;
requestAnimationFrame(() => {
pendingUpdate = false;
const layoutViewport = document.getElementById('layoutViewport');
// Since the bar is position: fixed we need to offset it by the
// visual viewport's offset from the layout viewport origin.
const viewport = event.target;
const offsetLeft = viewport.offsetLeft;
const offsetTop = viewport.height
- layoutViewport.getBoundingClientRect().height
+ viewport.offsetTop;
// You could also do this by setting style.left and style.top if you
// use width: 100% instead.
bottomBar.style.transform = `translate(${offsetLeft}px, ${offsetTop}px) scale(${1 / viewport.scale})`;
});
}
window.visualViewport.addEventListener('scroll', viewportHandler);
window.visualViewport.addEventListener('resize', viewportHandler)
Android에서는 키보드가 열리면 window.visualViewport.height와 window.innerHeight가 같이 줄어들지만, iOS에서는 visualViewport값이 독립적으로 변한다.
기본적으로 데스크탑 브라우저에서는 viewport 메타 태그를 사용하지 않기 때문에 무시됩니다.
<meta name="viewport" content="width=device-width, inital-scale=1.0">
html 스니펫을 통해 입력하는 경우에도 위의 코드가 자동으로 삽입 됩니다.
device-width
와 같은 특정한 값을 사용할 수도 있습니다. device-width
는 100% 스케일에서 CSS 픽셀들로 계산된 화면의 폭을 의미합니다.https://developer.mozilla.org/en-US/docs/Web/API/Visual_Viewport_API
https://developer.mozilla.org/en-US/docs/Web/API/Visual_Viewport_API
https://sungchuni.tistory.com/20