less没有我们平常使用的if,else条件判断,而是用when来实现这种用法

1.比如我们要设置宽度

宽度可以百分比,也可以是像素,当是百分比时做对应处理,当是px时做另一种处理,这时候就需要用when来实现if-else来实现条件判断

  • less语法实现if-else条件判断
@bs:20rem;//定义变量
//ispercentage是less中提供的方法,判断是不是百分比,返回bool值
.w(@width) when not(ispercentage(@width)){
//当不是百分比时(less中用的是not来表示非)
width:@width/@bs;
}
.w(@width) when (ispercentage(@width)){
//当是百分比时
width:@width;
}
//less代码 测试
.container {
width: 2.5rem;
}
.container-less {
width: 50%;
}
//编译之后的css代码
.container {
width: 2.5rem;
}
.container-less {
width: 50%;
}

2.less有类似于c#语言的特点,方法的重载也就是可以定义多个重名方法,根据传递参数不同来识别对应方法调用

@bs:20rem;
.w(@width) when not(ispercentage(@width)){
width:@width/@bs;
}
.w(@width) when (ispercentage(@width)){
width:@width;
}
.h(@height) when not(ispercentage(@height)){
height: @height/@bs;
}
.h(@height) when (ispercentage(@height)){
height:@height;
}
//定义多个同名方法position
//只设置宽高的pos
.pos(@width;@height){
.w(@width);
.h(@height);
position:absolute;
}
//设置宽高和url的pos
.pos(@width;@height;@url){
.w(@width);
.h(@height);
position:absolute;
background: url("../images/@{url}") no-repeat center/ 100% 100%;
}
//设置宽高,url和背景图片大小的pos
.pos(@width;@height;@url;@pos1;@pos2){
.w(@width);
.h(@height);
position:absolute;
background: url("../images/@{url}") no-repeat center/ @pos1/@bs @pos2/@bs;
}
结果

.con1{
.pos(30;50)
}
.con2{
.pos(30;50;'page1/card1.png');
}
.con3{
.pos(400;50%;'page1/card1.png';400;500)
}

.con1 {
width: 1.5rem;
height: 2.5rem;
position: absolute;
}
.con2 {
width: 1.5rem;
height: 2.5rem;
position: absolute;
background: url("../images/page1/card1.png") no-repeat center / 100% 100%;
}
.con3 {
width: 20rem;
height: 50%;
position: absolute;
background: url("../images/page1/card1.png") no-repeat center / 20rem 25rem;
}

随机推荐

  1. 2019-6-23-修复-dotnet-Core-缺SDK编译失败

    title author date CreateTime categories 修复 dotnet Core 缺SDK编译失败 lindexi 2019-6-23 10:55:9 +0800 2019 ...

  2. 【CSS3 + 原生JS】上升的方块动态背景

    GIF图有点大,网速慢的或将稍等片刻或可浏览本人的制作的demo. Demo : 点击查看 HTML: <!DOCTYPE html> <html lang="en&quo ...

  3. Python--day37--进程锁

    进程锁的示意图: 锁.py: #锁 #火车票 import json import time from multiprocessing import Process from multiprocess ...

  4. H3C FTP被动数据传输方式

  5. linux 共享队列

    一个设备驱动, 在许多情况下, 不需要它自己的工作队列. 如果你只偶尔提交任务给队列, 简单地使用内核提供的共享的, 缺省的队列可能更有效. 如果你使用这个队列, 但是, 你 必须明白你将和别的在共享 ...

  6. linux 使用 jiffies 计数器

    这个计数器和来读取它的实用函数位于 <linux/jiffies.h>, 尽管你会常常只是包含 <linux/sched.h>, 它会自动地将 jiffies.h 拉进来. 不 ...

  7. router-link-active的作用

    如上图所示,创建了3个路由跳转选项,css实现后的效果如下 ↓↓↓ 当我切换“电影” “影院” “我的” 三个路由选项时,文字由黑色变成红色 此时可用vue自带的 router-link-active ...

  8. Linux 内核 kobject 初始化

    本书已经展示了许多数据类型, 带有简单的在编译或者运行时初始化机制. 一个 kobject 的初始化有些复杂, 特别当使用它的所有函数时. 不管一个 kobject 如何使用, 但是, 必须进行几个步 ...

  9. Linux 内核 回顾: ISA

    设计上 ISA 总线非常老了, 并且是非常地低能, 但是它仍然持有一块挺大的控制设备的 市场. 如果速度不重要并且你想支持老式主板, 一个 ISA 实现要优于 PCI. 这个老标准 的另外一个好处是如 ...

  10. ubuntu16.04 无法wifi链接一段时间掉线且无法再连接

    ubuntu16.04 无法wifi链接一段时间掉线且无法再连接,从网上搜索的确认这个一个bug. 解决方法: 1.Get details of your PCI wireless card by r ...