ホームページ  >  記事  >  ウェブフロントエンド  >  stylus_html/css_WEB-ITnose の使用を開始する

stylus_html/css_WEB-ITnose の使用を開始する

WBOY
WBOYオリジナル
2016-06-24 11:45:271466ブラウズ

スタイラスの紹介

それは一体何ですか?開発にとって、CSS の弱点は静的であることです。開発効率を本当に向上させるツールが必要です。LESS と SASS はこの点で貢献しています。

Stylus は、Node.js コミュニティによって 2010 年に作成された CSS 前処理フレームワークであり、主に Node プロジェクトの CSS 前処理をサポートするために使用されます。そのため、Stylus は、堅牢で動的、表現力豊かな CSS を作成できる新しい言語です。これは比較的新しく、本質的に行うことは SASS/LESS などに似ています。学ぶべきことはたくさんあるはずなので、CSS コードを記述するためのスクリプトに似ています。

Stylus はデフォルトでファイル拡張子として .styl を使用し、多様な CSS 構文をサポートします。

Stylus は機能がより強力で、js とより密接に関連しています。そこで私は Stylus を選択し、しばらく SASS で遊んでみました。主な理由は、これが実行するのに Ruby に依存していたため、SASS の使用をやめたからです。

ドキュメントリファレンス

公式Stylus API
Zhang Xinxu中国語翻訳
Stylusを試してください!

Stylusのインストール

グローバルインストール、インストール前にnodejsをインストールする必要があります。インストール方法を検索してください。

$ npm install stylus -g

こうすることで、Stylus をインストールした後でも、通常通り Stylus を使用することができます。

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ディレクトリを作成し、その中にexample.stylファイルを作成します。次に stylusExample ディレクトリ

$ stylus --compress src/

で以下のコマンドを実行すると、コンパイルされた src/example.css が出力されます。これは --compress パラメータを持ってきたことを意味します。圧縮 CSS ファイルが生成されました。

$ stylus --css css/example.css css/out.styl CSSをstylに変換します
$ stylus help box-shadow CSSプロパティヘルプ
$ stylus --css test.css 同じベース名で.stylファイルを出力します

grunt 生成

grunt 生成は、具体的には Grunt チュートリアル - フロントエンド自動化のチュートリアルを書きました。

stylusExample ディレクトリに 2 つのファイルを作成します。これらの 2 つのファイルは grunt に必要なファイルです。

package.json: プロジェクトのメタデータを保存するために使用されます。
Gruntfile.js: タスクを構成または定義し、Grunt プラグインをロードするために使用されます。

package.json content

json{  "name": "test",  "version": "1.0.0",  "description": "测试的例子",  "repository": {    "type": "git",    "url": ""  }}

次に、これらのプラグインをインストールすると、スタイラス ファイルの変更が自動的に生成され、対応するファイル ディレクトリに生成されます。エラーが報告された場合は、コマンドラインの前に sudo を追加して、最大の実行権限を付与する必要があります。

npm install grunt --save-dev
npm install grunt-contrib-watch --save-dev: ファイルの変更を監視し、対応するアクションを実行します。 npm>>
npm install grunt-contrib-stylus --save-dev: stylus writes styl Output css npm>>

インストールするとこのような警告が表示されます npm WARN package.json test@1.0.0 README データがありません 無視できます不快に感じる場合は、stylusExample ディレクトリの下に README.md ファイルを作成し、内容を入力する必要があります。コマンド echo "#stylus learning" >> README.md を実行することもできます

プラグインが実行されると、package.json ファイルは次のようになります。

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 に記述する必要があります。

js/// 包装函数module.exports = function(grunt) {    // 任务配置,所有插件的配置信息    grunt.initConfig({        pkg: grunt.file.readJSON(&#39;package.json&#39;),        stylus:{            build: {                options: {                    linenos: false,                    compress: true                },                files: [{                    &#39;css/index.css&#39;: [&#39;src/index.styl&#39;]                }]            }        },        // watch插件的配置信息        watch: {            another: {                files: [&#39;css/*&#39;,&#39;src/*.styl&#39;],                tasks: [&#39;stylus&#39;],                options: {                    livereload: 1337                }            }        }    });    // 告诉grunt我们将使用插件    grunt.loadNpmTasks(&#39;grunt-contrib-watch&#39;);    grunt.loadNpmTasks(&#39;grunt-contrib-stylus&#39;);    // 告诉grunt当我们在终端中输入grunt时需要做些什么    grunt.registerTask(&#39;default&#39;, [&#39;watch&#39;]);};

上記を読んで理解してください。ディレクトリは正しいので、通知する必要はありません。この時点で、スタイラス

スタイラスアプリケーション

を使ってプレイできます。ここからが実際にプレイを開始する時間です。

Stylus をお試しください!

stylus

body,html    margin:0    padding:0

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

stylus にコンパイルされました: 強力な機能豊富な言語

-pos(type, args)  i = 0  position: unquote(type)  {args[i]}: args[i + 1] is a &#39;unit&#39; ? args[i += 1] : 0  {args[i += 1]}: args[i + 1] is a &#39;unit&#39; ? args[i += 1] : 0absolute()  -pos(&#39;absolute&#39;, arguments)fixed()  -pos(&#39;fixed&#39;, arguments)#prompt  absolute: top 150px left 5px  width: 200px  margin-left: -(@width / 2)#logo  fixed: top left

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

nibSt にコンパイルされましたylus プラグイン

stylus

@import &#39;nib&#39;body  background: linear-gradient(20px top, white, black)

コンパイルinto

cssbody {  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);}

ネスティング(入れ子)

stylus

header    #logo        border:1px solid red

Compile into

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

柔軟な構文(柔軟な使い方)

stylus

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

Compile

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

フレキシブル&(フレキシブル& )

stylus

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

Compile into

cssul 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: valbutton     border-radius(5px);

Compile into

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

パラメータなし

スタイラス

border-radius()    -webkit-border-radius: arguments    -moz-border-radius: arguments    border-radius: argumentsbutton     border-radius: 5px 10px;

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

デフォルトパラメータにコンパイルされます

パラメータなし

スタイラス

add(a, b = a)  a + badd(10, 5)// => 15add(10)// => 20

関数本体

各パラメータに割り当てがあるため、単位は組み込みのunit()を通じてpxに変更されますしたがって、単位変換は無視できます。

add(a, b = a)  a = unit(a, px)  b = unit(b, px)  a + badd(15%, 10deg)// => 25

複数の戻り値

組み込みのunit()を通じて単位をpxに変換します。値は各パラメータに割り当てられているため、単位の変換は無視できます。

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

Variables(変数)

共通メソッド

stylus

font-size = 14pxbody    font font-size Arial, sans-seri

cssbody {  font: 14px Arial, sans-seri;}
にコンパイルされます

変数は属性に配置されます

stylus

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

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

ブロックにコンパイルされる属性 参照

stylus

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

へのアクセスは、条件付きで属性を定義する

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

属性にコンパイルされます

stylus: ただし、z-index が事前に指定されていない場合に限り、z-index 値 1 を指定します。 :

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:属性会“向上冒泡”查找堆栈直到被发现,或者返回null(如果属性搞不定)下面这个例子,@color被弄成了blue.

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

编译成

cssbody {  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

编译成

csstable 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 officialborder-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;}

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.styl或blueprint/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);  }}

CSS字面量(CSS Literal)

stylus

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

编译成

cssbody {  font: 14px;}

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。