前段時間作vue項目,用到了css的提高開發效率的工具stylus,感受很好用。如今又開始寫靜態頁面了,因而將強大的stylus拿過來繼續用。因而就寫了這篇使用經驗,算是本身總結一下。javascript
使用前,咱們須要在終端裏面進行全局安裝stylus,這樣在項目中可使用stylus將styl文件解析成css(固然也能夠將css反編譯成styl文件)。css
$ npm install stylus -g
可使用命令查看是否安裝成功了(大寫)vue
$ stylus -V
安裝完成以後你能夠看到下面一些經常使用的參數java
Usage: stylus [options] [command] [< in [> out]] [file|dir ...] Commands: help <prop> Opens help info for <prop> in your default browser. (OS X only) Options: -u, --use <path> Utilize the stylus plugin at <path> -i, --interactive Start interactive REPL -w, --watch Watch file(s) for changes and re-compile -o, --out <dir> Output to <dir> when passing files -C, --css <src> [dest] Convert CSS input to Stylus -I, --include <path> Add <path> to lookup paths -c, --compress Compress CSS output -d, --compare Display input along with output -f, --firebug Emits debug infos in the generated css that can be used by the FireStylus Firebug plugin -l, --line-numbers Emits comments in the generated CSS indicating the corresponding Stylus line -V, --version Display the version of Stylus -h, --help Display help information
-w、-o、-c是咱們會經常使用到的web
-w :監聽文件,只要原文件改變,解析後的目標文件也會同時改變 -o :指定目標文件,不指定的話就是和源文件同名 -c :壓縮文件,將源文件解析後並壓縮
安裝完成後,咱們進入項目的根目錄(最好是style這級目錄),假如咱們有一個style目錄,裏面有一個example.styl文件(stylus文件的後綴名就是styl),對文件進行解析。npm
// 將style目錄下面的styl文件都解析爲相同文件名都css文件,並放在style目錄裏面 // 而且監聽文件 $ stylus -w style/ // 將style目錄下面的styl文件都解析爲相同文件名都css文件,並放在style目錄裏面 // 並對css文件進行壓縮 // 而且監聽文件 $ stylus -w -c style/ // 將style目錄下面的styl文件都解析爲指定的文件名css,與style同級目錄 // 而且監聽文件 $ stylus -w style/ -o main.css
全部的前期準備工做完成,如今開始對stylus進行基本使用,看看效果less
stylus文件函數
stylus文件 body ul color: red font-size: 20px li color: yellow font-size: 36px css文件
body ul { color: #f00; font-size: 20px; } body ul li { color: #ff0; font-size: 36px; }
就是這麼簡單,這麼方便。這樣就能夠節省不少寫選擇器的時間了,這樣也不容易出錯了。工具
知道什麼是stylus文件格式後,咱們來看看一些在平時開發中經常使用到的一些技巧型的東西url
&符號,表示同級元素,即和&同一列樣式的全部者
// style文件 ul li color: red &:first-child font-size: 20px // css文件 ul li { color: #f00; } ul li:first-child { font-size: 20px; }
@name,表示繼承前面父級或本身已經定義過樣式的name的樣式
// stylus文件 .list background: red .part background: @background // css文件 .list { background: #f00; } .list .part { background: #f00; }
除了可使用@來使用定義好的樣式外,咱們還能夠給變量賦值樣式,讓好在後面調用
//stylus文件 font-size = 14px body font font-size Arial, sans-seri // css文件 body { font: 14px Arial, sans-seri; }
能夠將變量放在屬性中
// stylus文件 #prompt width: w = 200px margin-left: -(w / 2) // css文件 #prompt { width: 200px; margin-left: -100px; }
有條件的使用屬性
// stylus:指定z-index值爲1,可是,只有在z-index以前未指定的時候才這樣: // stylus文件 position() position: arguments z-index: 1 unless @z-index #logo z-index: 20 position: absolute #logo2 position: absolute // css文件 #logo { z-index: 20; position: absolute; } #logo2 { position: absolute; z-index: 1; }
函數方法
咱們能夠在stylus文件裏面定義函數,而後在後面調用(當沒有參數的時候,能夠直接使用arguments來代替)
// stylus文件 border-radius(val) -webkit-border-radius: val -moz-border-radius: val border-radius: val button border-radius(5px); // css文件 button { -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; }
Interpolation(插值)
// stylus文件 vendors = webkit moz o ms official border-radius() for vendor in vendors if vendor == official border-radius: arguments else -{vendor}-border-radius: arguments #content border-radius: 5px // css文件 #content { -webkit-border-radius: 5px; -moz-border-radius: 5px; -o-border-radius: 5px; -ms-border-radius: 5px; border-radius: 5px; }
@import
引入外來stylus文件,就可使用裏面定義的函數和變量來
@import('main.css') 當沒有指定文件後綴的時候默認爲stylus
@import('style/') 當文件名都沒有指定時,默認爲文件夾裏面的main.styl或index.styl
@font-face
// stylus文件 @font-face font-family Geo font-style normal src url(fonts/geo_sans_light/GensansLight.ttf) .ingeo font-family Geo // css文件 @font-face { font-family: Geo; font-style: normal; src: url("fonts/geo_sans_light/GensansLight.ttf"); } .ingeo { font-family: Geo; }
@media
// stylus文件 @media print #header #footer display none // css文件 @media print { #header, #footer { display: none; } }
@keyframes
// stylus文件 @keyframes pulse 0% background-color red transform scale(1.0) rotate(0deg) 33% background-color blue -webkit-transform scale(1.1) rotate(-5deg) // css文件 @-moz-keyframes pulse { 0% { background-color: #f00; transform: scale(1) rotate(0deg); } 33% { background-color: #00f; -webkit-transform: scale(1.1) rotate(-5deg); } } @-webkit-keyframes pulse { 0% { background-color: #f00; transform: scale(1) rotate(0deg); } 33% { background-color: #00f; -webkit-transform: scale(1.1) rotate(-5deg); } } @-o-keyframes pulse { 0% { background-color: #f00; transform: scale(1) rotate(0deg); } 33% { background-color: #00f; -webkit-transform: scale(1.1) rotate(-5deg); } } @keyframes pulse { 0% { background-color: #f00; transform: scale(1) rotate(0deg); } 33% { background-color: #00f; -webkit-transform: scale(1.1) rotate(-5deg); } }