關(guān)于sass的map的一些問(wèn)題
問(wèn)題描述
關(guān)于sass的map/list的問(wèn)題
例如有一個(gè)嵌套格式的map
$breakpoints-map: ( small:(min-width: 320px,base-font:12px,vertical-rhythm:1.3 ), medium:(min-width: 480px,base-font:14px,vertical-rhythm:1.414 ), large:(min-width: 960px,base-font:16px,vertical-rhythm:1.5 ));
然后弄一個(gè)@mixin,取到list中的內(nèi)容,分別賦值給需要的CSS屬性
@mixin mapListDome($map) { @each $key,$value in $map { @media screen and (min-width: map-get($value,min-width)) { font-size: map-get($value,base-font); line-height: map-get($value,vertical-rhythm); @content; } } }
這樣調(diào)用
.wrap { @include mapListDome($breackpoints-map){ height:auto; }
問(wèn)題來(lái)了:
如果想在調(diào)用的時(shí)候新增一個(gè)屬性,比如width,或者去掉一個(gè)屬性,比如font-size,那么只能去修改$breakpoints-map或者修改mapListDome這個(gè)@mixin,很不方便,而{}內(nèi)的是@centent定義的,只能輸出相同的內(nèi)容。
以前都是這樣使用:
$viewpoints:(small:320px,medium:480px,large:960px);$font-size:(small:12px,medium:14px,large:16px);$vertical-rhythm:(small:1.3,medium:1.141,large:1.5);@mixin mapListDome($map1,$map2:(),$map3:()){ @each $key,$value in $map1{@media screen and (min-width:$value){ //獲取多個(gè)map中, 同名屬性對(duì)應(yīng)的值font-size:map-get($map2,$key);line-height:map-get($map3,$key);} }}
調(diào)用時(shí),通過(guò)刪減參數(shù),增減CSS屬性
.wrap{ @mapListDome($viewpoints);//不使用任何css屬性 @mapListDome($viewpoints,$font-size);//只使用font-size @mapListDome($viewpoints,$font-size,$vertical-rhythm);//使用全部屬性 }
但是這樣寫(xiě)也有很多問(wèn)題
1、要寫(xiě)很多遍small、meduim、large這樣的重復(fù)屬性名稱2、如果css屬性很多,要傳入大量map,很麻煩
補(bǔ)充:還有多重列表。。
$list-img: ( (small, #000, 320px, 0 0), (medium, #f60, 480px, 0 -24px), (large, #f50, 960px, 0 -48px));@mixin mediaImg($list) { @each $name, $color, $viewpoints, $pos in $list {@media screen and (min-width: $viewpoints) {border: 1px solid $color;background-image: url(../images/#{$name}.jpg);background-position: $pos;} }}.wrap { @include mediaImg($list-img);}
看起來(lái)很方便,但是假設(shè)第三個(gè)list里漏掉一個(gè)960px,屬性就全錯(cuò)位了,而且不會(huì)報(bào)錯(cuò)。
所以,關(guān)于map/list的使用,不知道有沒(méi)有什么比較便捷的使用方法?
問(wèn)題解答
回答1:/必須的viewpoints媒體查詢map$viewpoints-breakpoints: ( small: 480px, medium: 992px, large: 1200px);//可選css屬性map(可以不使用)$property-list: ( small: (font-size: 14px,color: lighten(#333,75%),width: percentage(4/12) ), medium: (font-size: 16px,color: lighten(#333,50%),width: percentage(6/12) ), large: (font-size: 18px,color: lighten(#333,25%),width: percentage(7/12) ));//參數(shù)map-name為斷點(diǎn)small,medium,large,它們也是嵌套層的名稱@mixin respond-list($map-name, $property: (), $viewpoints: $viewpoints-breakpoints) { //檢查是否包含顯示器分辨率斷點(diǎn) @if map-has-key($viewpoints,$map-name) {//取得斷點(diǎn)對(duì)應(yīng)的分辨率值$view-width: map-get($viewpoints, $map-name);// 取得對(duì)應(yīng)small,medium,large之一的內(nèi)容,組成一個(gè)名為$map-in-key的新map$map-in-key: map-get($property,$map-name);@media screen and (min-width: $view-width) { //遍歷$map-in-key這個(gè)新map中的屬性名稱和值,輸出為css屬性 @each $key, $value in $map-in-key {#{$key}: $value; } @content;} } @else {//斷點(diǎn)不合法或未寫(xiě)時(shí),拋出錯(cuò)誤信息@warn 'Unfortunately! The #{$map-name} is not a valid parameter or undefinded.'; }}.dome-list { line-height: 1; color: #f65; @include respond-list(small) {//調(diào)用時(shí),如不需要引入屬性都自己寫(xiě),只需寫(xiě)入斷點(diǎn)line-height: 1.2; } @include respond-list(medium,$property-list) {//需要引入現(xiàn)成的屬性,參數(shù)加入屬性mapline-height: 1.5; };}
編譯后:
.dome-list { line-height: 1; color: #f65}@media screen and (min-width: 480px) { .dome-list { line-height: 1.2 }}@media screen and (min-width: 992px) { .dome-list { font-size: 16px; color: #b3b3b3; width: 50%; line-height: 1.5 }}
唯一一點(diǎn)麻煩的就是,一般斷點(diǎn)都有2至5個(gè)不等,需要@include多次,不過(guò)為了靈活使用,暫時(shí)只想到這些了
相關(guān)文章:
1. android - weex 項(xiàng)目createInstanceReferenceError: Vue is not defined2. javascript - 如圖,百度首頁(yè),查看源代碼為什么什么都沒(méi)有?3. 網(wǎng)頁(yè)爬蟲(chóng) - python requests爬蟲(chóng),如何post payload4. html - 關(guān)于CSS實(shí)現(xiàn)border的0.5px設(shè)置?5. PHPExcel表格導(dǎo)入數(shù)據(jù)庫(kù)怎么導(dǎo)入6. android - 哪位大神知道java后臺(tái)的api接口的對(duì)象傳到前端后輸入日期報(bào)錯(cuò),是什么情況?求大神指點(diǎn)7. pdo 寫(xiě)入到數(shù)據(jù)庫(kù)的內(nèi)容為中文的時(shí)候?qū)懭雭y碼8. PHP類封裝的插入數(shù)據(jù),總是插入不成功,返回false;9. vue2.0+webpack 如何使用bootstrap?10. mac連接阿里云docker集群,已經(jīng)卡了2天了,求問(wèn)?
