stylus 在静态页面上的使用经验

前段时间做vue项目,用到了css的提升开发效率的工具stylus,感觉很好用。现在又开始写静态页面了,于是将强大的stylus拿过来继续用。于是就写了这篇使用经验,算是自己总结一下。

stylus的安装

使用前,我们需要在终端里面进行全局安装stylus,这样在项目中可以使用stylus将styl文件解析成css(当然也可以将css反编译成styl文件)。

$ npm install stylus -g

可以使用命令查看是否安装成功了(大写)

$ stylus -V

安装完成之后你可以看到下面一些常用的参数

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是我们会常用到的

-w :监听文件,只要原文件改变,解析后的目标文件也会同时改变
-o :指定目标文件,不指定的话就是和源文件同名
-c :压缩文件,将源文件解析后并压缩

stylus的命令行操作

安装完成后,我们进入项目的根目录(最好是style这级目录),假如我们有一个style目录,里面有一个example.styl文件(stylus文件的后缀名就是styl),对文件进行解析。

// 将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的基本使用语法

所有的前期准备工作完成,现在开始对stylus进行基本使用,看看效果

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文件格式后,我们来看看一些在平时开发中常用到的一些技巧型的东西

&符号

&符号,表示同级元素,即和&同一列样式的所有者

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

Variables(变量)

除了可以使用@来使用定义好的样式外,我们还可以给变量赋值样式,让好在后面调用

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

原文地址:https://www.cnblogs.com/HarkPark/p/8684894.html

时间: 2024-08-04 23:41:21

stylus 在静态页面上的使用经验的相关文章

浅谈在静态页面上使用动态参数,会造成spider多次和重复抓取的解决方案

原因: 早期由于搜索引擎蜘蛛的不完善,蜘蛛在爬行动态的url的时候很容易由于网站程序的不合理等原因造成蜘蛛迷路死循环. 所以蜘蛛为了避免之前现象就不读取动态的url,特别是带?的url 解决方案: 1):配置路由 routes.MapRoute("RentofficeList", "rentofficelist/{AredId}-{PriceId}-{AcreageId}-{SortId}-{SortNum}.html", new { controller = &q

mac os x 之通过远程主机在nginx上部署web静态页面

1.mac使用ssh命令登陆远程主机 因为苹果mac os x自带ssh命令,所以我们只需打开终端输入 $ ssh [email protected] 在这之前最好在服务器上上传自己的ssh key,避免每次登陆输入密码 稍作等待就连接上服务器了   2.mac使用scp命令向远处主机上传文件 在终端窗口,按下command+n,打开另一个终端窗口,并输入 $ scp ~/local/file [email protected]:~/file  当然一般我们上传的是文件夹,所以加上-r $ sc

关于线上静态页面资源更新的一些经验分享

目录 关于线上静态页面资源更新的一些经验分享 关于Linux的Patch 关于git 关于Idea 关于线上静态页面资源更新的一些经验分享 最近在负责公司的后台项目,包括了后端和前端.后端直接编译完打成jar包直接上线运行没什么问题.但是前端的页面文件更新每次都要把页面给运维,然后告诉运维路径让运维挨个替换,当然也可以整包替换, 但是如果文件比较多的情况下,整包替换就不合适了.因为现在开发的项目版本控制基本必不可少了,这时候可以利用版本控制软件来生成Patch文件,然后直接交给运维,让运维在项目

mac上nginx静态页面访问403

第一天配置好的nginx可以正常的访问静态页面,第二天通过网络云盘下载的index.html文件之后,再次访问出现了403forbidden的问题. 查看文件的权限: [email protected] 1 hu staff 6351 4 12 21:10 bluePlan.js [email protected] 1 hu staff 113 4 12 21:10 bluePlan.less [email protected] 1 hu staff 1146 4 12 21:19 index.

DataTables学习:从最基本的入门静态页面,使用ajax调用Json本地数据源实现前端开发深入学习,根据后台数据接口替换掉本地的json本地数据,以及报错的处理地方,8个例子(显示行附加信息,回调使用api,动态显示和隐藏列...),详细教程

一.DataTables  个人觉得学习一门新的插件或者技术时候,官方文档是最根本的,入门最快的地方,但是有时候看完官方文档,一步步的动手写例子,总会出现各种莫名其妙的错误,需要我们很好的进行研究出错的地方. 官方网站(中文):http://datatables.club/ 官方网站:https://www.datatables.net/  二.简单的例子 怎样简单地使用DataTables?使用下方简单的几行代码,一个方法初始化table. $(document).ready(function

只用table模仿一个静态页面

尝试只用table标签来模仿一个静态页面,这样做可以很好的增加对网页结构理解. 要尝试模仿的页面如下: 附上我自己写的代码,因为只是用了HTML,没有用到CSS和js,所以有些样式写出来和原图有些出入 <!DOCTYPE html> <html> <head> <title>只用table来模仿一个网页</title> <meta charset="utf-8"> </head> <body>

PHP代码为什么不能直接保存HTML文件——&gt;PHP生成静态页面教程

1.服务器会根据文件的后缀名去进行解析,如果是HTML文件则服务器不会进行语法解析,而是直接输出到浏览器. 2.如果一个页面中全部都是HTML代码而没有需要解析的PHP语法,则没有必要保存为PHP文件,这样反而会降低运行效率. 3.如果是需要PHP控制HTML代码的输出,比如需要PHP判断用户是否登陆,如果登陆则输出A,未登录则输出B.这就需要PHP来进行控制了.HTML不能实现这样的功能 PHP生成静态页面教程 ,一些基本的概念 一,PHP脚本与动态页面. PHP脚本是一种服务器端脚本程序,可

学习MVC之租房网站(十二)-缓存和静态页面

在上一篇<学习MVC之租房网站(十一)-定时任务和云存储>学习了Quartz的使用.发邮件,并将通过UEditor上传的图片保存到云存储.在项目的最后,再学习优化网站性能的一些技术:缓存和页面静态化. 使用缓存可以降低数据库的压力,而使用页面静态化则可以降低Web服务器的压力. 一 缓存 ASP.NET下可用的缓存为System.Web.Caching.Cache,保存在服务器内存中,不适用于服务器集群,虽然也用没用过集群,但现在的主流都使用NoSQL数据库来做缓存,典型的有Redis和Mem

ASP.NET MVC生成静态页面

1.先付上封装好生成静态页的原代码: public class Common { #region 获取模板页的Html代码 /// <summary> /// 获取页面的Html代码 /// </summary> /// <param name="url">模板页面路径</param> /// <param name="encoding">页面编码</param> /// <returns