關于sass的map的一些問題
問題描述
關于sass的map/list的問題
例如有一個嵌套格式的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 ));
然后弄一個@mixin,取到list中的內容,分別賦值給需要的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; } } }
這樣調用
.wrap { @include mapListDome($breackpoints-map){ height:auto; }
問題來了:
如果想在調用的時候新增一個屬性,比如width,或者去掉一個屬性,比如font-size,那么只能去修改$breakpoints-map或者修改mapListDome這個@mixin,很不方便,而{}內的是@centent定義的,只能輸出相同的內容。
以前都是這樣使用:
$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){ //獲取多個map中, 同名屬性對應的值font-size:map-get($map2,$key);line-height:map-get($map3,$key);} }}
調用時,通過刪減參數,增減CSS屬性
.wrap{ @mapListDome($viewpoints);//不使用任何css屬性 @mapListDome($viewpoints,$font-size);//只使用font-size @mapListDome($viewpoints,$font-size,$vertical-rhythm);//使用全部屬性 }
但是這樣寫也有很多問題
1、要寫很多遍small、meduim、large這樣的重復屬性名稱2、如果css屬性很多,要傳入大量map,很麻煩
補充:還有多重列表。。
$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);}
看起來很方便,但是假設第三個list里漏掉一個960px,屬性就全錯位了,而且不會報錯。
所以,關于map/list的使用,不知道有沒有什么比較便捷的使用方法?
問題解答
回答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) ));//參數map-name為斷點small,medium,large,它們也是嵌套層的名稱@mixin respond-list($map-name, $property: (), $viewpoints: $viewpoints-breakpoints) { //檢查是否包含顯示器分辨率斷點 @if map-has-key($viewpoints,$map-name) {//取得斷點對應的分辨率值$view-width: map-get($viewpoints, $map-name);// 取得對應small,medium,large之一的內容,組成一個名為$map-in-key的新map$map-in-key: map-get($property,$map-name);@media screen and (min-width: $view-width) { //遍歷$map-in-key這個新map中的屬性名稱和值,輸出為css屬性 @each $key, $value in $map-in-key {#{$key}: $value; } @content;} } @else {//斷點不合法或未寫時,拋出錯誤信息@warn 'Unfortunately! The #{$map-name} is not a valid parameter or undefinded.'; }}.dome-list { line-height: 1; color: #f65; @include respond-list(small) {//調用時,如不需要引入屬性都自己寫,只需寫入斷點line-height: 1.2; } @include respond-list(medium,$property-list) {//需要引入現成的屬性,參數加入屬性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 }}
唯一一點麻煩的就是,一般斷點都有2至5個不等,需要@include多次,不過為了靈活使用,暫時只想到這些了
相關文章:
1. 請教使用PDO連接MSSQL數據庫插入是亂碼問題?2. node.js - nodejs開發中常用的連接mysql的庫3. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?4. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處5. 視頻文件不能播放,怎么辦?6. python - 數據與循環次數對應不上7. mysql - 把一個表中的數據count更新到另一個表里?8. 黑客 - Python模塊安全權限9. flask - python web中如何共享登錄狀態?10. mysql 查詢身份證號字段值有效的數據
