less和scss

一、less基础语法

1、声明变量:@变量名:变量值;
      使用变量:@变量名;

2、混合(Mixins)
1)无参混合
声明: .class{}
调用:在选择器中,使用.class;直接调用

2)有参无默认值混合:
声明:.class(@param){}
调用:.class(paramValue);
3)有参有默认值混合:
声明:.class(@param:10px){}
调用:.class(paramValue);

>>>如果声明时,没有给参数赋默认值,则调用时,必须赋值,否则报错
>>>无参混合,实际上就是一个普通的class选择器,会被编译到CSS文件中;
而有参混合,在编译时,不会出现在CSS文件中。

@color:#ff0000;//声明变量
@length:100px;//声明变量
#div1{
    width: 100px;
    height:@length;//使用变量
    background-color: @color;
}
//无参混合
.borderRadius{
    border-radius: 10px;
    -webkit-border-radius: 10px;
    -moz-border-radius: 10px;    
}
//有参无默认值混合
.borderRadius1(@radius){
    border-radius: @radius;
    -webkit-border-radius: @radius;
    -moz-border-radius: @radius;    
}
//有参有默认值混合
.borderRadius2(@radius:10px){
    border-radius: @radius;
    -webkit-border-radius: @radius;
    -moz-border-radius: @radius;    
}
.class(){
    width: 10px;
    height: 10px;
    .borderRadius2();
}

【变量使用原则】:
多次频繁出现的值,后期需要统一修改的值,牵扯到数值运算的值,推荐使用变量;
【less中的变量类型】
在CSS中出现的属性值,在less中都可以用作变量名
1)数值类:不带单位的 123 带单位的 1px
2)字符串:带引号的 "hahhaha" 不带引号的 red #fffff
3)颜色类:red #ff0000 RGB(255,0,0)
4)值列表类型:多个值用逗号或空格分隔 10px solid red

3、less中的匹配模式
1)声明:
@pipei(@条件1,参数){}

@pipei(@条件2,参数){}

@pipei(@_,参数){}
2)调用:
@pipei(条件的值,参数的值){}
3)匹配规则
根据调用时输入的条件值,去寻找与之匹配的混合执行。
@_表示不管匹配成功与否,都会执行的选项

/*匹配模式*/
.pipei(lefts,@width:10px){
    margin-left: @width;  
}
.pipei(rights,@width:10px){
    margin-right: @width;   
}
.pipei(tops,@width:10px){
    margin-top: @width;  
}
.pipei(bottoms,@width:10px){
    margin-bottom: @width;  
}
.pipei(@_,@width:10px){//@_表示不管匹配成功与否,都会执行的选项
    padding: 10px;
}

@postion:lefts;
.class1{
    .pipei(@postion,20px); //调用:@pipei(条件的值,参数的值){}
}

4、@arguments 特殊变量:
在混合中,@arguments表示混合传入的所有参数。@arguments中的多个参数用空格分隔。
.border(@width,@style,@color){
border:@arguments;//-->border:@width @style @color;
}

.argu(@width,@style,@color){
//  border:@width @style @color;
    border: @arguments;
}
.class2{
    .argu(10px,solid,red);  //调用:分别传入三个参数的值
}

5、LESS中的加减乘除运算:
LESS中的所有变量和数值,可以进行+ - * /运算。
需要注意的是,当颜色值运算时,红绿蓝分三组运算。组内单独计算,组间互不干扰。
6、LESS中的嵌套
LESS中允许CSS选择器按照HTML代码的结构进行嵌套。
1)less中的嵌套默认是后代选择器,如果需要子代选择器,需要在前面加>
2)&符号,表示这个&所在的上一层选择器。比如上述&,表示 section ul:hover

/*
 * less中的嵌套
 */
#section{
    width: 800px;
    height: 200px;
    background-color: #ccc;
    >p{//>表示子代选择器
        color: red;
        font-weight: bold;//加粗
    }
    ul{
        padding: 0;
        list-style: none;
        >li{
            float: left;
            width: 100px;
            height: 50px;
            background-color: yellow;
            text-align: center;
            &:hover{//&表示上一层选择器(#section ul li:hover)
                background-color: green;
            }
    }
    }
}

二、sacc基础语法

1、scss中的变量
 1)声明变量:$变量名:变量值;
 SCSS中,允许将变量嵌套在字符串中,但是变量必须使用#{}包裹
 eg:border-#{$left}:10px solid red;

$width:100px;//scss中的声明变量
$position:left;
#div1{
    width: $width;
    height: $width/10;
    background-color: red;
    border-#{$position}:10px solid yellow;
}

2、SCSS中的运算,会将单位进行运算。使用时需注意最终的单位是否正确。
 eg:10px * 10px=100 px*px;
 3、SCSS中的嵌套:选择器嵌套、属性嵌套、伪类嵌套
1)选择器嵌套ul{ li{} }
 嵌套默认表示后代选择器,如果需要子代选择器,可以在选择器前面加>
可以在选择器的大括号中,使用&表示上一层的选择器。
 2)伪类嵌套: li{ $:hover}
在选择器的{}中,使用&配合伪类事件,可以表示当前选择器的伪类
3)属性嵌套:font:{size:18px} -->font-size:18px;
对于属性名有-分割为多段的属性,可以使用属性嵌套。属性名的前半部分必须紧跟:,才能
用{}包裹属性的后半部分。

section{
    background-color: #CCCCCC;
    p{
        color: red;
    }
    ul{
        padding: 0;
        li{
            list-style: none;
            //$=="section ul li"
            font:{
                size: 16px;
                weight:bold;
                family:"微软雅黑";
                style:"italic"//字体为斜体
            }
        }
    }
}

4、混合宏、继承、占位符
 1)混合宏:声明@mixin声明混合宏,在其他选择器中使用@include调用混合宏
 @minxin hunhe{} .class{@include hunhe}
 @minxin hunhe(@param){} .class{@include hunhe(value)}
 @minxin hunhe(@param:value){} .class{@include hunhe()}

//继承
.class1{
    color:red;
    }
.class{
    @extend .class1;
    background-color: yellow;
}
//混合宏
@mixin hunhe($color:red){
    color: $color;
}
.class3{
    @include hunhe(green);
    background-color: yellow;
}
.class4{
    @include hunhe;
    background-color: blue;
}
//占位符
%class1{
    color: red;
}
.class4{
    @extend %class1;
    background-color: yellow;
}
.class5{
    @extend %class1;
    background-color: green;
}

 1)混合宏:声明时,可以有参数,也可以没有参数。参数可以有默认值,也可以没有默认值。
 但是调用时,必须符合声明时的要求。与LESS中的混合相同。
 >>>优点:1)可以传参

                   2)不会产生同名class
 >>>缺点:调用时,会把混合宏中的所有代码copy到选择器中,产生大量重复代码

 2)继承:声明一个普通的class,在其他选择器中使用@extend 继承这个class
 .class1{} .class1{ @extend .class1;}
 >>>优点:将相同代码提取到并集选择其中,减少冗余代码;
 >>>缺点:1)不能传参

                   2)会生出一个多于的class
 3)占位符:使用%声明占位符,在其他选择器中使用@extend继承占位符;
 %class1{} .class2{@extend %class1;}
 >>>优点:1)将相同代码提取到并集选择其中,减少冗余代码;
                   2)不会生出一个多于的class。
 >>>缺点:不能传参

 

5、if条件结构
 条件结构需要写在选择器里面,条件结构的大括号直接包裹样式属性。
 @if 条件{}
 @else{}

//scss中的条件语句
.class6{
  width: 100px;
  height: 100px;
  @if 1>2{
      background-color: yellow;//设置背景色
  }@else{
      background-color: green;
  }
}

6、for循坏:
 @for $i from 1 to 10{} //不包含10
 @for $i from 1 through 10{} //包含10

//for循坏
@for $i 1 through 10{
    .border-#{$i}{
        border: #{$i}px solid red;//设置外边框
    }
} 

7、while循坏:
 $i:@extend;
 @while $i<10{
 $i:$i+1;
 }

//while循坏
$i:0;
@while $i<10{
    .while-#{$i}{
        border: #{$i}px solid red;
    }
    $i:$i+1;
}

 8、each 循坏遍历
 @each $item in a,b,c,d{
 //$item表示a,b,c,d的每一项
 }

 

//each 循坏遍历
$i:0;
@each $item in c1,c2,c3,c4{
    $i:$i+1;
    .#{$item}{
        border: #{$i}px solid red;
    }
}

 

posted @ 2017-09-17 22:02  姐姐jy  阅读(952)  评论(0编辑  收藏  举报