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. fakeroot与sudo的区别

    fakeroot 可以用来模拟 root 权限,以便建立特定权限与档案拥有者的压缩文件案(tar, ar, .deb 等).透过 LD_PRELOAD 的 dynamic loader 功能,用户不必 ...

  2. 嵌套app ,的h5分支 项目 (分享,保存图片,返回app)

    function callAppBridge(func, argObj) {   const app = window.webkit || window.app;   const args = typ ...

  3. 最全Pycharm教程(43)——Pycharm扩展功能之UML类图使用 代码结构

    版权声明:本文为博主原创文章,转载时麻烦注明源文章链接,谢谢合作 https://blog.csdn.net/u013088062/article/details/50353202 1.什么是UML ...

  4. codeforces 1230 div2

    C 给一个图,并且在边上放上多米诺骨牌,使得每个多米诺骨牌指向的顶点的数字是一致的,并且每种骨牌只能用一种.问最多能够覆盖多少条边. 先生成每一个点指向的数字,然后判断就好了. #include< ...

  5. JS正则验证两位小数,验证数字最简单正则表达式大全

    <h3>输入完按回车后即可验证!</h3> 正整数: <input type="text" size="20" onkeydown ...

  6. PHP 面试题三

    1.nginx使用哪种网络协议? nginx是应用层 我觉得从下往上的话 传输层用的是tcp/ip 应用层用的是http fastcgi负责调度进程 2. <? echo 'hello tush ...

  7. HDU 1568

    - - 我自己开始以为是数值范围是1到100000000.... 搞了半天才发现是斐波那契数列的项数1到100000000 坑爹.!! 不会,只能看网上大牛的题解. 具体解释请看:http://www ...

  8. solaris 10 基本使用

    1. 新增用户 注释掉home行# cat /etc/auto_master # # Copyright 2003 Sun Microsystems, Inc. All rights reserved ...

  9. echarts实现多条可拖动节点的折现图

    这篇博文主要是利用echarts实现两条以及多条可拖动节点的曲线,demo脱胎于官方demo,在官方demo的基础上添加了另一条曲线.因为之前写过一篇在vue中使用echarts实现可拖动节点的折线图 ...

  10. jQuery 工具类函数-使用$.extend()扩展Object对象

    除使用$.extend扩展工具函数外,还可以扩展原有的Object对象,在扩展对象时,两个对象将进行合并,当存在相同属性名时,后者将覆盖前者,调用格式为: $. extend (obj1,obj2,… ...