侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

CSS的预处理框架stylus学习教程

前端  /  管理员 发布于 7年前   288

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:</p><p> -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 内容

JavaScript Code复制内容到剪贴板
  1. {   
  2.   "name": "test",   
  3.   "version": "1.0.0",   
  4.   "description": "测试的例子",   
  5.   "repository": {   
  6.     "type": "git",   
  7.     "url": ""  
  8.   }   
  9. }  

然后安装必备插件,这些插件让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 文件变成了这样样子滴。

JavaScript Code复制内容到剪贴板
  1. {   
  2.   "name": "test",   
  3.   "version": "1.0.0",   
  4.   "description": "测试的例子",   
  5.   "repository": {   
  6.     "type": "git",   
  7.     "url": ""  
  8.   },   
  9.   "devDependencies": {   
  10.     "grunt": "^0.4.5",   
  11.     "grunt-contrib-stylus": "^0.21.0",   
  12.     "grunt-contrib-watch": "^0.6.1"  
  13.   }   
  14. }  

这个时候你需要使用这些插件儿完成你的任务需要在Gruntfile.js里面写你的执行任务。

JavaScript Code复制内容到剪贴板
  1. /// 包装函数   
  2. module.exports = function(grunt) {   
  3.     // 任务配置,所有插件的配置信息   
  4.     grunt.initConfig({   
  5.         pkg: grunt.file.readJSON('package.json'),   
  6.         stylus:{   
  7.             build: {   
  8.                 options: {   
  9.                     linenos: false,   
  10.                     compress: true  
  11.                 },   
  12.                 files: [{   
  13.                     'css/index.css': ['src/index.styl']   
  14.                 }]   
  15.             }   
  16.         },   
  17.         // watch插件的配置信息   
  18.         watch: {   
  19.             another: {   
  20.                 files: ['css/*','src/*.styl'],   
  21.                 tasks: ['stylus'],   
  22.                 options: {   
  23.                     livereload: 1337   
  24.                 }   
  25.             }   
  26.         }   
  27.     });   
  28.     // 告诉grunt我们将使用插件   
  29.     grunt.loadNpmTasks('grunt-contrib-watch');   
  30.     grunt.loadNpmTasks('grunt-contrib-stylus');   
  31.     // 告诉grunt当我们在终端中输入grunt时需要做些什么   
  32.     grunt.registerTask('default', ['watch']);   
  33. };  

注意读懂上面的哦,目录高对哦,这些没有必要提醒哦,这个时候你可以好好耍一下stylus
Stylus应用

这个时候真正的开始玩耍了哦。
Try Stylus!

stylus

CSS Code复制内容到剪贴板
  1. body,html   
  2.     margin:0   
  3.     padding:0  

编译成

CSS Code复制内容到剪贴板
  1. body,   
  2. html {   
  3.   margin: 0;   
  4.   padding: 0;   
  5. }  

stylus : 强大的功能丰富的语言

CSS Code复制内容到剪贴板
  1. -pos(type, args)   
  2.   i = 0   
  3.   position: unquote(type)   
  4.   {args[i]}: args[i + 1] is a 'unit' ? args[i += 1] : 0   
  5.   {args[i += 1]}: args[i + 1] is a 'unit' ? args[i += 1] : 0   
  6.   
  7. absolute()   
  8.   -pos('absolute', arguments)   
  9.   
  10. fixed()   
  11.   -pos('fixed', arguments)   
  12.   
  13. #prompt  
  14.   absolute: top 150px left 5px  
  15.   width: 200px  
  16.   margin-left: -(@width / 2)   
  17.   
  18. #logo  
  19.   fixed: top left  

编译成

CSS Code复制内容到剪贴板
  1. #prompt {   
  2.   position: absolute;   
  3.   top: 150px;   
  4.   left: 5px;   
  5.   width: 200px;   
  6.   margin-left: -100px;   
  7. }   
  8. #logo {   
  9.   position: fixed;   
  10.   top: 0;   
  11.   left: 0;   
  12. }  

nibStylus插件

stylus

CSS Code复制内容到剪贴板
  1. @import 'nib'  
  2. body   
  3.   background: linear-gradient(20px top, white, black)   

编译成

CSS Code复制内容到剪贴板
  1. body {   
  2.   background: -webkit-linear-gradient(20px top, #fff, #000);   
  3.   background: -moz-linear-gradient(20px top, #fff, #000);   
  4.   background: -o-linear-gradient(20px top, #fff, #000);   
  5.   background: -ms-linear-gradient(20px top, #fff, #000);   
  6.   background: linear-gradient(20px top, #fff, #000);   
  7. }  

Nesting(嵌套)

stylus

CSS Code复制内容到剪贴板
  1. header   
  2.     #logo  
  3.         border:1px solid red  

编译成

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

Flexible syntax(灵活的用法)

stylus

CSS Code复制内容到剪贴板
  1. body   
  2.     font 14px/1.5 Helvetica, arial, sans-serif  
  3.     button   
  4.     button.button   
  5.     input[type='button']   
  6.     input[type='submit']   
  7.         border-radius 5px  
  8.   
  9. header    
  10.     #logo,div   
  11.         font-size:14px  

编译成

CSS Code复制内容到剪贴板
  1. body {   
  2.   font: 14px/1.5 Helvetica, arial, sans-serif;   
  3. }   
  4. body button,   
  5. body button.button,   
  6. body input[type='button'] {   
  7.   border-radius: 5px;   
  8. }   
  9. header #logo,   
  10. header div {   
  11.   font-size: 14px;   
  12. }  

Flexible &(灵活&)

stylus

CSS Code复制内容到剪贴板
  1. ul   
  2.     li a   
  3.         display: block  
  4.         color: blue  
  5.         padding: 5px  
  6.         html.ie &   
  7.             padding: 6px  
  8.         &:hover   
  9.             color: red  

编译成

CSS Code复制内容到剪贴板
  1. ul li a {   
  2.   display: block;   
  3.   color: #00f;   
  4.   padding: 5px;   
  5. }   
  6. html.ie ul li a {   
  7.   padding: 6px;   
  8. }   
  9. ul li a:hover {   
  10.   color: #f00;   
  11. }  

Functions 方法
返回值

stylus

CSS Code复制内容到剪贴板
  1. border-radius(val)   
  2.     -webkit-border-radius: val   
  3.     -moz-border-radius: val   
  4.     border-radius: val   
  5.   
  6. button    
  7.     border-radius(5px);  

编译成

CSS Code复制内容到剪贴板
  1. button {   
  2.   -webkit-border-radius: 5px;   
  3.   -moz-border-radius: 5px;   
  4.   border-radius: 5px;   
  5. }  

Transparent mixins

不带参数

stylus

CSS Code复制内容到剪贴板
  1. border-radius()   
  2.     -webkit-border-radius: arguments   
  3.     -moz-border-radius: arguments   
  4.     border-radius: arguments   
  5.   
  6. button    
  7.     border-radius: 5px 10px;  

编译成

CSS Code复制内容到剪贴板
  1. button {   
  2.   -webkit-border-radius: 5px 10px;   
  3.   -moz-border-radius: 5px 10px;   
  4.   border-radius: 5px 10px;   
  5. }  

默认参数

不带参数

stylus

CSS Code复制内容到剪贴板
  1. add(a, b = a)   
  2.   a + b   
  3.   
  4. add(10, 5)   
  5. // => 15   
  6.   
  7. add(10)   
  8. // => 20  

函数体

通过内置unit()把单位都变成px, 因为赋值在每个参数上,因此,我们可以无视单位换算。

CSS Code复制内容到剪贴板
  1. add(a, b = a)   
  2.   a = unit(a, px)   
  3.   b = unit(b, px)   
  4.   a + b   
  5.   
  6. add(15%, 10deg)   
  7. // => 25  

多个返回值

通过内置unit()把单位都变成px, 因为赋值在每个参数上,因此,我们可以无视单位换算。

CSS Code复制内容到剪贴板
  1. sizes()   
  2.  15px 10px  
  3.   
  4. sizes()[0]   
  5. // => 15px  

  • 上一条:
    使用css实现圆角图形绘制
    下一条:
    CSS编程中一些值得注意的地方小结
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 使用 Alpine.js 排序插件对元素进行排序(0个评论)
    • 在js中使用jszip + file-saver实现批量下载OSS文件功能示例(0个评论)
    • 在vue中实现父页面按钮显示子组件中的el-dialog效果(0个评论)
    • 使用mock-server实现模拟接口对接流程步骤(0个评论)
    • vue项目打包程序实现把项目打包成一个exe可执行程序(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-03
    • 2018-04
    • 2018-05
    • 2018-06
    • 2018-09
    • 2018-11
    • 2018-12
    • 2019-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-04
    • 2021-05
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客