stylus让你用更骚的姿势写css

认识stylus这货

张鑫旭老师的stylus文档

我这里有一波祖传的mixin, 可以让你写更精简可复用的css代码, 这位少侠请了解一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223

tac()
text-align center

norpt()
background-repeat no-repeat

// 文本处理
txt($width, $height, $size)
width $width
height $height
line-height $height
font-size $size
tac()

// 字体大小
fs($size)
font-size $size

// 文本加粗
bold()
font-weight bold

// 行内块
inline()
display inline-block

// 圆
circle($size)
width $size
height $size
border-radius 100%

// 小圆点
dot($size, $color)
display inline-block
width $size
height $size
border-radius 100%
background-color $color

// 模态对话框
modal()
z-index 1000
position fixed
top 0
right 0
bottom 0
left 0
background-color rgba(0, 0, 0, .6)

// 占满宽度
full-width($height)
width 375px
height $height

// 头像
avatar($size)
norpt()
width $size
height $size
background-size $size $size
border-radius 100%

// 描线
stroke-line($width, $y)
width $width
border 0.5px solid #8A91A4
transform translateY($y)

// 左浮动
fl($margin-left)
float left
margin-left $margin-left

// 文本行
text-line($height, $font-size)
inline()
height $height
line-height $height
font-size $font-size

// 背景图
bg($width, $height, $img)
norpt()
background-size $width $height
background-image url('../../assets/images/' + $img + '@2x.png')
@media (-webkit-min-device-pixel-ratio 3), (min-device-pixel-ratio 3)
background-image url('../../assets/images/' + $img + '@3x.png')

// 带背景图
with-bg($width, $height, $img)
norpt()
width $width
height $height
background-size $width $height
background-image url('../../assets/images/' + $img + '@2x.png')
@media (-webkit-min-device-pixel-ratio 3), (min-device-pixel-ratio 3)
background-image url('../../assets/images/' + $img + '@3x.png')

// 有背景图
has-bg($width, $height)
norpt()
width $width
height $height
background-size $width $height

// 组件背景图
bg-img($img)
background-image url('../../assets/images/' + $img + '@2x.png')
@media (-webkit-min-device-pixel-ratio 3), (min-device-pixel-ratio 3)
background-image url('../../assets/images/' + $img + '@3x.png')

// app.vue背景图
app-bg-img($img)
background-image url('./assets/images/' + $img + '@2x.png')
@media (-webkit-min-device-pixel-ratio 3), (min-device-pixel-ratio 3)
background-image url('./assets/images/' + $img + '@3x.png')

// 全屏带背景图
with-bg-fs($img)
width 375px
height 667px
background-image url($img)
background-size 375px 667px

// 绝对定位, left, top
ab-lt($left, $top)
position absolute
top $top
left $left

// 绝对定位, right, top
ab-rt($right, $top)
position absolute
top $top
right $right

// 绝对定位, left, bottom
ab-lb($left, $bottom)
position absolute
left $left
bottom $bottom

// 绝对定位, right, bottom
ab-rb($right, $bottom)
position absolute
right $right
bottom $bottom

ab-r($right)
position absolute
right $right
top 50%
transform translateY(-50%)

// 绝对定位, 上下居中, left
ab-l($left)
position absolute
left $left
top 50%
transform translateY(-50%)

// 绝对定位, 左右居中, top
ab-center-t($top)
position absolute
left 50%
top $top
transform translateX(-50%)

// 绝对定位, 上下左右都居中
ab-center()
position absolute
top 50%
left 50%
transform translate(-50%, -50%)

// 不换行
no-wrap()
text-overflow ellipsis
overflow hidden
white-space nowrap

// 扩展点击区域
extend-click()
&:before
content ''
position absolute
top -10px
left -10px
right -10px
bottom -10px

// 灰阶处理
gray($scale)
filter grayscale($scale)

// 1px边框
border-1px($color)
position: relative
&:after
display: block
position: absolute
left: 0
bottom: 0
width: 100%
border-top: 1px solid $color
content: ' '
@media (-webkit-min-device-pixel-ratio: 1.5),(min-device-pixel-ratio: 1.5)
-webkit-transform: scaleY(0.7)
transform: scaleY(0.7)
@media (-webkit-min-device-pixel-ratio: 2),(min-device-pixel-ratio: 2)
-webkit-transform: scaleY(0.5)
transform: scaleY(0.5)
@media (-webkit-min-device-pixel-ratio: 3),(min-device-pixel-ratio: 3)
-webkit-transform: scaleY(0.33)
transform: scaleY(0.33)

// 占满全屏
full-screen()
width 100vw
height 100vh
position relative

用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<template>
<div class="sub-path-card-root">
<div
class="pic"
:style="{ backgroundImage: `url(${ imgUrl })` }"
>
</div>
<div class="txt">{{ name }}</div>
<jsl-stars class="stars" :score=score />
</div>
</template>


<script type="text/ecmascript-6">
import JslStars from 'base-components/stars/stars'

export default {
props: ['img', 'score', 'name', 'imgUrl'],
data () {
return {
data: ''
}
},
components: {
JslStars
}
}
</script>


<style scoped lang="stylus" rel="stylesheet/stylus">
@import "~styles/mixin"

.sub-path-card-root
width 101px
margin 0 auto
.pic
margin 0 auto
has-bg(101px, 101px)
.bg1
bg-img('bg1')
.bg2
bg-img('bg2')
.bg3
bg-img('bg3')
.txt
txt(43px, 22px, 16px)
margin 7px auto
color #fff
.stars
margin 9px auto 20px
</style>