VUE -- stylus入門使用方法

sizes()
 15px 10px

sizes()[0]
// => 15px

 

stylus介紹

是個什麼鬼?對於開發來講,CSS的弱點在於靜態化。咱們須要一個真正能提升開發效率的工具,LESS, SASS都在這方面作了一些貢獻。css

Stylus 是一個CSS的預處理框架,2010年產生,來自Node.js社區,主要用來給Node項目進行CSS預處理支持,因此 Stylus 是一種新型語言,能夠建立健壯的、動態的、富有表現力的CSS。比較年輕,其本質上作的事情與 SASS/LESS 等相似,應該是有不少借鑑,因此近似腳本的方式去寫CSS代碼。html

Stylus默認使用 .styl 的做爲文件擴展名,支持多樣性的CSS語法。前端

Stylus功能上更爲強壯,和js聯繫更加緊密。因此我選擇 StylusSASS 玩兒過一段時間,主要是這玩意依賴ruby運行,因此我放棄使用了。node

文檔參考

官方Stylus API 
張鑫旭中文翻譯
Try Stylus!git

Stylus安裝

全局安裝,安裝以前你須要你安裝 nodejs 這個怎麼安裝搜去哦。github

$ npm install stylus -g

這樣就算是安裝完Stylus了,也能夠正常使用Stylus。web

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  

 

生成CSS

命令行中

創建一個stylusExample/,再在裏面創建 src 目錄專門存放 stylus 文件,在裏面創建 example.styl 文件。而後在 stylusExample 目錄下面執行下面命令npm

$ stylus --compress src/json

輸出compiled src/example.css ,這個時候表示你生成成功了,帶上--compress參數表示你生成壓縮的CSS文件。ruby

$ stylus --css css/example.css css/out.styl CSS轉換成styl 
$ stylus help box-shadow CSS屬性的幫助 
$ stylus --css test.css 輸出基本名一致的.styl文件

grunt生成

grunt生成 就比較爽歪歪了,具體grunt怎麼玩兒,俺寫了個教程Grunt教程-前端自動化 能夠學習如下。

stylusExample 目錄下建立兩個文件,這兩個文件是grunt必備文件。

package.json:用於保存項目元數據。 
Gruntfile.js:用於配置或定義任務、加載 Grunt 插件。

package.json 內容

{
  "name": "test",
  "version": "1.0.0",
  "description": "測試的例子",
  "repository": {
    "type": "git",
    "url": ""
  }
}

而後安裝必備插件,這些插件讓stylus文件變動了自動生成,生成到相對應的文件目錄中。若是報錯你須要在命令行前面加上sudo,給它最大的執行權限。

npm install grunt --save-dev 
npm install grunt-contrib-watch --save-dev :監視文件變更,作出相應動做。npm>> 
npm install grunt-contrib-stylus --save-dev :stylus編寫styl輸出css npm>>

安裝出現這樣的警告 npm WARN package.json test@1.0.0 No README data 能夠不理會,若是你看着不舒服,你須要在stylusExample目錄下面創建一個 README.md 文件並輸入內容。也可命令執行 echo "#stylus 學習" >> README.md

插件執行完畢以後 package.json 文件變成了這樣樣子滴。

{
  "name": "test",
  "version": "1.0.0",
  "description": "測試的例子",
  "repository": {
    "type": "git",
    "url": ""
  },
  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-contrib-stylus": "^0.21.0",
    "grunt-contrib-watch": "^0.6.1"
  }
}

這個時候你須要使用這些插件兒完成你的任務須要在Gruntfile.js裏面寫你的執行任務。

/// 包裝函數
module.exports = function(grunt) {
    // 任務配置,全部插件的配置信息
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        stylus:{
            build: {
                options: {
                    linenos: false,
                    compress: true
                },
                files: [{
                    'css/index.css': ['src/index.styl']
                }]
            }
        },
        // watch插件的配置信息
        watch: {
            another: {
                files: ['css/*','src/*.styl'],
                tasks: ['stylus'],
                options: {
                    livereload: 1337
                }
            }
        }
    });
    // 告訴grunt咱們將使用插件
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-stylus');
    // 告訴grunt當咱們在終端中輸入grunt時須要作些什麼
    grunt.registerTask('default', ['watch']);
};

 

注意讀懂上面的哦,目錄高對哦,這些沒有必要提醒哦,這個時候你能夠好好耍一下stylus

Stylus應用

這個時候真正的開始玩耍了哦。

Try Stylus!

stylus

body,html
    margin:0
    padding:0

編譯成

body,
html {
  margin: 0;
  padding: 0;
}

stylus : 強大的功能豐富的語言

-pos(type, args)
  i = 0
  position: unquote(type)
  {args[i]}: args[i + 1] is a 'unit' ? args[i += 1] : 0
  {args[i += 1]}: args[i + 1] is a 'unit' ? args[i += 1] : 0

absolute()
  -pos('absolute', arguments)

fixed()
  -pos('fixed', arguments)

#prompt
  absolute: top 150px left 5px
  width: 200px
  margin-left: -(@width / 2)

#logo
  fixed: top left

編譯成

#prompt {
  position: absolute;
  top: 150px;
  left: 5px;
  width: 200px;
  margin-left: -100px;
}
#logo {
  position: fixed;
  top: 0;
  left: 0;
}

nibStylus插件

stylus

@import 'nib'
body
  background: linear-gradient(20px top, white, black) 

編譯成

body {
  background: -webkit-linear-gradient(20px top, #fff, #000);
  background: -moz-linear-gradient(20px top, #fff, #000);
  background: -o-linear-gradient(20px top, #fff, #000);
  background: -ms-linear-gradient(20px top, #fff, #000);
  background: linear-gradient(20px top, #fff, #000);
}

Nesting(嵌套)

stylus

header
    #logo
        border:1px solid red

編譯成

header #logo {
  border: 1px solid #f00;
}

Flexible syntax(靈活的用法)

stylus

body
    font 14px/1.5 Helvetica, arial, sans-serif
    button
    button.button
    input[type='button']
    input[type='submit']
        border-radius 5px
        
header 
    #logo,div
        font-size:14px

編譯成

body {
  font: 14px/1.5 Helvetica, arial, sans-serif;
}
body button,
body button.button,
body input[type='button'] {
  border-radius: 5px;
}
header #logo,
header div {
  font-size: 14px;
}

 

Flexible &(靈活&)

stylus

ul
    li a
        display: block
        color: blue
        padding: 5px
        html.ie &
            padding: 6px
        &:hover
            color: red

 

編譯成

ul li a {
  display: block;
  color: #00f;
  padding: 5px;
}
html.ie ul li a {
  padding: 6px;
}
ul li a:hover {
  color: #f00;
}

Functions 方法

返回值

stylus

border-radius(val)
    -webkit-border-radius: val
    -moz-border-radius: val
    border-radius: val

button 
    border-radius(5px);

 

編譯成

button {
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}

 

Transparent mixins

不帶參數

stylus

border-radius()
    -webkit-border-radius: arguments
    -moz-border-radius: arguments
    border-radius: arguments

button 
    border-radius: 5px 10px;

 

編譯成

button {
  -webkit-border-radius: 5px 10px;
  -moz-border-radius: 5px 10px;
  border-radius: 5px 10px;
}

 

默認參數

不帶參數

stylus

add(a, b = a)
  a + b

add(10, 5)
// => 15

add(10)
// => 20

函數體

經過內置unit()把單位都變成px, 由於賦值在每一個參數上,所以,咱們能夠無視單位換算。

add(a, b = a)
  a = unit(a, px)
  b = unit(b, px)
  a + b

add(15%, 10deg)
// => 25

多個返回值

經過內置unit()把單位都變成px, 由於賦值在每一個參數上,所以,咱們能夠無視單位換算。

 

Variables(變量)

經常使用方法

stylus

font-size = 14px

body
    font font-size Arial, sans-seri

 

編譯成

body {
  font: 14px Arial, sans-seri;
}

變量放在屬性中

stylus

#prompt
    position: absolute
    top: 150px
    left: 50%
    width: w = 200px
    margin-left: -(w / 2)

編譯成

#prompt {
  position: absolute;
  top: 150px;
  left: 50%;
  width: 200px;
  margin-left: -100px;
}

塊屬性訪問引用

stylus

#prompt
    position: absolute
    width: 200px
    margin-left: -(@width / 2)

編譯成

#prompt {
  position: absolute;
  width: 200px;
  margin-left: -100px;
}

屬性有條件地定義屬性

stylus:指定z-index值爲1,可是,只有在z-index以前未指定的時候才這樣:

position()
  position: arguments
  z-index: 1 unless @z-index

#logo
  z-index: 20
  position: absolute

#logo2
  position: absolute

編譯成

#logo {
  z-index: 20;
  position: absolute;
}
#logo2 {
  position: absolute;
  z-index: 1;
}

向上冒泡

stylus:屬性會「向上冒泡」查找堆棧直到被發現,或者返回null(若是屬性搞不定)下面這個例子,@color被弄成了blue.

body
  color: red
  ul
    li
      color: blue
      a
        background-color: @color

編譯成

body {
  color: #f00;
}
body ul li {
  color: #00f;
}
body ul li a {
  background-color: #00f;
}

Iteration(迭代)

stylus

table
    for row in 1 2 3 4 5
        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;
}
table tr:nth-child(4) {
  height: 40px;
}
table tr:nth-child(5) {
  height: 50px;
}

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

編譯成

#content {
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  -o-border-radius: 5px;
  -ms-border-radius: 5px;
  border-radius: 5px;
}

Operators(運算符)

運算符優先級 
下表運算符優先級,從最高到最低:

.
 []
 ! ~ + -
 is defined
 ** * / %
 + -
 ... ..
 <= >= < >
 in
 == is != is not isnt
 is a
 && and || or
 ?:
 = := ?= += -= *= /= %=
 not
 if unless

@import

@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.stylblueprint/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

編譯成

@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

編譯成

@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)

編譯成

@-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);
  }
}

CSS字面量(CSS Literal)

stylus

@css {
  body {
    font: 14px;
  }
}

編譯成

body {
  font: 14px;
}

工具

相關文章
相關標籤/搜索