CSS ์ ๋๋ฉ์ด์ ๋ฐ ํธ๋์ง์ ํธ๋ฆฌ๊ฑฐ
CSS ์ ๋๋ฉ์ด์ ์ ์น ์์๊ฐ ํ๋์ CSS ์คํ์ผ ๊ตฌ์ฑ์์ ๋ค๋ฅธ ๊ตฌ์ฑ์ผ๋ก ์ ํํ๋ ํธ๋์ง์ ์ ์ง์ํฉ๋๋ค. ๋ธ๋ผ์ฐ์ ๋ ๋ก๋ ์ ์ ์๋ ์ ๋๋ฉ์ด์ ์ ์์ํ ์ ์์ง๋ง CSS ์ ๋๋ฉ์ด์ ์ ํธ๋ฆฌ๊ฑฐํ ์ด๋ฒคํธ๋ ํด๋์ค ์ถ๊ฐ ๋ฐ ์ ๊ฑฐ์ ์์กดํฉ๋๋ค. AMP๋ ๋ ๊ฐ์ง ์ ๋๋ฉ์ด์ ์ ํ์ ๋ชจ๋ ์ง์ํฉ๋๋ค.
์ ํํ ์๊ฐ ์ง์ ์ด ํ์ํ์ง ์์ผ๋ฉฐ ํฌ๊ธฐ๊ฐ ์๊ณ ์ ํ๋ ์ ๋๋ฉ์ด์ ์๋ CSS๋ฅผ ์ฌ์ฉํ์ธ์.
CSS ๋ฐ ํค ํ๋ ์ ์ ์
๋ค์๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก AMP์์ CSS๋ฅผ ์ ์ํ ์ ์์ต๋๋ค.
- ๋ฌธ์ ํค๋์
<style amp-custom>ํ๊ทธ์์. 75,000 ๋ฐ์ดํธ ์ ํ. - ์ธ๋ผ์ธ ์คํ์ผ. ์ธ๋ผ์ธ ์คํ์ผ์ ๊ฐ ์ธ์คํด์ค๋ 1,000 ๋ฐ์ดํธ ์ ํ์ด ์์ผ๋ฉฐ ์ธ๋ผ์ธ ์คํ์ผ์ 75,000 ๋ฐ์ดํธ
<style amp-custom>์ ํ์ ํฌํจ. - ๋ฌธ์ ํค๋์
<style amp-keyframes>ํ๊ทธ์์. 500,000 ๋ฐ์ดํธ ์ ํ. ํค ํ๋ ์ ์์ฑ์ผ๋ก ํ์ .
ํ์ด์ง๋ฅผ ๊ฐ๊ฒฐํ๊ณ ๋น ๋ฅด๊ฒ ์ ์งํ๊ธฐ ์ํด AMP๋ <amp style-custom> ํ๊ทธ์์ CSS ํฌ๊ธฐ๋ฅผ 75,000 ๋ฐ์ดํธ๋ก ์ ํํ์ต๋๋ค. ์ด๋ฅผ ์ฌ์ฉํ์ฌ ์ ๋๋ฉ์ด์
์คํ์ผ์ ์ ์ํ ์ ์์ง๋ง <amp style-keyframes> ํ๊ทธ์ 500,000 ๋ฐ์ดํธ ์ ํ์ ๊ท์คํ ์ฌ์ดํธ ์คํ์ผ ๋ฆฌ์์ค๋ฅผ ์๋ชจํ์ง ์๋ ์ ๊ตํ ์ ๋๋ฉ์ด์
์ ํ์ฉํฉ๋๋ค.
<style amp-custom>
div {
width: 100px;
height: 100px;
background: red;
position: relative;
animation: mymove 5s infinite;
}
</style>
</head>
<body>
<div></div>
<style amp-keyframes>
@keyframes mymove {
0% {transform: translatey(0px);}
25% {transform: translatey(200px);}
75% {transform: translatey(50px);}
100% {transform: translatey(100px);}
}
</style>
</body>
ํด๋์ค ์ถ๊ฐ, ์ ๊ฑฐ ๋ฐ ์ ํ
AMP ์ก์
toggleClass๋ฅผ ํ์ฉํ๋ฉด ์ ์๋ ์์์ ํด๋์ค๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํ ์ ์์ต๋๋ค.
elementName.toggleClass(class="className")
์ ๋๋ฉ์ด์ ํ๋ฒ๊ฑฐ ๋ฉ๋ด์ ๊ฐ์ด ์ฌ์ฉ์ ์ธํฐ๋์ ์ ํ์ฉํ ๋์ผํ ์์์์ ํด๋์ค๋ฅผ ์ ํํ ์ ์์ต๋๋ค.
<div
id="hamburger"
tabindex="1"
role="button"
on="tap:hamburger.toggleClass(class='close')"
></div>
force ์์ฑ์ ์ถ๊ฐํ๋ฉด toggleClass ์ก์
์ด ๋ค๋ฅธ ์์์ ์ ์ฉ๋๊ฑฐ๋ ๋ ํด๋์ค ๊ฐ ์ ํ์ด ๊ฐ๋ฅํฉ๋๋ค.
<button
on="tap:magicBox.toggleClass(class='invisible', force=true),magicBox.toggleClass(class='visible', force=false)"
>
Disappear
</button>
<button
on="tap:magicBox.toggleClass(class='visible', force=true),magicBox.toggleClass(class='invisible', force=false)"
>
Reappear
</button>
ํด๋์ค๋ฅผ ์ ๊ฑฐํ๊ณ ์ฌ์ ์ฉ์ ํ์ฉํ์ง ์์ผ๋ ค๋ ๊ฒฝ์ฐ false ๊ฐ์ force ์์ฑ์ ์ถ๊ฐํฉ๋๋ค. ํด๋์ค๋ฅผ ์ถ๊ฐํ๊ณ ์ ๊ฑฐ๋ฅผ ํ์ฉํ์ง ์์ผ๋ ค๋ ๊ฒฝ์ฐ true ๊ฐ์ force ์์ฑ์ ์ถ๊ฐํฉ๋๋ค.
CSS ๋ฐ ์ํ
amp-bind๋ฅผ ์ฌ์ฉํ์ฌ ์ํ๊ฐ ์ง์ ๋ CSS ํด๋์ค๋ฅผ ๊ฐ์์ ์๊ด์์ด ์ถ๊ฐ ๋๋ ์ ๊ฑฐํ ์ ์์ต๋๋ค.
<head>
<script
async
custom-element="amp-bind"
src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"
></script>
<style amp-custom>
div {
height: 100px;
width: 100px;
margin: 1em;
background-color: green;
margin-left: 100px;
transition: 2s;
}
.visible {
opacity: 1;
}
.invisible {
opacity: 0;
}
.left {
transform: translatex(-50px);
}
.right {
transform: translatex(50px);
}
button {
margin-top: 1rem;
margin-left: 1rem;
}
</style>
</head>
<body>
<amp-state id="magicBox">
<script type="application/json">
{
"visibleBox": {
"className": "visible"
},
"invisibleBox": {
"className": "invisible"
},
"moveLeft": {
"className": "left"
},
"moveRight": {
"className": "right"
}
}
</script>
</amp-state>
<div [class]="magicBox[animateBox].className"></div>
<button on="tap:AMP.setState({animateBox: 'invisibleBox'})">Disappear</button>
<button on="tap:AMP.setState({animateBox: 'visibleBox'})">Reappear</button>
<button on="tap:AMP.setState({animateBox: 'moveLeft'})">Move Left</button>
<button on="tap:AMP.setState({animateBox: 'moveRight'})">Move Right</button>
</body>
์ฐ์ ๋ฌธ์ head์ <style amp-custom> ํ๊ทธ์ CSS ํด๋์ค ๋ชฉ๋ก์ ์ถ๊ฐํ์ฌ ์ฌ๋ฌ ํด๋์ค ์ ๋๋ฉ์ด์
์ ์ ์ํฉ๋๋ค.
.visible {
opacity: 1;
}
.invisible {
opacity: 0;
}
.left {
transform: translatex(-50px);
}
.right {
transform: translatex(50px);
}
๋ค์์ผ๋ก ํด๋์ค์ ์ํ์ ํ์ด๋ฅผ ์ค์ ํฉ๋๋ค.
<amp-state id="magicBox">
<script type="application/json">
{
"visibleBox": {
"className": "visible"
},
"invisibleBox": {
"className": "invisible"
},
"moveLeft": {
"className": "left"
},
"moveRight": {
"className": "right"
}
}
</script>
</amp-state>
์์์ ํด๋์ค๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค.
<div [class]="magicBox[animateBox].className"></div>
์ฐ๊ฒฐ๋ AMP ์ก์ ๋๋ ์ด๋ฒคํธ์์ ์ํ๊ฐ ๋ณ๊ฒฝ๋ฉ๋๋ค. ๋ค์ ์์๋ ์ฌ์ฉ์ ์ธํฐ๋์ ์ ์ํ๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค.
<button on="tap:AMP.setState({animateBox: 'invisibleBox'})">Disappear</button>
<button on="tap:AMP.setState({animateBox: 'visibleBox'})">Reappear</button>
<button on="tap:AMP.setState({animateBox: 'moveLeft'})">Move Left</button>
<button on="tap:AMP.setState({animateBox: 'moveRight'})">Move Right</button>
์ด๋ฌํ ๋ฐฉ์์ผ๋ก amp-bind๋ฅผ ์ฌ์ฉํ๋ฉด ์ ์๋ ํด๋์ค์ ํด๋์ค๋ฅผ ๋ช
์์ ์ผ๋ก ์ค์ ํฉ๋๋ค. ๋ฐ๋ผ์ ๋ค๋ฅธ ํด๋์ค ์ ๊ฑฐ ์ ๋ค์ ๋ช
์ํ ํ์๊ฐ ์์ต๋๋ค.