日本好好热aⅴ|国产99视频精品免费观看|日本成人aV在线|久热香蕉国产在线

  • <cite id="ikgdy"><table id="ikgdy"></table></cite>
    1. 西西軟件下載最安全的下載網(wǎng)站、值得信賴的軟件下載站!

      首頁(yè)編程開(kāi)發(fā)其它知識(shí) → LESS 語(yǔ)法學(xué)習(xí)

      LESS 語(yǔ)法學(xué)習(xí)

      相關(guān)軟件相關(guān)文章發(fā)表評(píng)論 來(lái)源:西西整理時(shí)間:2013/1/16 11:01:45字體大。A-A+

      作者:西西點(diǎn)擊:0次評(píng)論:0次標(biāo)簽: LESS

      • 類(lèi)型:壁紙主題大小:3.8M語(yǔ)言:中文 評(píng)分:5.0
      • 標(biāo)簽:
      立即下載

      LESS 做為 CSS 的一種形式的擴(kuò)展,它并沒(méi)有閹割 CSS 的功能,而是在現(xiàn)有的 CSS 語(yǔ)法上,添加了很多額外的功能,所以學(xué)習(xí) LESS 是一件輕而易舉的事情,果斷學(xué)習(xí)之!

      變量

      很容易理解:

      @nice-blue: #5B83AD;
      @light-blue: @nice-blue + #111;
      
      #header { color: @light-blue; }
      

      輸出:

      #header { color: #6c94be; }
      

      甚至可以用變量名定義為變量:

      @fnord: "I am fnord.";
      @var: 'fnord';
      content: @@var;
      

      解析后:

      content: "I am fnord.";
      

      請(qǐng)注意 LESS 中的變量為完全的 ‘常量’ ,所以只能定義一次.

      Less開(kāi)發(fā)工具(WinLess)
      1.4
      類(lèi)別: 編程輔助    大小:4.1M    語(yǔ)言: 英文
      查看詳細(xì)信息 >>

      混合

      在 LESS 中我們可以定義一些通用的屬性集為一個(gè)class,然后在另一個(gè)class中去調(diào)用這些屬性. 下面有這樣一個(gè)class:

      .bordered {
        border-top: dotted 1px black;
        border-bottom: solid 2px black;
      }
      

      那如果我們現(xiàn)在需要在其他class中引入那些通用的屬性集,那么我們只需要在任何class中像下面這樣調(diào)用就可以了:

      #menu a {
        color: #111;
        .bordered;
      }
      .post a {
        color: red;
        .bordered;
      }
      

      .bordered class里面的屬性樣式都會(huì)在 #menu a 和 .post a中體現(xiàn)出來(lái):

      #menu a {
        color: #111;
        border-top: dotted 1px black;
        border-bottom: solid 2px black;
      }
      .post a {
        color: red;
        border-top: dotted 1px black;
        border-bottom: solid 2px black;
      }
      

      任何 CSS class, id 或者 元素 屬性集都可以以同樣的方式引入.

      帶參數(shù)混合

      在 LESS 中,你還可以像函數(shù)一樣定義一個(gè)帶參數(shù)的屬性集合:

      .border-radius (@radius) {
        border-radius: @radius;
        -moz-border-radius: @radius;
        -webkit-border-radius: @radius;
      }
      

      然后在其他class中像這樣調(diào)用它:

      #header {
        .border-radius(4px);
      }
      .button {
        .border-radius(6px);  
      }
      

      我們還可以像這樣給參數(shù)設(shè)置默認(rèn)值:

      .border-radius (@radius: 5px) {
        border-radius: @radius;
        -moz-border-radius: @radius;
        -webkit-border-radius: @radius;
      }
      

      所以現(xiàn)在如果我們像這樣調(diào)用它的話:

      #header {
        .border-radius;  
      }
      

      radius的值就會(huì)是5px.

      你也可以定義不帶參數(shù)屬性集合,如果你想隱藏這個(gè)屬性集合,不讓它暴露到CSS中去,但是你還想在其他的屬性集合中引用,你會(huì)發(fā)現(xiàn)這個(gè)方法非常的好用:

      .wrap () {
        text-wrap: wrap;
        white-space: pre-wrap;
        white-space: -moz-pre-wrap;
        word-wrap: break-word;
      }
      
      pre { .wrap }
      

      輸出:

      pre {
        text-wrap: wrap;
        white-space: pre-wrap;
        white-space: -moz-pre-wrap;
        word-wrap: break-word;
      }
      

      @arguments 變量

      @arguments包含了所有傳遞進(jìn)來(lái)的參數(shù). 如果你不想單獨(dú)處理每一個(gè)參數(shù)的話就可以像這樣寫(xiě):

      .box-shadow (@x: 0, @y: 0, @blur: 1px, @color: #000) {
        box-shadow: @arguments;
        -moz-box-shadow: @arguments;
        -webkit-box-shadow: @arguments;
      }
      .box-shadow(2px, 5px);
      

      將會(huì)輸出:

        box-shadow: 2px 5px 1px #000;
        -moz-box-shadow: 2px 5px 1px #000;
        -webkit-box-shadow: 2px 5px 1px #000;
      

      模式匹配和導(dǎo)引表達(dá)式

      有些情況下,我們想根據(jù)傳入的參數(shù)來(lái)改變混合的默認(rèn)呈現(xiàn),比如下面這個(gè)例子:

      .mixin (@s, @color) { ... }
      
      .class {
        .mixin(@switch, #888);
      }
      

      如果想讓.mixin根據(jù)不同的@switch值而表現(xiàn)各異,如下這般設(shè)置:

      .mixin (dark, @color) {
        color: darken(@color, 10%);
      }
      .mixin (light, @color) {
        color: lighten(@color, 10%);
      }
      .mixin (@_, @color) {
        display: block;
      }
      

      現(xiàn)在,如果運(yùn)行:

      @switch: light;
      
      .class {
        .mixin(@switch, #888);
      }
      

      就會(huì)得到下列CSS:

      .class {
        color: #a2a2a2;
        display: block;
      }
      

      如上,.mixin就會(huì)得到傳入顏色的淺色。如果@switch設(shè)為dark,就會(huì)得到深色。

      具體實(shí)現(xiàn)如下:

      第一個(gè)混合定義并未被匹配,因?yàn)樗唤邮躣ark做為首參

      第二個(gè)混合定義被成功匹配,因?yàn)樗唤邮躭ight

      第三個(gè)混合定義被成功匹配,因?yàn)樗邮苋我庵?/p>

      只有被匹配的混合才會(huì)被使用。變量可以匹配任意的傳入值,而變量以外的固定值就僅僅匹配與其相等的傳入值。

      我們也可以匹配多個(gè)參數(shù):

      .mixin (@a) {
        color: @a;
      }
      .mixin (@a, @b) {
        color: fade(@a, @b);
      }
      

      Now if we call .mixin with a single argument, we will get the output of the first definition, but if we call it withtwo arguments, we will get the second definition, namely @a faded to @b.

      引導(dǎo)

      當(dāng)我們想根據(jù)表達(dá)式進(jìn)行匹配,而非根據(jù)值和參數(shù)匹配時(shí),導(dǎo)引就顯得非常有用。如果你對(duì)函數(shù)式編程非常熟悉,那么你很可能已經(jīng)使用過(guò)導(dǎo)引。

      為了盡可能地保留CSS的可聲明性,LESS通過(guò)導(dǎo)引混合而非if/else語(yǔ)句來(lái)實(shí)現(xiàn)條件判斷,因?yàn)榍罢咭言贎media query特性中被定義。

      以此例做為開(kāi)始:

      .mixin (@a) when (lightness(@a) >= 50%) {
        background-color: black;
      }
      .mixin (@a) when (lightness(@a) < 50%) {
        background-color: white;
      }
      .mixin (@a) {
        color: @a;
      }
      

      when關(guān)鍵字用以定義一個(gè)導(dǎo)引序列(此例只有一個(gè)導(dǎo)引)。接下來(lái)我們運(yùn)行下列代碼:

      .class1 { .mixin(#ddd) }
      .class2 { .mixin(#555) }
      

      就會(huì)得到:

      .class1 {
        background-color: black;
        color: #ddd;
      }
      .class2 {
        background-color: white;
        color: #555;
      }
      

      導(dǎo)引中可用的全部比較運(yùn)算有: > >= = =< <。此外,關(guān)鍵字true只表示布爾真值,下面兩個(gè)混合是相同的:

      .truth (@a) when (@a) { ... }
      .truth (@a) when (@a = true) { ... }
      

      除去關(guān)鍵字true以外的值都被視示布爾假:

      .class {
        .truth(40); // Will not match any of the above definitions.
      }
      

      導(dǎo)引序列使用逗號(hào)‘,’—分割,當(dāng)且僅當(dāng)所有條件都符合時(shí),才會(huì)被視為匹配成功。

      .mixin (@a) when (@a > 10), (@a < -10) { ... }
      

      導(dǎo)引可以無(wú)參數(shù),也可以對(duì)參數(shù)進(jìn)行比較運(yùn)算:

      @media: mobile;
      
      .mixin (@a) when (@media = mobile) { ... }
      .mixin (@a) when (@media = desktop) { ... }
      
      .max (@a, @b) when (@a > @b) { width: @a }
      .max (@a, @b) when (@a < @b) { width: @b }
      

      最后,如果想基于值的類(lèi)型進(jìn)行匹配,我們就可以使用is*函式:

      .mixin (@a, @b: 0) when (isnumber(@b)) { ... }
      .mixin (@a, @b: black) when (iscolor(@b)) { ... }
      

      下面就是常見(jiàn)的檢測(cè)函式:

      iscolor

      isnumber

      isstring

      iskeyword

      isurl

      如果你想判斷一個(gè)值是純數(shù)字,還是某個(gè)單位量,可以使用下列函式:

      ispixel

      ispercentage

      isem

      最后再補(bǔ)充一點(diǎn),在導(dǎo)引序列中可以使用and關(guān)鍵字實(shí)現(xiàn)與條件:

      .mixin (@a) when (isnumber(@a)) and (@a > 0) { ... }
      

      使用not關(guān)鍵字實(shí)現(xiàn)或條件

      .mixin (@b) when not (@b > 0) { ... }
      

      嵌套規(guī)則

      LESS 可以讓我們以嵌套的方式編寫(xiě)層疊樣式. 讓我們先看下下面這段 CSS:

      #header { color: black; }
      #header .navigation {
        font-size: 12px;
      }
      #header .logo { 
        width: 300px; 
      }
      #header .logo:hover {
        text-decoration: none;
      }
      

      在 LESS 中, 我們就可以這樣寫(xiě):

      #header {
        color: black;
      
        .navigation {
          font-size: 12px;
        }
        .logo {
          width: 300px;
          &:hover { text-decoration: none }
        }
      }
      

      或者這樣寫(xiě):

      #header        { color: black;
        .navigation  { font-size: 12px }
        .logo        { width: 300px;
          &:hover    { text-decoration: none }
        }
      }
      

      代碼更簡(jiǎn)潔了,而且感覺(jué)跟DOM結(jié)構(gòu)格式有點(diǎn)像.

      注意 & 符號(hào)的使用—如果你想寫(xiě)串聯(lián)選擇器,而不是寫(xiě)后代選擇器,就可以用到&了. 這點(diǎn)對(duì)偽類(lèi)尤其有用如:hover 和 :focus.

      例如:

      .bordered {
        &.float {
          float: left; 
        }
        .top {
          margin: 5px; 
        }
      }
      

      會(huì)輸出

      .bordered.float {
        float: left;  
      }
      .bordered .top {
        margin: 5px;
      }
      

      運(yùn)算

      任何數(shù)字、顏色或者變量都可以參與運(yùn)算. 來(lái)看一組例子:

      @base: 5%;
      @filler: @base * 2;
      @other: @base + @filler;
      
      color: #888 / 4;
      background-color: @base-color + #111;
      height: 100% / 2 + @filler;
      

      LESS 的運(yùn)算已經(jīng)超出了我們的期望,它能夠分辨出顏色和單位。如果像下面這樣單位運(yùn)算的話:

      @var: 1px + 5;
      

      LESS 會(huì)輸出 6px.

      括號(hào)也同樣允許使用:

      width: (@var + 5) * 2;
      

      并且可以在復(fù)合屬性中進(jìn)行運(yùn)算:

      border: (@width * 2) solid black;
      

      Color 函數(shù)

      LESS 提供了一系列的顏色運(yùn)算函數(shù). 顏色會(huì)先被轉(zhuǎn)化成 HSL 色彩空間, 然后在通道級(jí)別操作:

      lighten(@color, 10%);     // return a color which is 10% *lighter* than @color
      darken(@color, 10%);      // return a color which is 10% *darker* than @color
      
      saturate(@color, 10%);    // return a color 10% *more* saturated than @color
      desaturate(@color, 10%);  // return a color 10% *less* saturated than @color
      
      fadein(@color, 10%);      // return a color 10% *less* transparent than @color
      fadeout(@color, 10%);     // return a color 10% *more* transparent than @color
      fade(@color, 50%);        // return @color with 50% transparency
      
      spin(@color, 10);         // return a color with a 10 degree larger in hue than @color
      spin(@color, -10);        // return a color with a 10 degree smaller hue than @color
      
      mix(@color1, @color2);    // return a mix of @color1 and @color2
      

      使用起來(lái)相當(dāng)簡(jiǎn)單:

      @base: #f04615;
      
      .class {
        color: saturate(@base, 5%);
        background-color: lighten(spin(@base, 8), 25%);
      }
      

      你還可以提取顏色信息:

      hue(@color);        // returns the `hue` channel of @color
      saturation(@color); // returns the `saturation` channel of @color
      lightness(@color);  // returns the 'lightness' channel of @color
      

      如果你想在一種顏色的通道上創(chuàng)建另一種顏色,這些函數(shù)就顯得那么的好用,例如:

      @new: hsl(hue(@old), 45%, 90%);
      

      @new 將會(huì)保持 @old的 色調(diào), 但是具有不同的飽和度和亮度.

      Math 函數(shù)

      LESS提供了一組方便的數(shù)學(xué)函數(shù),你可以使用它們處理一些數(shù)字類(lèi)型的值:

      round(1.67); // returns `2`
      ceil(2.4);   // returns `3`
      floor(2.6);  // returns `2`
      

      如果你想將一個(gè)值轉(zhuǎn)化為百分比,你可以使用percentage 函數(shù):

      percentage(0.5); // returns `50%`
      

      命名空間

      有時(shí)候,你可能為了更好組織CSS或者單純是為了更好的封裝,將一些變量或者混合模塊打包起來(lái), 你可以像下面這樣在#bundle中定義一些屬性集之后可以重復(fù)使用:

      #bundle {
        .button () {
          display: block;
          border: 1px solid black;
          background-color: grey;
          &:hover { background-color: white }
        }
        .tab { ... }
        .citation { ... }
      }
      

      你只需要在 #header a中像這樣引入 .button:

      #header a {
        color: orange;
        #bundle > .button;
      }
      

      作用域

      LESS 中的作用域跟其他編程語(yǔ)言非常類(lèi)似,首先會(huì)從本地查找變量或者混合模塊,如果沒(méi)找到的話會(huì)去父級(jí)作用域中查找,直到找到為止.

      @var: red;
      
      #page {
        @var: white;
        #header {
          color: @var; // white
        }
      }
      
      #footer {
        color: @var; // red  
      }
      

      注釋

      CSS 形式的注釋在 LESS 中是依然保留的:

      /* Hello, I'm a CSS-style comment */
      .class { color: black }
      

      LESS 同樣也支持雙斜線的注釋, 但是編譯成 CSS 的時(shí)候自動(dòng)過(guò)濾掉:

      // Hi, I'm a silent comment, I won't show up in your CSS
      .class { color: white }
      

      Importing

      你可以在main文件中通過(guò)下面的形勢(shì)引入 .less 文件, .less 后綴可帶可不帶:

      @import "lib.less";
      @import "lib";
      

      如果你想導(dǎo)入一個(gè)CSS文件而且不想LESS對(duì)它進(jìn)行處理,只需要使用.css后綴就可以:

      @import "lib.css";
      

      這樣LESS就會(huì)跳過(guò)它不去處理它.

      字符串插值

      變量可以用類(lèi)似ruby和php的方式嵌入到字符串中,像@{name}這樣的結(jié)構(gòu):

      @base-url: "http://assets.fnord.com";
      background-image: url("@{base-url}/images/bg.png");
      

      避免編譯

      有時(shí)候我們需要輸出一些不正確的CSS語(yǔ)法或者使用一些 LESS不認(rèn)識(shí)的專(zhuān)有語(yǔ)法.

      要輸出這樣的值我們可以在字符串前加上一個(gè) ~, 例如:

      .class {
        filter: ~"ms:alwaysHasItsOwnSyntax.For.Stuff()";
      }
      

      我們可以將要避免編譯的值用 “”包含起來(lái),輸出結(jié)果為:

      .class {
        filter: ms:alwaysHasItsOwnSyntax.For.Stuff();
      }
      

      JavaScript 表達(dá)式

      JavaScript 表達(dá)式也可以在.less 文件中使用. 可以通過(guò)反引號(hào)的方式使用:

      @var: `"hello".toUpperCase() + '!'`;
      

      輸出:

      @var: "HELLO!";
      

      注意你也可以同時(shí)使用字符串插值和避免編譯:

      @str: "hello";
      @var: ~`"@{str}".toUpperCase() + '!'`;
      

      輸出:

      @var: HELLO!;
      

      它也可以訪問(wèn)JavaScript環(huán)境:

      @height: `document.body.clientHeight`;
      

      如果你想將一個(gè)JavaScript字符串解析成16進(jìn)制的顏色值, 你可以使用 color 函數(shù):

      @color: color(`window.colors.baseColor`);
      @darkcolor: darken(@color, 10%);

        相關(guān)評(píng)論

        閱讀本文后您有什么感想? 已有人給出評(píng)價(jià)!

        • 8 喜歡喜歡
        • 3 頂
        • 1 難過(guò)難過(guò)
        • 5 囧
        • 3 圍觀圍觀
        • 2 無(wú)聊無(wú)聊

        熱門(mén)評(píng)論

        最新評(píng)論

        發(fā)表評(píng)論 查看所有評(píng)論(0)

        昵稱:
        表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
        字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)
        推薦文章

        沒(méi)有數(shù)據(jù)