less学习二---变量

less中声明的变量可以存储css属性值,还可以存储选择器,属性名,url以及@imporant等

变量声明及赋值格式:@variableName : varableValue ;

//属性值

//less
//变量
@pink:pink;
.content{
    color:@pink;
}

编译成

.content{
    color:#ffc0cb;//pink
}

//选择器

@selector:content;
.@{selector}{
    color:pink;
}
//或者
@sector:.content;
@{selector}{
  color:pink;
}
//编译后都是 .content{ color:pink; }

一定要把定义的选择器变量名放在{}里面,并在花括号的前面加@

//url

@img:"../img/";
.content{
    backgrond:url("@{img}/sea.jpg");
}

//编译后
.content{
    background:url("../img/sea.jpg");
}

在定义url变量时,注意将路径用引号扩起来;

//属性名

@property:color;
.content{
    background-@{property}:green;
    a{
        @{property}:white;
    }
}

//编译后
.content{
    background-color:#00ff00;
}
.content a{
    color:white;
}

在less中可以用一个变量来定义另一个变量

@primary:red;
.content{
    @color:primary;
    background-color:@@color;
}
//或者
.content{
    @color:@primary;
    background-color:@color;
}

//编译后都是
.content{
    background-color:red;
}

less 中的懒加载

即我们可以不需要在使用变量之前使用这个变量

@h:300px;
.content{
    width:@var;
    height:@h;
}
@var:9%;

//编译后
.content{
  width:9%;
  height:300px;
}

当我们重复定义多个变量时,以当前作用域中最后定义的为准,若当前作用域没有定义,则逐级向上寻找;

@var: 0;
.class {
  @var: 1;
  .brass {
    @var: 2;
    three: @var;
    @var: 3;
  }
  one: @var;
}

//编译后
.class {
  one: 1;
}
.class .brass {
  three: 3;
}

在3.0版本中最新的利用$propertyName来获取属性值,有时候利用好了可以使代码看起来更简便

.widget {
  color: #efefef;
  background-color: $color;
}
//编译后
.widget {
  color: #efefef;
  background-color: #efefef;
}

 

转载于:https://www.cnblogs.com/aniu-caili/p/9517236.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值