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. vue tab栏缓存解决跳转页面后返回的状态保持

    <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="UTF-8& ...

  2. GPU版TensorFlow怎么指定让CPU运行

    由于某些原因GPU版的TensorFlow运行起来会出现一些问题,比如内存溢出等情况.此时我们可以用CPU和系统内存来运行我们的程序. 代码如下: import osos.environ[" ...

  3. [Err] 1062 - Duplicate entry '0' for key 'PRIMARY'

    问题描述: sql语句执行的时候,插入语句无法正确执行 问题原因: 主键 重复 出现 0 解决方案: 将主键设置为自增 然而,设置自增后还是可能会出现下面的问题 #1062 – Duplicate e ...

  4. DOM事件和一些实用笔记

    let el = document.body.querySelector("style[type='text/css'], style:not([type])");返回HTML文档 ...

  5. HDU 1864 01背包、

    这题题意有点坑阿.感觉特别模糊. 我开始有一点没理解清楚.就是报销的话是整张整张支票报销的.也是我傻逼了 没一点常识 还有一点就是说单张支票总额不超过1000,每张支票中单类总额不超过600,我开始以 ...

  6. 如何在SpringMVC项目中部署WebService服务并打包生成客户端

    场景 某SpringMVC项目原本为一个HTTP的WEB服务项目,之后想在该项目中添加WebService支持,使该项目同时提供HTTP服务和WebService服务.其中WebService服务通过 ...

  7. HDU 1236

    水题~~但我做了很久: 题意:是中国人都懂了 思路:结构体排序: 以后要多用用重定义的排序手段,!!!!!多用!!多用!!多用!! #include<iostream> #include& ...

  8. Codeforces Round #194 (Div.1 + Div. 2)

    A. Candy Bags 总糖果数\(\frac{n^2(n^2+1)}{2}\),所以每人的数量为\(\frac{n}{2}(n^2+1)\) \(n\)是偶数. B. Eight Point S ...

  9. H3C 单路径网络中环路产生过程(1)

  10. P1000 A+B Problem

    题目描述 给定两个整数\(a,b\),输出它们的和. 输入格式 输入两个整数,表示\(a,b(1 \le a,b \le 10^9)\). 输出格式 输出一个整数,表示答案. 样例输入 20 30 样 ...