less語法之--匹配模式

四. less下的匹配模式(類似于混入)

它相當(dāng)于但又不完全是JS中的if,只有滿足條件后才能匹配

一句話總結(jié)匹配模式:
相當(dāng)于我傳入一個(gè)值,這個(gè)值會(huì)去匹配與它對(duì)應(yīng)的樣式;


4.1 首先先舉個(gè)簡(jiǎn)單的栗子1: 匹配浮動(dòng)方向

  • 定義一些浮動(dòng)方向的樣式:
    // 傳參 r ,對(duì)應(yīng) 右浮 ;
    .flo(r){
        float: right;
    }
     // 傳參 l ,對(duì)應(yīng)匹配 左浮動(dòng);
    .flo(l){
        float: left;
    }

舉個(gè)栗子--》匹配:

  • 混入的時(shí)候,會(huì)根據(jù)傳入的參數(shù)想上面 剛剛定義的浮動(dòng)樣式的參數(shù)進(jìn)行參數(shù)匹配,匹配到哪個(gè),就使用那個(gè)對(duì)應(yīng)的浮動(dòng)方向的樣式
.box_float_test1 {
        width: 200px;
        height: 200px;
        background: green;
        
        .flo(l);
    }

.box_float_test2 {
        width: 300px;
        height: 200px;
        background: green;
        
        .flo(r);
    }

輸出樣式:

.box_float_test1 {
        width: 200px;
        height: 200px;
        background: green;
        
        float: left;
    }

.box_float_test2 {
        width: 300px;
        height: 200px;
        background: green;
        
        float: right;
    }

4.2 不行的話在舉個(gè)栗子2: 匹配定位

    // 傳參r,對(duì)應(yīng)匹配到相對(duì)定位;
    .pos(r){
        position: relative;
    }
     // 傳參a,對(duì)應(yīng)匹配到絕對(duì)定位;
    .pos(a){
        position: absolute;
    }
     // 傳參f,對(duì)應(yīng)匹配到固定定位;
    .pos(f){
        position: fixed;
    }

舉例匹配:

.box_postition_test1 {
        width: 200px;
        height: 200px;
        background: pink;
        
        .pos(f);
    }

.box_postition_test2 {
        width: 300px;
        height: 200px;
        background: green;
        
        .pos(r);
    }

輸出樣式:

.box_postition_test1 {
        width: 200px;
        height: 200px;
        background: pink;
        
         position: fixed;
    }

.box_postition_test2 {
        width: 300px;
        height: 200px;
        background: green;
        
        position: relative;
    }

你也可以在瀏覽器上選中元素,看它對(duì)應(yīng)的樣式, 以及 定位方式是否一致!

4.3 來個(gè)復(fù)雜點(diǎn)的,使用less匹配三角形,請(qǐng)看下一篇

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容