stylus介绍

是个什么鬼?对于开发来说,CSS的弱点在于静态化。我们需要一个真正能提高开发效率的工具, LESS , SASS 都在这方面做了一些贡献。

Stylus 是一个CSS的预处理框架,2010年产生,来自Node.js社区,主要用来给Node项目进行CSS预处理支持,所以 Stylus 是一种新型语言,可以创建健壮的、动态的、富有表现力的CSS。比较年轻,其本质上做的事情与 SASS / LESS 等类似,应该是有很多借鉴,所以近似脚本的方式去写CSS代码。

Stylus 默认使用 .styl 的作为文件扩展名,支持多样性的CSS语法。

Stylus 功能上更为强壮,和js联系更加紧密。所以我选择 Stylus , SASS 玩儿过一段时间,主要是这玩意依赖ruby运行,所以我放弃使用了。

文档参考

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 目录专门存放 stylus 文件,在里面建立 example.styl 文件。然后在 stylusExample 目录下面执行下面命令

$ stylus --compress src/

输出 compiled 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怎么玩儿,俺写了个教程 Grunt教程-前端自动化 可以学习以下。

stylusExample 目录下创建两个文件,这两个文件是grunt必备文件。

package.json:用于保存项目元数据。Gruntfile.js:用于配置或定义任务、加载 Grunt 插件。

package.json 内容

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 install grunt-contrib-stylus --save-dev :stylus编写styl输出css

安装出现这样的警告 npm WARN package.json test@1.0.0 No README data 可以不理会,如果你看着不舒服,你需要在stylusExample目录下面建立一个 README.md 文件并输入内容。也可命令执行 echo "#stylus 学习" >> 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('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

编译成

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

编译成

css#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)

编译成

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

Nesting(嵌套)

stylus

header
#logo
border:1px solid red

编译成

cssheader #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

编译成

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

编译成

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

编译成

cssbutton {
-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;

编译成

cssbutton {
-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, 因为赋值在每个参数上,因此,我们可以无视单位换算。

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

Variables(变量)

常用方法

stylus

font-size = 14px

body
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值为1,但是,只有在z-index之前未指定的时候才这样:

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

Operators(运算符)

运算符优先级下表运算符优先级,从最高到最低:

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

@import

@import &quot;reset.css&quot;

当使用 @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;
}

.

stylus的更多相关文章

  1. PostCSS深入学习: PostCSS和Sass、Stylus或LESS一起使用

    如果你喜欢使用PostCSS,但又不想抛弃你最喜欢的预处理器.不用担心,你不需要作出二选一的选择,你可以把PostCSS和预处理器(Sass.Stylus或LESS)结合起来使用. 有几个PostCS ...

  2. CSS预处器的对比——Sass、Less和Stylus

    预处器的对比--Sass.LESS和Stylus 转载: 英文原文:http://net.tutsplus.com/tutorials/html-css-techniques/sass-vs-less ...

  3. 【前端开发】优化代码之减少引入,css预编译语言的优点,stylus的使用

    前言:我必须得承认在最最最开始的时候,我对于css的预编译是非常不以为然的,这是错误的.一般在页面编写过程中,我会将需要reset的css放在reset.css中,讲会需要重复用到的放置到public ...

  4. sass/less/stylus css编译

    早上来了听一同事说stylus如何才能编译成css文件,瞬时间有点蒙,一听感觉和less是差不多的功能,随着就上网去查,然后发现这个文章,介绍了这三种sass/less/stylus的安装和语法,贴在 ...

  5. CSS预处理器Sass、LESS 和 Stylus

    CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架.本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass.Less CSS.Stylus. 首先我们 ...

  6. sass、less和stylus的安装使用和入门实践

    刚 开始的时候,说实话,我很反感使用css预处理器这种新玩意的,因为其中涉及到了编程的东西,私以为很复杂,而且考虑到项目不是一天能够完成的,也很少是 一个人完成的,对于这种团队的项目开发,前端实践用c ...

  7. CSS预编译器配置-------LESS Sass Stylus webstorm

    预编译器配置说明 开头语,发挥CSS预处器的作用是一种很有挑战性的事情.CSS预处器有不同的语言,就有不同的语法和功能. 语法 在使用CSS预处器之前最重要的是对语法的理解,幸运的是,这三种CSS预处 ...

  8. CSS 预处理器(框架)初探:Sass、LESS 和 Stylus

    现在最为普遍的三款 CSS 预处理器框架,分别是 Sass.Less CSS.Stylus. 拿less来说,可以在页面上直接使用less文件,但要引用less.js进行解析:同时也可以直接将less ...

  9. 为您详细比较三个 CSS 预处理器(框架):Sass、LESS 和 Stylus

    CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架.本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass.Less CSS.Stylus. 首先我们 ...

  10. less,sass,stylus配置和应用教程及三者比较

    less,sass,stylus配置和应用教程及三者比较  Less 1. 定义: Less是CSS预处理语言,在css基础之上增加了诸如变量,混合(mix),继承,运算,函数等功能,LESS既可以运 ...

随机推荐

  1. 使用Nginx的配置对cc攻击进行简单防御

    ddos攻击:分布式拒绝服务攻击,就是利用大量肉鸡或伪造IP,发起大量的服务器请求,最后导致服务器瘫痪的攻击. cc攻击:类似于ddos攻击,不过它的特点是主要是发起大量页面请求,所以流量不大,但是却 ...

  2. ARM开发板不工作的几个原因

    刚焊了5块ARM(LPC2478)的开发板,上程序测试了一下,发现只有一个板子工作其他四个全部歇菜.努力地找了一会最终发现是板子的来个电阻焊翻了.因为是1206 的封装而且来个电阻在PCB上摆放的位置 ...

  3. 【字符串哈希】【哈希表】Aizu - 1370 - Hidden Anagrams

    给你两个4k长度的串,问你最长公共子串.两个子串相同被定义为所有字母的出现次数分别相同即可. 就枚举第一个串的所有子串,将字母出现的次数看作一个大数,进行哈希(双关键字),塞到哈希表里面.然后枚举第二 ...

  4. 升级到XCode6后,iOS8里设置tableview的setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法

    我们在使用tableview时会发现分割线的左边会短一些,通常可以使用 setSeparatorInset:UIEdgeInsetsZero 来解决.但是升级到XCode6之后,在iOS8里发现没有效 ...

  5. SpringBoot整合Mybatis多数据源 (AOP+注解)

    SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding=& ...

  6. 简单的INI解析封装

    简单封装的一个ini解析处理类(支持跨平台).支持功能: 加载并解析指定ini文件中的配置: 读取指定 section 下的指定 key 的值.提示:支持按数值型读取,或按文本类型读取:使用示例: a ...

  7. 十一. 图形、图像与多媒体5.Graphics2D类的绘图方法

    Java语言在Graphics类提供绘制各种基本的几何图形的基础上,扩展Graphics类提供一个Graphics2D类,它拥用更强大的二维图形处理能力,提供.坐标转换.颜色管理以及文字布局等更精确的 ...

  8. 将json字符串转换成list<T>

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Se ...

  9. 用js给循环的列表添加click事件

    纠结了两天终于搞定了,首先id这个东西必不可少,这个时候不能用onclik事件,而是需要使用代理事件. 比如说,这里有个列表如下: <ul> <li></li> & ...

  10. 多线程--Task,等待用户输入AutoResetEvent

    上一篇文章:.NET:如何让线程支持超时?已经说明目前微软主推的多线程方案是task: 注意:Task最好引用.NET4.5. 4.0也行,但不成熟.Thread引用2.0就够了. 1.通过构造函数创 ...