1. less循环
2. 内置函数`unit` `percentage` `image-width` `image-height`
3. `/`在background中的异常
4. 变量有作用域,不同的作用域可以使用同名的名字,(乱入:padding单位为%是相对宽度的)
与html结构对应,便于维护
.agreement {
> label {
width: 39px;
> em {
display: block;
width: 12px;
height: 12px;
margin: 14px 0 0 15px;
border: 1px solid #ccc;
border-radius: 50%;
position: relative;
}
&.checked > em {
border-color: #57bd77;
&::before {
content: "";
position: absolute;
width: 4px;
height: 4px;
left: 4px;
top: 4px;
background-color: #57bd77;
border-radius: 50%;
}
}
}
> span {
flex: 1;
padding: 12px 0;
font-size: 12px;
color: #9fafbd;
> a {
color: #10c172;
}
}
}
推荐使用子选择器(>)而不是后代选择器,因为更具体,可以减少干扰。
.cloud-2- {
&1 {
position: absolute;
left: percentage(-195px/@base-width);
top: percentage(-174px/@base-height);
width: percentage(310px/@base-width);
transition: all .65s ease;
}
&2 {
position: absolute;
left: percentage(-178px/@base-width);
bottom: percentage(-32px/@base-height);
width: percentage(186px/@base-width);
transition: all .65s ease;
}
}
.foo, .bar{
& + &{
font-size: 12px;
}
}
.foo + .foo,
.foo + .bar,
.bar + .foo,
.bar + .bar {
font-size: 12px;
}
.reimburse-tips > div {
@image-width: image-width("../img/tips-bg-2.png");
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
box-sizing: border-box;
padding: percentage(66px/@image-width) percentage(44px/@image-width) 0 percentage(34px/@image-width);
font-size: 1rem;
line-height: 1.42;
}
索引从1开始
@list-num: "①", "②", "③";
.set-list-num(@begin, @end) when (@begin <= @end) {
.reimburse-tips > div > ol > li:nth-child(@{begin})::before {
content: extract(@list-num, @begin);
}
.set-list-num(@begin + 1, @end);
}
.set-list-num(1, length(@list-num));
import "file.less";
.selector:extend(.parent-selector) {
}
.selector {
&:extend(.parent-selector>);
}
1. media query内的extend操作,近能继承当前块的其他选择器样式;
2. 非media query内的extend操作,将会继承所有media query中匹配的选择器样式;
3. 增强的mixin定义mixin时仅能使用类选择器和ID选择器,而extend操作可对应所有的选择器,因此当没有动态入参而又需要类选择器和ID选择器以外的选择器时,可使用extend来实现mixin的功能。
mixin相当于macro,会将样式规则内联到调用的位置中。而less中的mixin有以下的注意点;
less中通过混合(Mixin)后的when关键字来提供选择的作业控制,when只接受boolean值
1. 类型判断函数
1. 关系运算符: =
>
>=
<
<=
1. 逻辑运算符: and
or
not
.set-html-font-size(@width, @width-step, @font-size, @font-size-step, @max-font-size) when (@font-size <= @max-font-size) {
@media (min-width: @width) {
html {
font-size: @font-size;
}
}
.set-html-font-size(@width + @width-step, @width-step, @font-size + @font-size-step, @font-size-step, @max-font-size);
}
.set-html-font-size(320px, 25px, 10px, 1px, 15px);
less还支持 +
-
*
/
运算符。但对单位不一致的运算数进行运算要注意以下两点:
1. 运算数与运算符间必须用空格分隔;
2. 以第一个运算数的单位作为运算结果的单位;
/
在background中异常
background: url(../assets/fertility-wish/logo.png) no-repeat 0 0 / 100% 100%;
background: url(../assets/fertility-wish/logo.png) no-repeat left top / 100% 100%;
background: url(../assets/fertility-wish/logo.png) no-repeat 0 0;
background-size: 100% 100%;
unit
percentage
image-width
image-height
.logo {
position: absolute;
width: unit(image-width("../assets/fertility-wish/logo.png") / 2 / @base-font-size, rem);
height: unit(image-height("../assets/fertility-wish/logo.png") / 2 / @base-font-size, rem);;
left: 50%;
transform: translate3d(-50%, 0, 0);
bottom: unit(12px / @base-font-size, rem);
background: url(../assets/fertility-wish/logo.png) no-repeat 0 0;
background-size: 100% 100%;
}
.cradle {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background: url(../img/baby.png) no-repeat 0 0;
background-size: 100% 100%;
transform-origin: percentage(376.5px / @base-width) percentage(594px / @base-height);
animation: baby-shake 2.8s ease-in-out infinite;
}