大家好,我是翟路佳。全栈工程师,编程爱好者,喜欢分享。
我今年的首要目标是成为一名合格的讲师,帮助尽可能多的同学获得进步。
经常出没于:
总之,CSS 预处理工具的目的是提高 CSS 的开发效率。
对战开始!
Stylus 的优势:
@extend
PostCSS 的优势:
我选择 Stylus 的原因:
body
font 12px Helvetica, Arial, sans-serif
a.button
border-radius 5px
编译为
body {
font: 12px Helvetica, Arial, sans-serif;
}
a.button {
border-radius: 5px;
}
.foo
color red
.bar
color blue
编译为:
.foo {
color: red;
}
.foo .bar {
color: blue;
}
a
color red
&:hover
color yellow
编译为:
a {
color: red;
}
a:hover {
color: yellow;
}
$font-size = 14px
body
font-size $font-size
编译为:
body {
font-size: 14px;
}
安装
npm install stylus -g
使用
stylus [options] [文件|目录...] -o css/
常用 options
@import
导入的 CSSpackage.json
{
"scripts": {
"stylus": "mkdir css & stylus -m -w styl/screen.styl -o css/"
}
}
npm i gulp-stylus gulp-clean-css -D
gulpfile.js
gulp.task('stylus', () => {
return gulp.src('./styl/screen.styl')
.pipe(stylus({
compress: true,
'include css': true
}))
.pipe(cleanCSS({
level: 2,
rebaseTo: 'css/'
}))
.pipe(gulp.dest(DEST + 'css/'));
});
^[N..M]
a
b
c
d
font-size 14px
.abc ^[-1..-1]:hover
color red
编译为:
a b c d {
font-size: 14px;
}
.abc d:hover {
color: #f00;
}
p
margin 10px
padding (@margin / 2)
编译为:
p {
margin: 10px;
padding: 5px;
}
适用场景:用绝对定位居中
#logo
position absolute
top 50%
left 50%
width 150px
height 80px
margin-left -(@width / 2)
margin-top -(@height / 2)
margin (-@height / 2) 0 0 (-@width / 2)
for n in 1..10
.col-{n}
width 10% * n
编译成
.col-1 {
width: 10%;
}
.col-2 {
width: 20%;
}
....
适用场景:日历组件
for n in (0..6)
saturday = 8 - n
sunday = 9 - n
&.empty-{n}
flex n n 14.285714%*n
min-width 14.285714%*n
max-width 100%
~ :nth-child(7n + {saturday}),
~ :nth-child(7n + {sunday})
color orange
适用场景:spin
.bar
animation shousuo 1s ease-in-out infinite
for n in 1..5
&:nth-child({n})
animation-delay .1s * n
其它 spinkit
if/else
$need-support-ie = true
body
if $need-support-ie
padding 5px
else
margin 5px
编译为
body {
padding: 5px;
}
border-radius(n)
-webkit-border-radius n
-moz-border-radius n
border-radius n
form input[type=button]
border-radius(5px) // function
border-radius 5px // mixin
编译为:
form input[type=button] {
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
}
args...
box-shadow(args...)
-webkit-box-shadow args
-moz-box-shadow args
box-shadow args
#login
box-shadow 1px 2px 5px #eee
编译为:
#login {
-webkit-box-shadow: 1px 2px 5px #eee;
-moz-box-shadow: 1px 2px 5px #eee;
box-shadow: 1px 2px 5px #eee;
}
实例:开发过渡效果墙
我的观点:
darken
/lighten
embedurl
+prefix-classes(prefix)
.btn-primary
background-color $primary-color
border-color darken($primary-color, 15%)
.link
color red
&:hover
color lighten(red, 10%)
background: embedurl('logo.png')
// => background: url("data:image/png;base64,…")
background: embedurl('logo.svg', 'utf8')
// => background: url("data:image/svg+xml;charset=utf-8,…")
+prefix-classes('.tqb-dp-')
.header
height 2.5rem
.back-button
color white
.tqb-dp-header {
height: 2.5rem;
}
.tqb-dp-header .tqb-dp-back-button {
color: white;
}
适用场景:独立组件
@import 'nib'
body
background linear-gradient(top, white, black)
编译为:
body {
background: -webkit-gradient(linear, left top, left bottom, color-stop(0, #fff), color-stop(1, #000));
background: -webkit-linear-gradient(top, #fff 0%, #000 100%);
background: -moz-linear-gradient(top, #fff 0%, #000 100%);
background: linear-gradient(top, #fff 0%, #000 100%);
}
#back-to-top
fixed bottom right
编译为
#back-to-top {
position: fixed;
right: 0;
bottom: 0;
}
.intrinsic-ratio-box
height 0
padding-bottom 20%
position relative
.real-container
absolute top left
width 100%
height 100%
屏幕上不可见,但对搜索引擎和阅读器可见。
.visually-hidden
border 0
clip rect(0,0,0,0)
position absolute
width 1px
height 1px
margin -1px
overflow hidden
padding 0
$
,如 $border-color
@extend
,多用 mixinQ&A
参考阅读: