π¨ μ€ν¬λ‘€μ λΆλλ½κ² - Scroll Snap μμ±
CSS Scroll snap
CSS Scroll snapμ μΉ νμ΄μ§μμ μ€ν¬λ‘€μ ν λ, μμκ° μ€ν¬λ‘€λλ μμΉμ μλμΌλ‘ μ€λ λλλ‘ νλ CSS μμ±μ΄λ€.
μλ₯Όλ€μ΄ μ©μκ° μΉ νμ΄μ§λ₯Ό μ€ν¬λ‘€ν λ μ€κ°μ λ©μΆ°λ²λ¦¬λ©΄ μ½ν μΈ μ μ€κ°μμ λ©μΆ° μ£Όμ μ½ν μΈ μ μΌλΆλ§ 보μ΄κ² λλλ°, μ΄λ₯Ό 미리 μ€μ ν μμΉλ‘ μλ μ€λ νμ¬ μμ°μ€λ¬μ΄ μ€ν¬λ‘€ μμ§μκ³Ό ν¨κ» μ¬μ©μ κ²½νμ λμ± ν₯μμν€λ κ²μ΄λ€.
μ€ν¬λ‘€ μ€λ μ΄ μλ μμ μλ μλ°μ€ν¬λ¦½νΈλ‘ μΌμΌν μμ μμΉλ₯Ό κ³μ°ν΄μ μ‘°μ ν΄μΌ λ¬μ§λ§, CSS μμ±μ ν΅ν΄ 곡μ μ§μν¨μΌλ‘μ¨ κ°λ°μ νΈλ¦¬ν¨κ³Ό λλΆμ΄ μ±λ₯λ λΉμ½μ μΌλ‘ μμΉνλ€κ³ λ³Ό μ μλ€.
Scroll snap μ¬μ©λ²
μ°μ μ€ν¬λ‘€ μ€λ
μ μ μ©ν λΆλͺ¨ 컨ν
μ΄λ μμμ λ§λ€κ³ , κ·Έ μμ μ€λ
λ μμ μμ μμμ λ§λ€μ΄ μ€λ€. κ·Έλ¦¬κ³ λΆλͺ¨μ scroll-snap-type μμ±κ³Ό μμμ scroll-snap-align μμ±μ μ μ©ν΄μ£Όλ©΄ λλ€. (μ΄λ€ μμ±μ λν΄μλ μλ μμΈν λ€λ£¬λ€)
<div class="scroll-container">
<div class="scroll-area">1</div>
<div class="scroll-area">2</div>
<div class="scroll-area">3</div>
<div class="scroll-area">4</div>
</div>
/* λΆλͺ¨ μ€ν¬λ‘€ μ€λ
컨ν
μ΄λ */
.scroll-container {
overflow: auto;
scroll-snap-type: y mandatory; /* y μΆ λ°©ν₯μΌλ‘λ§ scroll snap μ μ© */
}
/* μμ μ€ν¬λ‘€ μ€λ
μμ */
.scroll-area {
scroll-snap-align: start; /* μ€ν¬λ‘€ μμΉ λ§μΆ€ */
}
/* μ¬κΈ°μλΆν΄ λ¨μ κΎΈλ―ΈκΈ° --------------------------- */
.scroll-area:nth-of-type(1) {
background: #49b293;
}
.scroll-area:nth-of-type(2) {
background: #c94e4b;
}
.scroll-area:nth-of-type(3) {
background: #4cc1be;
}
.scroll-area:nth-of-type(4) {
background: #8360A6;
}
See the Pen Scroll Snap Demo by Envato Tuts+ (@tutsplus) on CodePen.
Scroll snap μμ±
Scroll snapμλ μ¬μ©ν μ μλ 4 κ°μ§ μμ±μ΄ μλ€.
- scroll-snap-type
- scroll-snap-align
- scroll-padding
- scroll-margin
μλμ CSS μμλ λ€μ HTML μλ¦¬λ¨ΌνΈ κ΅¬μ±μ κΈ°μ€μΌλ‘ μ€λͺ νλ€.
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
scroll-snap-type
scroll-snap-type μμ±μ μ€ν¬λ‘€ μ€λ
μ΄ λμνλ λ°©μμ μ§μ νλ€. 첫 λ²μ§Έ μΈμλ‘ μ€λ
μ΄ μ μ©λ μΆμ μ§μ νκ³ , λ λ²μ§Έ μΈμλ‘ μ€λ
μ μ© λ°©μμ μ§μ νλ€.
.container {
scroll-snap-type : [scroll snap axis], [scroll snap strictness];
}
/* y λ°©ν₯μΌλ‘ μ€ν¬λ‘€ μ€λ
μ μ μ© */
.container {
scroll-snap-type: y mandatory;
}
/* x λ°©ν₯μΌλ‘ μ€ν¬λ‘€ μ€λ
μ μ μ© */
.container {
scroll-snap-type: x proximity;
}
[ scroll snap axis ]
- x: μν(κ°λ‘) μΆμΌλ‘ snap position μ§μ
- y: μμ§(μΈλ‘) μΆμΌλ‘ snap position μ§μ
- block: snap areaμ block μΆμΌλ‘ μ§μ
- inline: inline μΆμΌλ‘ μ§μ
- both: λ μΆμ μμΉλ₯Ό κ°λ³μ μΌλ‘ μ€λ .
[ scroll snap strictness ]
- none: μ€λ νμ§ μμ.
- proximity: μ€ν¬λ‘€ μμΉκ° μ€λ μμΉμ κ°κΉμμ§λ©΄ μμ°μ€λ½κ² μ€λ
- mandatory: μ€ν¬λ‘€ μμΉκ° μ€λ μμΉμ μ νν μΌμΉν΄μΌλ§ μ€λ
scroll-snap-type μμ± κ°μ mandatory λ‘ μ μΈ μ μ£Όμν μ μ, μ½ν μΈ κ°μ κ°κ²©μ΄ λμ λ κ°μ λ‘ μ€λ μ νκ² λλ©΄ μ€κ° μ½ν μΈ λ₯Ό 건λλ°κ³ λ€μ μ½ν μΈ λ‘ μ΄λνλ κ²½μ°κ° λ°μν μ μλ€λ μ μ΄λ€.
See the Pen scroll snap by barzz12 (@inpaSkyrim) on CodePen.
scroll-snap-align
scroll-snap-align μμ±μ μ€ν¬λ‘€ μ€λ
μ΄ μ μ©λλ μμκ° μ€λ
μμΉμ μ λ ¬λλ λ°©μμ μ§μ νλ€. scroll-snap-typeμμ μ§μ ν μΆμ κΈ°μ€μΌλ‘ snap areaμ μ λ ¬μ μ νλ€.
- none: snap positionμ μ§μ νμ§ μμ
- start: μ€ν¬λ‘€ μ€λ μμΉλ₯Ό snap area μμ λΆλΆμ λ§μΆλλ‘ μ§μ
- end: μ€ν¬λ‘€ μ€λ μμΉλ₯Ό snap areaμ λ λΆλΆμ λ§μΆλλ‘ μ§μ
- center: μ€ν¬λ‘€ μ€λ μμΉλ₯Ό snap areaμ κ°μ΄λ° λΆλΆμ λ§μΆλλ‘ μ§μ
/* μ€λ
μμΉμ λ§μΆ° μμ μμ λΆλΆμ μ λ ¬ */
.item {
scroll-snap-align: start;
}
/* μ€λ
μμΉμ λ§μΆ° μμ μ€μ λΆλΆμ μ λ ¬ */
.item {
scroll-snap-align: center;
}
/* μ€λ
μμΉμ λ§μΆ° μμ λ λΆλΆμ μ λ ¬ */
.item {
scroll-snap-align: end;
}
scroll-padding
scroll-padding μμ±μ μ€ν¬λ‘€ μμμ ν¨λ©(padding)μ μ‘°μ νλ μμ±μ΄λ€. μ΄ μμ±μ μ¬μ©νλ©΄ μ€ν¬λ‘€ μ€λ
μ΄ μ μ©λλ μμμ ν보ν μ μλ€.
μ λ§λ‘ μμμ padding κ°μ΄ λ³κ²½λλ κ²μ΄ μλκ³ , ν΄λΉ λ·° ν¬νΈ(viewport)μ κ°μ§ paddingμ΄ μ μ©λλ κ²μ΄λ€.
/* μ€ν¬λ‘€ μμ μ/μλλ‘ ν¨λ© 100px μΆκ° */
.container {
scroll-snap-type: y mandatory;
scroll-padding: 100px 0 100px 0; /* `scroll-padding-top`, `scroll-padding-right`, `scroll-padding-bottom`, `scroll-padding-left` */
}
/* μ€ν¬λ‘€ μμ μ/ν/μ’/μ° λͺ¨λμ ν¨λ© 50px μΆκ° */
.container {
scroll-snap-type: y mandatory;
scroll-padding: 50px;
}
scroll-margin
scroll-padding κ³Ό λ§μ°¬κ°μ§λ‘ λ·° ν¬νΈμ κ°μ§ margin μμμ μ§μ νλ μμ±μ΄λ€.
/* μ€ν¬λ‘€ μ€λ
μ΄ μ μ©λλ μμμ μ/ν/μ’/μ° λ§μ§μ 100pxλ‘ μ§μ */
.container {
scroll-margin: 100px; /* `scroll-margin-top`, `scroll-margin-right`, `scroll-margin-bottom`, `scroll-margin-left` */
}
/* μ€ν¬λ‘€ μ€λ
μ΄ μ μ©λλ μμμ μ/ν λ§μ§μ 50px, μ’/μ° λ§μ§μ 100pxλ‘ μ§μ */
.container {
scroll-margin: 50px 100px;
}
μ΄λ¬ν scroll-padding , scroll-margin μμ±μ μμ©νμλ©΄, μ΄μ νΉμ λ€μ μ½ν
μΈ μ λν μμ μλ리μ€λ₯Ό μ¬μ©μμκ² μ 곡ν μ μλ€.
scroll-snap-stop
scroll-snap-stop μμ±μ μ€ν¬λ‘€ μ€λ μμΉμ λλ¬νμ λ μ€ν¬λ‘€μ μ€μ§ν μ§ μ¬λΆλ₯Ό μ§μ νλ€.
- normal: μ€ν¬λ‘€μ΄ μ€λ λλλΌλ κ³μ μ§νλ μ μμ
- always: μ€ν¬λ‘€μ΄ μ€λ μμΉμ λλ¬νλ©΄ μ€μ§
/* μ€ν¬λ‘€ μ€λ
μ΄ μμμ λμμ μ€λ¨λλλ‘ μ§μ */
.item {
scroll-snap-stop: always;
}
/* μ€ν¬λ‘€ μ€λ
μ΄ μμμ μ€μμμλ§ μ€λ¨λλλ‘ μ§μ */
.item {
scroll-snap-stop: normal;
}
# μ°Έκ³ μλ£
https://wit.nts-corp.com/2018/08/28/5317
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Scroll_Snap