23日目TIL
SCSS
🍈 @mixin
@includeで指定したコード内容を使用します.
このとき指定したターゲットにはパラメータがないかdefault値がある可能性があります.
// 매개변수가 없는 @mixin
@mixin noArgument{
font : {
size:100px;
weight:bold;
family :sans-serif;
};
}
.box1{
width:100px;
height:200px;
@include noArgument;
}
// 매개변수가 있는 @mixin
@mixin Argument($size){
font : {
size:$size;
weight:bold;
family :sans-serif;
};
}
.box2{
width:100px;
height:200px;
@include Argument(1000px);
}
@mixin noArgumentValue($size : 3000px){
font : {
size:$size;
weight:bold;
family :sans-serif;
};
}
//매개변수는 기존에 설정되어 있으나 쓰지않아 default value로 처리
.box3{
width:100px;
height:200px;
@include noArgumentValue;
}
//매개변수는 기존에 설정되어 있으나 쓰지않아 default value로 처리
.box4{
width:100px;
height:200px;
@include noArgumentValue();
}
.box1 {
width: 100px;
height: 200px;
font-size: 100px;
font-weight: bold;
font-family: sans-serif;
}
.box2 {
width: 100px;
height: 200px;
font-size: 1000px;
font-weight: bold;
font-family: sans-serif;
}
.box3 {
width: 100px;
height: 200px;
font-size: 3000px;
font-weight: bold;
font-family: sans-serif;
}
.box4 {
width: 100px;
height: 200px;
font-size: 3000px;
font-weight: bold;
font-family: sans-serif;
}
@mixinプロパティ
{ }
スタイルブロックが追加された内容をmixinに伝えることができるので、それを得るためにmixinは使うべき@content
:null
無価であればnullとして扱う.()
かっこで値が得られ、使用時に使用できます.展開演算子の例1
//$width, $height를 제외한 나머지 값을 $marginRest에 몰아 넣는다.
@mixin content($width , $height, $marginRest...){
width : $width;
height : $height;
margin : $marginRest;
}
.box{
@include content(
100px,
200px,
50px,
100px,
150px,
200px
);
}
.box {
width: 100px;
height: 200px;
margin: 50px, 100px, 150px, 200px;
}
展開演算子の例2
@mixin spread($t, $r, $b, $l){
margin-top : $t;
margin-right : $r;
margin-bottom : $b;
margin-left : $l;
}
.box{
$m : 10px 20px 30px 40px;
@include spread($m...)
}
.box {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 30px;
margin-left: 40px;
}
spread($m)
で処理すると100万ドルの並びが全て$tに入るしたがって、$r$b$lには値がないので、後で展開演算子を配置して展開します.エラーが蒸し焼きで並べられているように...通過するはずです.
@mixin spread($t, $r:500px, $b:30px, $l:70px)
となると$tに並び、間違いはありませんが、欲しい結果ではありません.展開演算子の例3(+補間)
@mixin spread($p,$t, $r, $b, $l){
#{$p}: {
top : $t;
right : $r;
bottom : $b;
left : $l;
}
}
.box{
$m : 10px 20px 30px 40px;
@include spread(margin, $m...);
@include spread(padding, 10px 20px 30px 40px...)
}
.box {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 30px;
margin-left: 40px;
padding-top: 10px;
padding-right: 20px;
padding-bottom: 30px;
padding-left: 40px;
}
選択者または属性は一般変数では処理できず、#{$p}
で処理し、残りの配列は...
で分解し、個別に入れる.@contentの例
@mixin icon($url){
&::after{
content : url($url);
@content;
}
}
.box{
@include icon("/images/icon.png"){
position:absolute;
top : 0;
left : 50px;
}
}
.box::after {
content: url("/images/icon.png");
position: absolute;
top: 0;
left: 50px;
}
キーワード引数の例
@mixin pos($p , $t:null,$b:null){
position : $p;
top : $t;
bottom : $b;
}
.absolute{
@include pos( $b : 150px, $p:absolute, $t : 2000px)
}
.absolute {
position: absolute;
top: 2000px;
bottom: 150px;
}
パラメータを順番に渡さなくても、キーワード伝達値に基づいて処理されます.🍈 @mixinまとめ
@mixin pass($a:null,$b:null,$c:null){
&:hover{
color:blue;
font-size : $a;
width : $b;
height : $c;
@content (1000px, 2000px);
}
@content(10px,20px);
}
.div{
@include pass ($a:10,$b:20,$c:30) using($t , $b){
margin-top : $t;
margin-bottom : $b;
}
}
.div {
margin-top: 10px;
margin-bottom: 20px;
}
.div:hover {
color: blue;
font-size: 10;
width: 20;
height: 30;
margin-top: 1000px;
margin-bottom: 2000px;
}
🍈 さんこうえんざんし
if(check、checkがtrueの場合、checkはfalse)
@mixin content($size){
// @if($size < 30px){
// font-size : 30px;
// }
// @else{
// font-size : $size;
// }
font-size : if($size < 30px , 30px , $size);
}
.box100px{
@include content(100px);
}
.box20px{
@include content(20px);
}
.box100px {
font-size: 100px;
}
.box20px {
font-size: 30px;
}
$sizeは30 px以下で30 pxを処理し、30 pxを超えると対応する寸法を処理する.コメントセクションのifelseは同じ結果です.🍈 map.get
Mapはキー値に基づいて対応するターゲットを取得します.
@use "sass:map"
を上に追加する@use "sass:map";
$bootstrap-colors : (
primary : #0275d8,
success : #5cb85c
);
.notice{
color : map.get($bootstrap-colors, success);
}
.notice {
color: #5cb85c;
}
🍈@extend
拡張子を追加すると、選択プログラムが爆発したり、継承が重複したりするなどの問題が発生します.
実際の@mixinはこの@extendをすべて適用できるので、あまり使いにくいです.
🍈 @function @return @error
類似js使用
@errorの場合、SCSSにエラーが表示されます.(JSのthrowに類似)
関数の作成時に作成される既存の関数(url,repeat...)避けるために
A-B
(my-func)の形で書くことがある.拡張性のため、ある変数値について、既に記述されているものがあれば、後述する
!default
.🍈 @each
ListとMapで処理可能
🍈 @for
$i from a through b
=>a~b$i from a to b
=>aからb-1🍈 @while
@while $i > 0 { ... }
Reference
この問題について(23日目TIL), 我々は、より多くの情報をここで見つけました https://velog.io/@khw970421/TIL-23일차テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol