๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

Syntax-08-functions

Sass(SCSS) Syntax - 8. ํ•จ์ˆ˜(Functions)#

โ—๏ธ ํ•ด๋‹น ๊ธ€์€ ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜์—์„œ HTML & CSS, SASS(SCSS) Part์˜ ๋ฐ•์˜์›… ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฐ•์˜์ž๋ฃŒ(Sass(SCSS) ์™„์ „ ์ •๋ณต!)๋ฅผ ๋ณด๋ฉฐ ์ •๋ฆฌํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

ํ•จ์ˆ˜(Functions)#

ํ•จ์ˆ˜์™€ @mixins ์€ ๊ฑฐ์˜ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋ฐ˜ํ™˜๋˜๋Š” ๋‚ด์šฉ์ด ๋‹ค๋ฅด๋‹ค

@mixin ์€ ์ง€์ •ํ•œ ์Šคํƒ€์ผ(Style)์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐ˜๋ฉด,

ํ•จ์ˆ˜๋Š” ๋ณดํ†ต ์—ฐ์‚ฐ๋œ(Computed) ๊ฐ’์„ @return ์ง€์‹œ์–ด๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜ํ•œ๋‹ค.

// Mixins@mixin ๋ฏน์Šค์ธ์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜) {  ์Šคํƒ€์ผ;}
// Functions@function ํ•จ์ˆ˜์ด๋ฆ„($๋งค๊ฐœ๋ณ€์ˆ˜) {  @return ๊ฐ’}

@mixin ์€ @include ์ง€์‹œ์–ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ˜๋ฉด,

ํ•จ์ˆ˜๋Š” ํ•จ์ˆ˜์ด๋ฆ„์œผ๋กœ ๋ฐ”๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

// Mixin@include ๋ฏน์Šค์ธ์ด๋ฆ„(์ธ์ˆ˜);
// Functionsํ•จ์ˆ˜์ด๋ฆ„(์ธ์ˆ˜)
$max-width: 980px;
@function columns($number: 1, $columns: 12) {  @return $max-width * ($number / $columns)}
.box_group {  width: $max-width;
  .box1 {    width: columns();  }  .box2 {    width: columns(8);  }  .box3 {    width: columns(3);  }}

์ปดํŒŒ์ผํ•˜๋ฉด

.box_group {/* ์ด ๋„ˆ๋น„ */width: 980px;}.box_group .box1 {/* ์ด ๋„ˆ๋น„์˜ ์•ฝ 8.3% */width: 81.66667px;}.box_group .box2 {/* ์ด ๋„ˆ๋น„์˜ ์•ฝ 66.7% */width: 653.33333px;}.box_group .box3 {/* ์ด ๋„ˆ๋น„์˜ 25% */width: 245px;}

ํ•จ์ˆ˜๋Š” @include ๊ฐ™์€ ๋ณ„๋„์˜ ์ง€์‹œ์–ด ์—†์ด ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‚ด์žฅ ํ•จ์ˆ˜(Built-in Functions)์˜ ์ด๋ฆ„์ด ์ถฉ๋Œํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ง€์ •ํ•œ ํ•จ์ˆ˜์—๋Š” ๋ณ„๋„์˜ ์ ‘๋‘์–ด๋ฅผ ๋ถ™์—ฌ ์ถฉ๋Œ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, red() ๋ผ๋Š” ๋‚ด์žฅ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

๊ฐ™์€ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฆ„์ด ์ถฉ๋Œํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ„๋„์˜ ์ ‘๋‘์–ด๋ฅผ ๋ถ™์—ฌ extract-red() ๊ฐ™์€ ์ด๋ฆ„์„ ๋งŒ๋“ ๋‹ค.

// ๋‚ด๊ฐ€ ์ •์˜ํ•œ ํ•จ์ˆ˜@function extract-red($color) {  // ๋‚ด์žฅ ํ•จ์ˆ˜  @return rgb(red($color), 0, 0);}
div {  color: extract-red(#D55A93);}

ํ˜น์€ ํŠน๋ณ„ํ•œ ์ด๋ฆ„์„ ์ ‘๋‘์–ด๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

my-custom-func-red()

Reference#

ํŒจ์ŠคํŠธ์บ ํผ์Šค - ํ”„๋ก ํŠธ์—”๋“œ ๊ฐœ๋ฐœ ๊ฐ•์˜ - HTML & CSS, SASS(SCSS) Part by ParkYoungWoong

Sass(SCSS) ์™„์ „ ์ •๋ณต!