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. H3C DCC拨号配置任务

  2. 使用基于Apache Spark的随机森林方法预测贷款风险

    使用基于Apache Spark的随机森林方法预测贷款风险   原文:Predicting Loan Credit Risk using Apache Spark Machine Learning R ...

  3. Java如何计算hashcode值

    在设计一个类的时候,很可能需要重写类的hashCode()方法,此外,在集合HashSet的使用上,我们也需要重写hashCode方法来判断集合元素是否相等. 下面给出重写hashCode()方法的基 ...

  4. SQL 三个表练习(student,teacher,score)

  5. H3C 基本ACL部署位置示例

  6. P1088 上台阶

    题目描述 楼梯有 \(n(1 \le n \le 50)\) 阶台阶,上楼时可以一步上 \(1\) 阶,也可以一步上 \(2\) 阶,也可以一步上 \(3\) 阶,编程计算共有多少种不同的走法. 输入 ...

  7. P1023 活动安排

    题目描述 某个人可以在n个活动中选择一些出来参加.每个活动都有起止时间.而且每个时间段只能参加一个活动.问,这个人最多能加参加几个活动. 可以在活动结束时,立即开始新的活动. 输入格式 第一行是一个整 ...

  8. react + webpack 多页面搭建

    一.利用 creat-react-app 新建一个react单页面应用. cnpm i -g create-react-app create-react-app demo cd demo npm st ...

  9. ZR普转提2

    ZR普转提2 A 谢谢刁神教我A题 刚开始读错题了,以为是一个不可做的数位DP,然后就暴力滚粗 直到问了问刁神,发现自己题意是错的 然后成了比较简单的题目 直接暴力枚举每一位填什么,剩下的位数的数字都 ...

  10. dotnet 数组自动转基类数组提示 Co-variant array conversion 是什么问题

    在 C# 的语法,可以提供自动将某个类的数组自动转这个类的基类数组的方法,但是这样的转换在 Resharper 会提示 Co-variant array conversion 这是什么问题? 在 C# ...