2016-10-04 64 views
0

我試圖找出在sass中編寫媒體查詢的方法。這是我的代碼如何在sass中編寫媒體查詢?

devices: ("sphone","phone","tablet") 
 
$breakpoints: (\t width2: ('sphone' : 320, 'phone' : 360, 'tablet' : 600), height2: ('sphone' : 509, 'phone' : 583, 'tablet' : 952)) 
 

 
=screen-width($device) 
 
    @each $name in $devices 
 
    @if $device != $name 
 
     @error "No device of such name" 
 
    $media: (max-width: map-get(map-get($breakpoints,"width2"),$device) + px) 
 
    @media screen only and ($media) 
 
    @content 
 
\t \t 
 
\t \t 
 
.hello 
 
    background: #333 
 
    @include screen-width("mobile") 
 
    background: #444 
 

我試圖編譯青菜一樣的,但它一直就扔我下面的錯誤。我不知道爲什麼。

Properties are only allowed within rules, directives, mixin includes, or other properties. 

任何見解?

回答

0

您在代碼中存在一些錯誤,在$devices變量中缺少$。你寫@include screen-width("mobile")但手機不在設備列表中。在$media變量max-width必須是一個字符串,然後您必須使用插值添加@media規則。

@each循環中的比較是錯誤的,但我暫時沒有解決這個問題。此代碼有效:

$devices: ("sphone","phone","tablet") 
$breakpoints: (width2: ('sphone' : 320, 'phone' : 360, 'tablet' : 600), height2: ('sphone' : 509, 'phone' : 583, 'tablet' : 952)) 

=screen-width($device) 
    $media: "max-width:" map-get(map-get($breakpoints,"width2"),$device) + px 
    @media screen only and (#{$media}) 
    @content 

.hello 
    background: #333 
    @include screen-width("sphone") 
    background: #444 
+0

是的!這工作。謝謝 :) –