title: stylus入門學習筆記
date: 2018-09-06 17:35:28
tags: [stylus]
description: 學習到 vue, 有人推薦使用 stylus 這個 css 預處理器。而以前也只是停留在據說過 stylus,並無實際操做過。如今正好抽空來學習一下唄;若是會 less,sass之類的 css 預編譯器,學 stylus 也是 so easy!
---
學習到 vue, 有人推薦使用 stylus 這個 css 預處理器。而以前也只是停留在據說過 stylus,並無實際操做過。如今正好抽空來學習一下唄;若是會 less,sass之類的 css 預編譯器,學 stylus 也是 so easy!
學習來源:張鑫旭我的博客,這是個 css 界大牛哦css
話很少說,先來段代碼比較一下吧。vue
body color: white textarea, input border: 1px solid #333 p li ul margin: 0 .contain &:hover opacity: 0.8
等同於node
body { color: white; } body textarea, body input { border: 1px solid #333; } body p, body li, body ul { margin: 0; } body .contain:hover { opacity: 0.8; }
目前看來,stylus 就是將 sass 的花括號去掉,分號不要而已。按照以前的 sass、less 的語法寫,就差不了太多。因此不作多解釋,繼續下面。web
有 stylus 沒法處理的屬性值,可使用 unquote()express
filter unquote('progid: DXImageTransform.Microsoft.BasicImage(rotation=1)')
轉化成瀏覽器
filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=1)
直接使用 '=' 來聲明, 變量中能夠帶 $
符sass
font-size = 14px $color = #ddd body { font-size: font-size background-color: $color }
等同於ruby
body { font-size: 14px; background-color: #ddd; }
Stylus支持經過使用{}字符包圍表達式來插入值,其會變成標識符的一部分。例如,-webkit-{'border' + '-radius'}等同於-webkit-border-radius.less
vendor(prop, args) -webkit-{prop} args -moz-{prop} args {prop} args border-radius() vendor('border-radius', arguments) box-shadow() vendor('box-shadow', arguments) button border-radius 1px 2px / 3px 4px
轉換成函數
button { -webkit-border-radius: 1px 2px / 3px 4px; -moz-border-radius: 1px 2px / 3px 4px; border-radius: 1px 2px / 3px 4px; }
插值也能夠在選擇器上起做用。eg:
table for row in 1 2 3 tr:nth-child({row}) height: 10px * row
等同於:
table tr:nth-child(1) { height: 10px; } table tr:nth-child(2) { height: 20px; } table tr:nth-child(3) { height: 30px; }
利用這個屬性,咱們能夠封裝不少的方法了。將瀏覽器兼容前綴寫法封裝成一個函數。
add-attr-prefix(attr, args) { -webkit-{attr}: args -moz-{attr}: args -o-{attr}: args -ms-{attr}: args {attr}: args } // 封裝一下 box-shadow 函數 box-shadow() add-attr-prefix('box-shadow', arguments) body box-shadow: 8px 8px 20px red
等價於
body { -webkit-box-shadow: 8px 8px 20px red; -moz-box-shadow: 8px 8px 20px red; -o-box-shadow: 8px 8px 20px red; -ms-box-shadow: 8px 8px 20px red; box-shadow: 8px 8px 20px red; }
混入和函數定義方法一致,可是應用卻截然不同。
定義方式:
aaa () 語句塊
譬如,
border-radius(npx) -webkit-border-radius npx -moz-border-radius npx border-radius npx .box border-radius(5px) // 使用混入書寫,你能夠徹底忽略括號,提供夢幻般私有屬性的支持。 border-radius 5px
也能夠不傳參數,用 arguments
border-radius() -webkit-border-radius arguments -moz-border-radius arguments border-radius arguments box-shadow(args...) -webkit-box-shadow args -moz-box-shadow args box-shadow args box-shadow1() -webkit-box-shadow agruments -moz-box-shadow agruments box-shadow agruments .box box-shadow 1px 1px 5px red, 1px 3px 5px green .box1 box-shadow 1px 1px 5px red, 1px 3px 5px green
須要注意的是,argument 和 args...,前者能夠準確的將全部參數傳遞,包括逗號之類的一些特殊符號。
看一下輸出結果:
.box { -webkit-box-shadow: #ddd 1px 1px #eee 2px 2px; -moz-box-shadow: #ddd 1px 1px #eee 2px 2px; box-shadow: #ddd 1px 1px #eee 2px 2px; } .box1 { -webkit-box-shadow: #ddd 1px 1px, #eee 2px 2px; -moz-box-shadow: #ddd 1px 1px, #eee 2px 2px; box-shadow: #ddd 1px 1px, #eee 2px 2px; }
同 less, sass 同樣,引用父級也是用 &;利用 & 封裝一個條紋表格
stripe(even = #fff, odd = #eee) tr background-color odd &.even &:nth-child(even) background-color even
函數是能夠返回值的。有時候返回值不是一個具體的值,而是表示符。
swap(a, b) b a
這樣的時候,寫成
swap(a, b) (b a) swap(a, b) return b a
我的在實際項目中用到的方法少之又少;因此感受,不作遊戲開發的話,用到方法的少。看到這兒,就算是已經入門了。
Stylus支持三種註釋,單行註釋,多行註釋,以及多行緩衝註釋。
前面三種就很少說了,和之前的寫法徹底一致。對於多行緩衝註釋,跟多行註釋相似,不一樣之處在於開始的時候,這裏是/*!.這個至關於告訴Stylus壓縮的時候這段無視直接輸出。
if, else if, else 和常見的條件是同樣的。在 stylus 裏,也想 ruby 同樣,用了 unless (除非)條件。也很好理解,其基本上與 if 相反,本質上是 (!(expr)). eg:
disable = true unless disable display none
另外,Stylus 支持後綴條件,這就意味着 if 和 unless 能夠看成操做符;當右邊表達式爲真的時候執行左邊的操做對象。
通常適用於單行語句。
譬如:
negative(n) unless n is a 'unit' error('無效數值') if n < 0 yes else no // 就能夠寫成下面這種形式了 negative(n) error('無效數值') unless n is a 'unit' return yes if n < 0 no
for <val-name> [, <key-name>] in <expression>
感受實際開發中用不到,暫時先放着,待後續補齊吧。
任何 .css
擴展的文件名將做爲字面量。例如,
@import "reset.css" // 渲染成 ===>> @import "reset.css"
當使用 @import 沒有 .css 擴展,會被認爲是 Stylus 片斷(如:@import"mixins/border-radius")。
@import 工做原理爲:遍歷目錄隊列,並檢查任意目錄中是否有該文件(相似 node 的 require.paths)。該隊列默認爲單一路徑,從 filename 選項的 dirname 衍生而來。所以,若是你的文件名是 /tmp/testing/stylus/main.styl,導入將顯現爲 /tmp/testing/stylus/。
@import 也支持索引形式。這意味着當你 @import blueprint,則會理解成 blueprint.styl 或 blueprint/index.styl. 對於庫而言,這頗有用,既能夠展現全部特徵與功能,同時又能導入特徵子集。
還有像 @media,@font-face,@keyframes 等和 css 中的差不了太多。
注意,@keyframes 會經過 vendors 變量,會自動添加私有前綴(webkit moz official)。若是咱們只想有標準解析,很簡單,修改 vendors = official
.
看個例子感覺。
red = #E33E1E yellow = #E2E21E .message padding: 10px font: 14px Helvetica border: 1px solid #eee .warning @extends .message border-color: yellow background: yellow + 70% .error @extends .message border-color: red background: red + 70% .fatal @extends .error font-weight: bold color: red
生成 css 以下
.message, .warning, .error, .fatal { padding: 10px; font: 14px Helvetica; border: 1px solid #eee; } .warning { border-color: #e2e21e; background: #f6f6bc; } .error, .fatal { border-color: #e33e1e; background: #f7c5bc; } .fatal { font-weight: bold; color: #e33e1e; }
ok, stylus 的學習,就簡單到這兒了吧。後面有必要再深刻學習一波。
那一晚上我燒燬了全部的記憶,今後個人夢就透明瞭,那一天我扔掉了全部的昨天,今後個人腳步就輕盈了。 一一 泰戈爾(《飛鳥集》)