๐Ÿ” ๋ชจ๋ฐ”์ผ์—์„œ ๋ทฐํฌํŠธ ํ•˜๋‹จ์— ์š”์†Œ๋ฅผ ๊ณ ์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

3872 ๋‹จ์–ด webdevjavascript
์•ˆ๋…•ํ•˜์„ธ์š” ๐Ÿ‘‹๐Ÿผ

์–ผ๋งˆ ์ „๊นŒ์ง€๋งŒ ํ•ด๋„ ๋ทฐํฌํŠธ ํ•˜๋‹จ์˜ ๋ฒ„ํŠผ ์œ„์น˜๋ฅผ ๊ณ ์ •ํ•˜๋Š” ์ž‘์—…์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” Figma์—์„œ ๋ทฐํฌํŠธ ํ•˜๋‹จ์— ๋ถ™์ผ ๋งค์šฐ ์•„๋ฆ„๋‹ค์šด ๋ฒ„ํŠผ์„ ๊ทธ๋ฆฌ๋Š” ๋ฐ ์‹œ๊ฐ„์„ ๋ณด๋ƒˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ธ€์„ ์“ฐ๋Š” ์ˆœ๊ฐ„

.beautiful-button {
    position: fixed;
    bottom: 0;
}


๊ทธ๋ฆฌ๊ณ ... ํšจ๊ณผ๊ฐ€ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค!

๋‚˜๋Š” ํ•ด๊ฒฐ๋œ ์ผ์— ๋งŒ์กฑํ–ˆ๊ณ  ๋‹ค๋ฅธ ์ผ์„ ํ•˜๋Ÿฌ ๊ฐ”๋‹ค. ํ•„๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๊ธฐ ์ „๊นŒ์ง€๋Š” ๋ชจ๋“  ๊ฒƒ์ด ๊ดœ์ฐฎ์•˜์Šต๋‹ˆ๋‹ค. ํด๋ฆญํ•˜๋ฉด ํ‚ค๋ณด๋“œ๊ฐ€ ๋ณด์ด๊ณ  ๋‚ด ๋ฒ„ํŠผ์ด ์‚ฌ๋ผ์กŒ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋„ˆ๋ฌด ์ŠฌํŽ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌธ์ œ๋Š” ์•ˆ์ „ํ•œ ์˜์—ญ์— ์žˆ์ง€ ์•Š์„๊นŒ์š”? ๋‚˜๋Š” ์ด๊ฒƒ์„ ์‹œ๋„ํ•œ๋‹ค

.beautiful-button {
    position: fixed;
    bottom: 0;
    bottom: calc(0 + env(safe-area-inset-bottom));
}


์ด๊ฒƒ๋„ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋‚˜๋Š” Safari๊ฐ€ Safari๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ๋‹ค์Œ์œผ๋กœ ์—ฐ๊ตฌ๋ฅผ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ฐ€์žฅ ๋จผ์ € ๋– ์˜ค๋ฅธ ํ•ด๊ฒฐ์ฑ…์€ ๋ทฐํฌํŠธ์˜ ๋†’์ด๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด์—ˆ์Šต๋‹ˆ๋‹ค.

const viewportHeight = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0);


๊ทธ๋ฆฌ๊ณ  ์ด๊ฒƒ์€ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ‚ค๋ณด๋“œ๊ฐ€ ์žˆ๋Š” ๊ฒƒ๊ณผ ์—†๋Š” ๋ทฐํฌํŠธ ๋†’์ด๋Š” ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

์ธํ„ฐ๋„ท ๊ฒ€์ƒ‰๊ณผ ๋™๋ฃŒ์˜ ์งˆ๋ฌธ์„ ๋ฐ›์€ ํ›„ Visual Viewport API( https://developer.mozilla.org/en-US/docs/Web/API/Visual_Viewport_API )์— ๋Œ€ํ•ด ์•Œ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋ทฐํฌํŠธ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” API๊ฐ€ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค.
The visual viewport is the visual portion of a screen excluding on-screen keyboards, areas outside of a pinch-zoom area, or any other on-screen artifact that doesn't scale with the dimensions of a page. (MDN) .

๊ทธ๋ž˜์„œ ๋‚ด ์†”๋ฃจ์…˜์€ ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค

window.visualViewport.height

window.visualViewport๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฒคํŠธ๋ฅผ ์ˆ˜์‹ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

window.visualViewport.addEventListener('resize', resizeHandler);
window.visualViewport.addEventListener('scroll', scrollHandler);


๊ทธ๋ฆฌ๊ณ  e.target๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ทฐํฌํŠธ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

๊ทธ๊ฒŒ ๋‹ค์•ผ! ๋‚ด ์•„๋ฆ„๋‹ค์šด ๋‹จ์ถ”๊ฐ€ ์ด์ œ ์žˆ์–ด์•ผ ํ•  ์œ„์น˜์— ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค โค๏ธ

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ