国产成人精品999视频&日本一区二区亚洲人妻精品&久久久精品国产99久久精&99热这里只有成人精品国产&精品国产剧情av一区二区&成人亚洲精品久久久久app&国产精品美女高潮抽搐A片

Less 備忘清單

NPM version Downloads Repo Dependents Github repo

本備忘單旨在快速理解 Less 所涉及的主要概念,顯示了它的常用方法使用清單

入門(mén)

介紹

Less(Leaner Style Sheets 的縮寫(xiě))是一門(mén)向后兼容的 CSS 擴(kuò)展語(yǔ)言

在 Node.js 環(huán)境中使用 Less

$ npm install -g less
$ lessc styles.less styles.css

在瀏覽器環(huán)境中使用 Less

<link rel="stylesheet/less" type="text/css" href="styles.less" />
<script src="https://cdn.jsdelivr.net/npm/less@4" ></script>

變量(Variables)

@width: 10px;
@height: @width + 10px;

#header {
  width: @width;
  height: @height;
}

編譯 css 為:

#header {
  width: 10px;
  height: 20px;
}

另見(jiàn): 變量的更多信息

混合(Mixins)

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

#menu a {
  color: #111;
  .bordered();
}

.post a {
  color: red;
  .bordered();
}

另見(jiàn): 混合(Mixin)的更多信息

嵌套(Nesting)

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

???? 更改為 less 的寫(xiě)法 ? ????

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

父選擇器 &

.button {
  color: blue;
  &-ok {
    background-image: url("ok.png");
  }
  &:hover {
    color: green;
  }
}

編譯 css 為:

.button {
  color: blue;
}
.button-ok {
  background-image: url("ok.png");
}
.button:hover {
  color: green;
}

@規(guī)則嵌套和冒泡

.component {
  width: 300px;
  @media (min-width: 768px) {
    width: 600px;
    @media  (min-resolution: 192dpi) {
      background-image: url(/img/icon2x.png);
    }
  }
  @media (min-width: 1280px) {
    width: 800px;
  }
}

編譯 css 為:

.component {
  width: 300px;
}
@media (min-width: 768px) {
  .component {
    width: 600px;
  }
}
@media (min-width: 768px) and (min-resolution: 192dpi) {
  .component {
    background-image: url(/img/icon2x.png);
  }
}
@media (min-width: 1280px) {
  .component {
    width: 800px;
  }
}

運(yùn)算(Operations)

算術(shù)運(yùn)算符 +、-、*/ 對(duì)任何數(shù)字、顏色或變量進(jìn)行運(yùn)算

@conversion-1: 5cm + 10mm; // 結(jié)果是 6cm
@conversion-2: 2 - 3cm - 5mm; // 結(jié)果 -1.5cm
@incompatible-units: 2 + 5px - 3cm;
// 結(jié)果是 4px
@base: 5%;
@filler: @base * 2; // 結(jié)果是 10%
@other: @base + @filler; // 結(jié)果是 15%

@base: 2cm * 3mm; // 結(jié)果是 6cm
@color: (#224488 / 2); // 結(jié)果是 #112244
background-color: #112244 + #111;
// 結(jié)果是 #223355
@color: #222 / 2;
// 結(jié)果是 `#222 / 2`, not #111
background-color: (#FFFFFF / 16);
// 結(jié)果是 #101010

calc() 特例

為了與 CSS 保持兼容,calc() 并不對(duì)數(shù)學(xué)表達(dá)式進(jìn)行計(jì)算,但是在嵌套函數(shù)中會(huì)計(jì)算變量和數(shù)學(xué)公式的值

@var: 50vh/2;
width: calc(50% + (@var - 20px));
// 結(jié)果是 calc(50% + (25vh - 20px))

轉(zhuǎn)義(Escaping)

@min768: ~"(min-width: 768px)";
.element {
  @media @min768 {
    font-size: 1.2rem;
  }
}

編譯 css 為:

@media (min-width: 768px) {
  .element {
    font-size: 1.2rem;
  }
}

從 Less 3.5 開(kāi)始,可以簡(jiǎn)寫(xiě)為

@min768: (min-width: 768px);
.element {
  @media @min768 {
    font-size: 1.2rem;
  }
}

在 Less 3.5+ 版本中,許多以前需要“引號(hào)轉(zhuǎn)義”的情況就不再需要了

函數(shù)(Functions)

@base: #f04615;
@width: 0.5;

.class {
  width: percentage(@width); // 返回 `50%`
  color: saturate(@base, 5%);
  background-color: 
      spin(lighten(@base, 25%), 8);
}

Less 內(nèi)置了多種函數(shù)用于轉(zhuǎn)換顏色、處理字符串、算術(shù)運(yùn)算等

命名空間和訪問(wèn)符

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

.button 類(lèi)混合到 #header a 中,我們可以這樣做

#header a {
  color: orange;
  #bundle.button();
  // 還可以書(shū)寫(xiě)為 #bundle > .button 形式
}

映射(Maps)

#colors() {
  primary: blue;
  secondary: green;
}

.button {
  color: #colors[primary];
  border: 1px solid #colors[secondary];
}

輸出符合預(yù)期(css):

.button {
  color: blue;
  border: 1px solid green;
}

另見(jiàn):映射(Maps)

作用域(Scope)

@var: red;

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

和上面實(shí)例代碼相同

@var: red;

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

另見(jiàn):懶加載

注釋?zhuān)–omments)

/* 一個(gè)塊注釋
 * style comment! */
@var: red;

// 這一行被注釋掉了!
@var: white;

塊注釋和行注釋都可以使用

導(dǎo)入(Importing)

@import "library"; // library.less
@import "typo.css";

另見(jiàn):導(dǎo)入(Importing)的知識(shí)

Extend

nav ul {
  &:extend(.inline);
  background: blue;
}
.inline {
  color: red;
}

編譯 css 為:

nav ul {
  background: blue;
}
.inline,
nav ul {
  color: red;
}

函數(shù)

邏輯函數(shù) if & boolean

@bg: black;
@bg-light: boolean(luma(@bg) > 50%);

div {
  background: @bg; 
  color: if(@bg-light, black, white);
}

編譯 css 為:

div {
  background: black;
  color: white;
}

字符串函數(shù)

:-:-
escapeURL 編碼應(yīng)用于輸入字符串中的特殊字符
e字符串轉(zhuǎn)義
%第一個(gè)參數(shù)是帶有占位符的字符串
escape('a=1') // 輸出 a%3D1

@mscode: "ms:alwaysHasItsOwnSyntax.For.Stuff()" 
filter: e(@mscode);
// 輸出 filter: ms:alwaysHasItsOwnSyntax.For.Stuff();

format-a-d: %("repetitions: %a file: %d", 1 + 2, "directory/file.less");
// 輸出 format-a-d: "repetitions: 3 file: "directory/file.less"";

替換字符串 replace

replace("Hello, Mars?", "Mars\?", "Earth!");
replace("One + one = 4", "one", "2", "gi");
replace('This is a string.', "(string)\.$", "new $1.");
replace(~"bar-1", '1', '2');

預(yù)期輸出

"Hello, Earth!";
"2 + 2 = 4";
'This is a new string.';
bar-2;

length

@list: "banana", "tomato", "potato", "peach";
n: length(@list);

預(yù)期輸出

n: 4;

返回值列表中的元素?cái)?shù)

extract

@list: apple, pear, coconut, orange;
value: extract(@list, 3);

預(yù)期輸出

value: coconut;

返回列表中指定位置的值

range

value: range(4);
// 輸出 value: 1 2 3 4;
value: range(10px, 30px, 10);
// 輸出 value: 10px 20px 30px;

生成跨越一系列值的列表

each

@selectors: blue, green, red;

each(@selectors, {
  .sel-@{value} {
    a: b;
  }
});

預(yù)期輸出

.sel-blue {
  a: b;
}
.sel-green {
  a: b;
}
.sel-red {
  a: b;
}

每個(gè)列表成員的每個(gè)規(guī)則集都綁定到 @value@key@index 變量

@set: {
  one: blue;
  two: green;
  three: red;
}
.set {
  each(@set, {
    @{key}-@{index}: @value;
  });
}

預(yù)期輸出

.set {
  one-1: blue;
  two-2: green;
  three-3: red;
}

將規(guī)則集的評(píng)估綁定到列表的每個(gè)成員

each()

set-2() {
  one: blue;
  two: green;
  three: red;
}
.set-2 {
  // 調(diào)用 mixin 并迭代每個(gè)規(guī)則
  each(.set-2(), .(@v, @k, @i) {
    @{k}-@{i}: @v;
  });
}

預(yù)期輸出

.set-2 {
  one-1: blue;
  two-2: green;
  three-3: red;
}

使用 rangeeach 創(chuàng)建一個(gè) for 循環(huán)

each(range(4), {
  .col-@{value} {
    height: (@value * 50px);
  }
});

預(yù)期輸出

.col-1 {
  height: 50px;
}
.col-2 {
  height: 100px;
}
.col-3 {
  height: 150px;
}
.col-4 {
  height: 200px;
}

數(shù)學(xué)函數(shù)

:-:-
ceil(2.4) (輸出 3)向上舍入到下一個(gè)最大整數(shù) #
floor(2.6) (輸出 2)向下舍入到下一個(gè)最小整數(shù) #
percentage(0.5) (輸出 50%)將浮點(diǎn)數(shù)轉(zhuǎn)換為百分比字符串 #
round(1.67) (輸出 2)應(yīng)用舍入 #
sqrt(25cm) (輸出 5cm)計(jì)算數(shù)字的平方根。保持單位不變 #
abs(25cm) (輸出 25cm)計(jì)算數(shù)字的絕對(duì)值。 保持單位不變 #
sin(1deg) (輸出 0.01745240643728351)計(jì)算正弦函數(shù) #
asin(-0.8414709848078965) (輸出 -1rad)計(jì)算反正弦(正弦的倒數(shù))函數(shù) #
cos(1deg) (輸出 0.9998476951563913)計(jì)算余弦函數(shù) #
acos(0.5403023058681398) (輸出 1rad)計(jì)算反余弦(余弦的倒數(shù))函數(shù) #
tan(1deg) (輸出 0.017455064928217585)計(jì)算正切函數(shù) #
atan(-1.5574077246549023) (輸出 -1rad)計(jì)算反正切(正切的倒數(shù))函數(shù) #
pi() (輸出 3.141592653589793)π (pi) #
pow(0cm, 0px) (輸出 1cm)返回第一個(gè)參數(shù)的第二個(gè)參數(shù)次冪的值 #
mod(11cm, 6px) (輸出 5cm)返回第一個(gè)參數(shù)模數(shù)第二個(gè)參數(shù)的值 #
min(5, 10) (輸出 5)返回一個(gè)或多個(gè)值中的最小值 #
max(5, 10) (輸出 10)返回一個(gè)或多個(gè)值中的最大值 #

顏色定義函數(shù)

:-:-
rgb#
rgba#
argb#
hsl#
hsla#
hsv#
hsva#

類(lèi)型函數(shù)

:-:-
isnumber值是否為數(shù)字 #
isstring值是否為字符串 #
iscolor值是否為顏色值 #
iskeyword值是否為 keyword #
isurl值是否為 url 值 #
ispixel值是否為像素值 #
isem值是否為 em 值 #
ispercentage值是否為 百分百 值 #
isunit值是是否為指定單位的數(shù)字 #
isruleset值是否為規(guī)則集 #
isdefined值是否為 defined #

雜項(xiàng)函數(shù)

:-:-
color#
image-size#
image-width#
image-height#
convert#
data-uri#
default#
unit#
get-unit#
svg-gradient#

顏色通道函數(shù)

:-:-
hue#
saturation#
lightness#
hsvhue#
hsvsaturation#
hsvvalue#
red#
green#
blue#
alpha#
luma#
luminance#

色彩運(yùn)算函數(shù)

:-:-
saturate#
desaturate#
lighten#
darken#
fadein#
fadeout#
fade#
spin#
mix#
tint#
shade#
greyscale#
contrast#

顏色混合功能

:-:-
multiply#
screen#
overlay#
softlight#
hardlight#
difference#
exclusion#
average#
negation#

另見(jiàn)