Quantcast
Channel: CSSタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8660

SCSS まとめ2(関数)

$
0
0

SCSSまとめ集

  1. 基本文法
  2. 関数
  3. コンパイル方法

目次

  1. 関数とは?
  2. 自作関数
  3. Mixinについて
  4. 組込関数

1. 関数とは?

  • 他言語同様、処理を行って戻り値を取得することができる。
  • あらかじめ用意されてある関数も存在する。

2. 自作関数

scss
@functionmyFunction($val){@return$val*100px;}.box{width:myFunction(2);}//トランスパイル後.box{width:200px;}
  • デフォルト値、引数を複数設定する場合
scss
@functionmyFunction($val1:100px,$val2:200px){@return$val1+$val2;}.box{width:myFunction();}//トランスパイル後.box{width:300px;}
  • 可変長引数の場合
scss
@functionmyFunction($vals...){$width:0px;@each$valin$vals{$width:$width+$val;}@return$width;}.box{width:myFunction(50px,100px,150px);}//トランスパイル後.box{width:300px;}

3. Mixinについて

  • 引数を指定したスタイルの継承を行う。
  • @mixinで関数を定義、@includeで呼び出す。
  • @contentで、コンテントブロックを渡すことができる。
scss
@mixinbox($width:100px,$bgcolor:#000){width:$width;background:$bgcolor;}.hoge{@includebox;}//トランスパイル後.hoge{width:100px;background:#000;}

@contentを使う場合

scss(content使用例)
@mixinbox($width:100px,$bgcolor:#000){width:$width;background:$bgcolor;@content;}.color1{@includebox{color:red;}}.color2{@includebox{color:blue;}}//トランスパイル後.color1{width:100px;background:#000;color:red;}.color2{width:100px;background:#000;color:blue;}

4. 組込関数

あらかじめ用意されてある関数。以下に記載。


文字列

関数意味
str-index文字の検索(何文字目かを返す)
str-slice文字の切り出し
str-length文字の長さ
str-insert文字を挿入
to-lower-case小文字に変換
to-upper-case大文字に変換
unquoteクォーテーションを取り除く
quoteダブルクォートを付与する
scss(文字列の関数)
//str-index($string, $substring).hoge{prop:str-index("abcdefg","c");//3prop:str-index("あいうえお","い");//2prop:str-index("あいうえお","か");//nullprop:str-index("abcdefg","ef");//5}//str-slice($string, $start-at, $end-at).hoge{prop:str-slice("abcdefg",2,4);//"bcd"prop:str-slice("あいうえお",3);//"うえお"prop:str-slice("zyxcba",-2);//"ba"}//str-length($string).hoge{prop:str-length("hogehoge");//8prop:str-length("ふがfuga");//6}//str-insert($string, $insert, $index).hoge{prop:str-insert("abcde","z",1);//"zabcde"prop:str-insert("abcde","zzz",3);//"abzzzcde"}//to-lower-case($string).hoge{prop:to-lower-case("ABCDEF");//"abcdef"prop:to-lower-case("あいうEお");//"あいうeお"}//to-upper-case($string).hoge{prop:to-upper-case("abcdef");//"ABCDEF"prop:to-upper-case("あいうeお");//"あいうEお"}//unquote($string).hoge{prop:unquote("hogehoge");//hogehogeprop:unquote('あいうえお');//あいうえお}//quote($string).hoge{prop:quote(hogehoge);//"hogehoge"}

数値

関数意味
ceil小数点を切り上げた値を返す
floor小数点を切り捨てた値を返す
round小数点を四捨五入した値を返す
min最小値を返す
max最大値を返す
random1~指定した数値の範囲内の数値を返す(引数省略時は0以上1未満)
abs絶対値を返す
scss(数値の関数)
//ceil($number)$number1:ceil(10.1);//11//floor($number)$number2:floor(10.1);//10//round($number)$number3:round(10.4);//10//min($number...)$number4:min(1,5,10);//1//max($number...)$number5:max(1,5,10);//10//random($limit)$number6:random(100);//56//abs($number)$number7:abs(-100);//100

関数意味
rgbRGB値を計算して返す
rgbaRGBA値を計算して返す
redRGB値のR値を返す
greenRGB値のG値を返す
blueRGB値のB値を返す
mix中間色を返す
hslHSL値(色相、彩度、輝度)を計算して返す
hslaHSLA値を計算して返す
hueHSL値の色相を返す
saturationHSL値の彩度を返す
lightnessHSL値の輝度を返す
scss(色の関数)
//rgb($red, $green, $blue)$color1:rgb(111,123,254);//#6f7bfe//rgba($red, $green, $blue, $alpha)$color2:rgba(111,123,254,0.4);//rgba(111, 123, 254, 0.4)//mix($color1, $color2, $weight)$color3:mix(red,green,50%);//#804000//hsl($hue, $saturation, $lightness)$color4:hsl(122,10%,55%);//#819882//hsla($hue, $saturation, $lightness, $alpha)$color5:hsla(122,10%,55%,0.5);//rgba(129,152,130,0.5)//hue($color)$color6:hue(#123456);//210deg//saturation($color)$color7:saturation(#123456);//65.3846153846%//lightness($color)$color8:lightness(#123456);//20.3921568627%

セレクタ

関数意味
selector-appendセレクタ名を連結して返す
selector-nestセレクタ名を子孫セレクタとして連結して返す
selector-extendセレクタ名をカンマ区切りで返す
selector-replaceセレクタ名を置換して返す
scss(セレクタの関数)
//selector-append($selectors...)$selectors:selector-append("#box",".content",".new");//#box.content.new//selector-nest($selectors...)$selectors:selector-nest("#box",".content",".new");//#box .content .new//selector-extend($selector, $extendee, $extender)$selectors:selector-extend(".box .text",".text",".item");//.box .text, .box .item//selector-replace($selector, $original, $replacement)$selectors:selector-replace(".box .text",".text",".item");//.box ,item

その他

関数意味
selector-append
type-ofデータ型を返す
untitless単位が含まれているかどうかを返す
length配列の要素数を返す
append配列の末尾に追加して返す
nth配列の値を返す
map-get連想配列の値を返す
scss(その他の関数)
//type-of($value).hoge{prop:typeof(123);//numberprop:typeof("hoge");//stringprop:typeof(true);//boolprop:typeof(null);//null}//untitless($number).hoge{prop:unitless(100);//trueprop:unitless(100px);//falseprop:unitless(100%);//false}//length($list).hoge{prop:length(12);//2prop:length("hoge"1true);//3}//append($list, $val, [$separator])//$separatorには"space", "comma", "auto"のいずれかが指定可能.hoge{prop:append("hoge""fuga""piyo");//"hoge" "fuga" "piyo"prop:append(1,2,3,comma);//1, 2, 3}//nth($array, $index)$games:['switch','ps','xbox'];$str:nth($games,1);//'switch'//map-get($map, $key)$urls:(google:'https://www.google.com/',yahoo:'https://www.yahoo.co.jp/',amazon:'https://www.amazon.co.jp/');$url:map-get($urls,google);//'https://www.google.com/'

Viewing all articles
Browse latest Browse all 8660

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>