前端指南 - HTML/CSS/Javascript

    無(wú)規(guī)矩不成方圓,一起來(lái)看看大神的經(jīng)驗(yàn)。前端指南 - HTML/CSS/Javascript

    無(wú)規(guī)矩不成方圓,一起來(lái)看看大神的經(jīng)驗(yàn)。

    HTML

    語(yǔ)義

    HTML5 為我們提供了許多語(yǔ)義元素,旨在精確描述內(nèi)容。確保你受益于它豐富的詞匯。

    <!-- bad -->
    <div id=main>
      <div class=article>
        <div class=header>
          <h1>Blog post</h1>
          <p>Published: <span>21st Feb, 2015</span></p>
        </div>
        <p>…</p>
      </div>
    </div>
    
    <!-- good -->
    <main>
      <article>
        <header>
          <h1>Blog post</h1>
          <p>Published: <time datetime=2015-02-21>21st Feb, 2015</time></p>
        </header>
        <p>…</p>
      </article>
    </main>

    確保您了解正在使用的元素的語(yǔ)義。用錯(cuò)誤的方式使用語(yǔ)義元素比保持中立更糟糕。

    <!-- bad -->
    <h1>
      <figure>
        <img alt=Company src=logo.png>
      </figure>
    </h1>
    
    <!-- good -->
    <h1>
      <img alt=Company src=logo.png>
    </h1>

    簡(jiǎn)潔

    保持代碼簡(jiǎn)潔。忘記你舊的XHTML習(xí)慣。

    <!-- bad -->
    <!doctype html>
    <html lang=en>
      <head>
        <meta http-equiv=Content-Type content="text/html; charset=utf-8" />
        <title>Contact</title>
        <link rel=stylesheet href=style.css type=text/css />
      </head>
      <body>
        <h1>Contact me</h1>
        <label>
          Email address:
          <input type=email placeholder=you@email.com required=required />
        </label>
        <script src=main.js type=text/javascript></script>
      </body>
    </html>
    
    <!-- good -->
    <!doctype html>
    <html lang=en>
      <meta charset=utf-8>
      <title>Contact</title>
      <link rel=stylesheet href=style.css>
    
      <h1>Contact me</h1>
      <label>
        Email address:
        <input type=email placeholder=you@email.com required>
      </label>
      <script src=main.js></script>
    </html>

    輔助功能

    輔助功能不應(yīng)該是事后考慮的。您不必是 WCAG 專家來(lái)改進(jìn)您的網(wǎng)站,您可以立即開始修復(fù)能帶來(lái)巨大變化的小事情,例如:

    • 學(xué)習(xí)正確使用屬性alt
    • 確保您的鏈接和按鈕被標(biāo)記為此類(無(wú)暴行)<div class=button>
    • 不完全依靠顏色來(lái)傳達(dá)信息
    • 明確標(biāo)記表單控件
    <!-- bad -->
    <h1><img alt=Logo src=logo.png></h1>
    
    <!-- good -->
    <h1><img alt=Company src=logo.png></h1>

    語(yǔ)言和字符編碼

    雖然定義語(yǔ)言是可選的,但建議始終在根元素上聲明它。

    HTML 標(biāo)準(zhǔn)要求頁(yè)面使用 UTF-8 字符編碼。必須聲明它,雖然可以在內(nèi)容類型 HTTP 標(biāo)頭中聲明它,但建議始終在文檔級(jí)別聲明它。

    <!-- bad -->
    <!doctype html>
    <title>Hello, world.</title>
    
    <!-- good -->
    <!doctype html>
    <html lang=en>
      <meta charset=utf-8>
      <title>Hello, world.</title>
    </html>

    性能

    除非在內(nèi)容之前加載腳本有正當(dāng)理由,否則不要阻止頁(yè)面的呈現(xiàn)。如果樣式表很重,請(qǐng)隔離最初絕對(duì)必需的樣式,并推遲在單獨(dú)的樣式表中加載輔助聲明。兩個(gè) HTTP 請(qǐng)求明顯慢于一個(gè)請(qǐng)求,但速度感知是最重要的因素。

    <!-- bad -->
    <!doctype html>
    <meta charset=utf-8>
    <script src=analytics.js></script>
    <title>Hello, world.</title>
    <p>...</p>
    
    <!-- good -->
    <!doctype html>
    <meta charset=utf-8>
    <title>Hello, world.</title>
    <p>...</p>
    <script src=analytics.js></script>

    CSS

    分號(hào)

    雖然分號(hào)在技術(shù)上是 CSS 中的分隔符,但始終將其視為終止符。

    /* bad */
    div {
      color: red
    }
    
    /* good */
    div {
      color: red;
    }

    箱模型

    理想情況下,整個(gè)文檔的框模型應(yīng)相同。全局是好的,但如果可以避免,則不要更改特定元素上的默認(rèn)框模型。* { box-sizing: border-box; }

    /* bad */
    div {
      width: 100%;
      padding: 10px;
      box-sizing: border-box;
    }
    
    /* good */
    div {
      padding: 10px;
    }

    Flow

    如果可以避免元素的默認(rèn)行為,請(qǐng)不要更改它。盡可能多地將元素保留在自然文檔流中。例如,刪除圖像下方的空白不應(yīng)使您更改其默認(rèn)顯示:

    /* bad */
    img {
      display: block;
    }
    
    /* good */
    img {
      vertical-align: middle;
    }

    同樣,如果可以避免元素,請(qǐng)不要從流中取下元素。

    /* bad */
    div {
      width: 100px;
      position: absolute;
      right: 0;
    }
    
    /* good */
    div {
      width: 100px;
      margin-left: auto;
    }

    定位

    有許多方法可以定位 CSS 中的元素。青睞現(xiàn)代布局規(guī)范(如 Flexbox 和 Grid),并避免從普通文檔流中刪除元素,例如使用 。position: absolute

    選擇

    最小化與 DOM 緊密耦合的選擇器。考慮在選擇器超過(guò) 3 個(gè)結(jié)構(gòu)偽類、后代或同級(jí)組合器時(shí),將類添加到要匹配的元素。

    /* bad */
    div:first-of-type :last-child > p ~ *
    
    /* good */
    div:first-of-type .info

    避免在不需要時(shí)使選擇器過(guò)載。

    /* bad */
    img[src$=svg], ul > li:first-child {
      opacity: 0;
    }
    
    /* good */
    [src$=svg], ul > :first-child {
      opacity: 0;
    }

    特 異性

    不要使值和選擇器難以覆蓋。盡量減少 的 和 的 使用。id!important

    /* bad */
    .bar {
      color: green !important;
    }
    .foo {
      color: red;
    }
    
    /* good */
    .foo.bar {
      color: green;
    }
    .foo {
      color: red;
    }

    重寫

    重寫樣式使選擇器和調(diào)試更加困難。盡可能避免。

    /* bad */
    li {
      visibility: hidden;
    }
    li:first-child {
      visibility: visible;
    }
    
    /* good */
    li + li {
      visibility: hidden;
    }

    繼承

    不要復(fù)制可以繼承的樣式聲明。

    /* bad */
    div h1, div p {
      text-shadow: 0 1px 0 #fff;
    }
    
    /* good */
    div {
      text-shadow: 0 1px 0 #fff;
    }

    簡(jiǎn)潔

    保持代碼簡(jiǎn)潔。使用速記屬性,避免在不需要時(shí)使用多個(gè)屬性。

    /* bad */
    div {
      transition: all 1s;
      top: 50%;
      margin-top: -10px;
      padding-top: 5px;
      padding-right: 10px;
      padding-bottom: 20px;
      padding-left: 10px;
    }
    
    /* good */
    div {
      transition: 1s;
      top: calc(50% - 10px);
      padding: 5px 10px 20px;
    }

    語(yǔ)言

    比起數(shù)學(xué),更喜歡英語(yǔ)。

    /* bad */
    :nth-child(2n + 1) {
      transform: rotate(360deg);
    }
    
    /* good */
    :nth-child(odd) {
      transform: rotate(1turn);
    }

    供應(yīng)商前綴

    積極終止過(guò)時(shí)的供應(yīng)商前綴。如果需要使用它們,請(qǐng)?jiān)跇?biāo)準(zhǔn)屬性之前插入它們。

    /* bad */
    div {
      transform: scale(2);
      -webkit-transform: scale(2);
      -moz-transform: scale(2);
      -ms-transform: scale(2);
      transition: 1s;
      -webkit-transition: 1s;
      -moz-transition: 1s;
      -ms-transition: 1s;
    }
    
    /* good */
    div {
      -webkit-transform: scale(2);
      transform: scale(2);
      transition: 1s;
    }

    動(dòng)畫

    有利于轉(zhuǎn)換而不是動(dòng)畫。避免對(duì) 和 以外的其他屬性進(jìn)行動(dòng)畫處理。opacitytransform

    /* bad */
    div:hover {
      animation: move 1s forwards;
    }
    @keyframes move {
      100% {
        margin-left: 100px;
      }
    }
    
    /* good */
    div:hover {
      transition: 1s;
      transform: translateX(100px);
    }

    單位

    可以時(shí)使用無(wú)單位值。如果您使用相對(duì)單位,則有利。首選秒數(shù)而不是毫秒。rem

    /* bad */
    div {
      margin: 0px;
      font-size: .9em;
      line-height: 22px;
      transition: 500ms;
    }
    
    /* good */
    div {
      margin: 0;
      font-size: .9rem;
      line-height: 1.5;
      transition: .5s;
    }

    顏色

    如果需要透明度,請(qǐng)使用 。否則,始終使用十六進(jìn)制格式。rgba

    /* bad */
    div {
      color: hsl(103, 54%, 43%);
    }
    
    /* good */
    div {
      color: #5a3;
    }

    繪圖

    當(dāng)資源易于使用 CSS 進(jìn)行復(fù)制時(shí),請(qǐng)避免 HTTP 請(qǐng)求。

    /* bad */
    div::before {
      content: url(white-circle.svg);
    }
    
    /* good */
    div::before {
      content: "";
      display: block;
      width: 20px;
      height: 20px;
      border-radius: 50%;
      background: #fff;
    }

    黑客

    不要使用它們。

    /* bad */
    div {
      // position: relative;
      transform: translateZ(0);
    }
    
    /* good */
    div {
      /* position: relative; */
      will-change: transform;
    }

    Javascript

    性能

    比起性能,更有利于可讀性、正確性和表達(dá)性。JavaScript 基本上永遠(yuǎn)不會(huì)成為您的性能瓶頸。而是優(yōu)化圖像壓縮、網(wǎng)絡(luò)訪問(wèn)和 DOM 回流等內(nèi)容。如果您只記住本文檔中的一個(gè)準(zhǔn)則,請(qǐng)選擇此準(zhǔn)則。

    // bad (albeit way faster)
    const arr = [1, 2, 3, 4];
    const len = arr.length;
    var i = -1;
    var result = [];
    while (++i < len) {
      var n = arr[i];
      if (n % 2 > 0) continue;
      result.push(n * n);
    }
    
    // good
    const arr = [1, 2, 3, 4];
    const isEven = n => n % 2 == 0;
    const square = n => n * n;
    
    const result = arr.filter(isEven).map(square);

    無(wú) 國(guó)籍

    盡量保持功能純凈。理想情況下,所有函數(shù)都不應(yīng)產(chǎn)生副作用,不使用外部數(shù)據(jù)并返回新對(duì)象,而不是突變現(xiàn)有對(duì)象。

    // bad
    const merge = (target, ...sources) => Object.assign(target, ...sources);
    merge({ foo: "foo" }, { bar: "bar" }); // => { foo: "foo", bar: "bar" }
    
    // good
    const merge = (...sources) => Object.assign({}, ...sources);
    merge({ foo: "foo" }, { bar: "bar" }); // => { foo: "foo", bar: "bar" }

    當(dāng)?shù)厝?/h3>

    盡可能依賴本機(jī)方法。

    // bad
    const toArray = obj => [].slice.call(obj);
    
    // good
    const toArray = (() =>
      Array.from ? Array.from : obj => [].slice.call(obj)
    )();

    強(qiáng)制

    在有意義的時(shí)候接受隱含的脅迫。否則避免。不要貨物崇拜。

    // bad
    if (x === undefined || x === null) { ... }
    
    // good
    if (x == undefined) { ... }

    循環(huán)

    不要使用循環(huán),因?yàn)樗鼈儠?huì)強(qiáng)制您使用可變對(duì)象。依賴于方法。array.prototype

    // bad
    const sum = arr => {
      var sum = 0;
      var i = -1;
      for (;arr[++i];) {
        sum += arr[i];
      }
      return sum;
    };
    
    sum([1, 2, 3]); // => 6
    
    // good
    const sum = arr =>
      arr.reduce((x, y) => x + y);
    
    sum([1, 2, 3]); // => 6

    如果你不能,或者使用方法可以說(shuō)是濫用,使用遞歸。array.prototype

    // bad
    const createDivs = howMany => {
      while (howMany--) {
        document.body.insertAdjacentHTML("beforeend", "<div></div>");
      }
    };
    createDivs(5);
    
    // bad
    const createDivs = howMany =>
      [...Array(howMany)].forEach(() =>
        document.body.insertAdjacentHTML("beforeend", "<div></div>")
      );
    createDivs(5);
    
    // good
    const createDivs = howMany => {
      if (!howMany) return;
      document.body.insertAdjacentHTML("beforeend", "<div></div>");
      return createDivs(howMany - 1);
    };
    createDivs(5);

    下面是一個(gè)通用循環(huán)函數(shù),使遞歸更易于使用。

    參數(shù)

    忘記對(duì)象。其余參數(shù)始終是一個(gè)更好的選項(xiàng),因?yàn)椋?code>arguments

    1. 它的名字,所以它給你一個(gè)更好的想法,函數(shù)期待的參數(shù)
    2. 它是一個(gè)真正的數(shù)組,這使得它更容易使用。
    // bad
    const sortNumbers = () =>
      Array.prototype.slice.call(arguments).sort();
    
    // good
    const sortNumbers = (...numbers) => numbers.sort();

    應(yīng)用

    忘記 。改用點(diǎn)差運(yùn)算符。apply()

    const greet = (first, last) => `Hi ${first} ${last}`;
    const person = ["John", "Doe"];
    
    // bad
    greet.apply(null, person);
    
    // good
    greet(...person);

    綁定

    當(dāng)有一個(gè)更慣用的方法時(shí),不要這樣做。bind()

    // bad
    ["foo", "bar"].forEach(func.bind(this));
    
    // good
    ["foo", "bar"].forEach(func, this);
    // bad
    const person = {
      first: "John",
      last: "Doe",
      greet() {
        const full = function() {
          return `${this.first} ${this.last}`;
        }.bind(this);
        return `Hello ${full()}`;
      }
    }
    
    // good
    const person = {
      first: "John",
      last: "Doe",
      greet() {
        const full = () => `${this.first} ${this.last}`;
        return `Hello ${full()}`;
      }
    }

    高階函數(shù)

    不必嵌套函數(shù)。

    // bad
    [1, 2, 3].map(num => String(num));
    
    // good
    [1, 2, 3].map(String);

    組成

    避免多次嵌套函數(shù)調(diào)用。改用合成。

    const plus1 = a => a + 1;
    const mult2 = a => a * 2;
    
    // bad
    mult2(plus1(5)); // => 12
    
    // good
    const pipeline = (...funcs) => val => funcs.reduce((a, b) => b(a), val);
    const addThenMult = pipeline(plus1, mult2);
    addThenMult(5); // => 12

    緩存

    緩存功能測(cè)試、大型數(shù)據(jù)結(jié)構(gòu)和任何昂貴的操作。

    // bad
    const contains = (arr, value) =>
      Array.prototype.includes
        ? arr.includes(value)
        : arr.some(el => el === value);
    contains(["foo", "bar"], "baz"); // => false
    
    // good
    const contains = (() =>
      Array.prototype.includes
        ? (arr, value) => arr.includes(value)
        : (arr, value) => arr.some(el => el === value)
    )();
    contains(["foo", "bar"], "baz"); // => false

    變量

    一遍又一遍地偏袒。constletletvar

    // bad
    var me = new Map();
    me.set("name", "Ben").set("country", "Belgium");
    
    // good
    const me = new Map();
    me.set("name", "Ben").set("country", "Belgium");

    條件

    如果(否則)和切換語(yǔ)句,則支持 IIFE 并返回語(yǔ)句。

    // bad
    var grade;
    if (result < 50)
      grade = "bad";
    else if (result < 90)
      grade = "good";
    else
      grade = "excellent";
    
    // good
    const grade = (() => {
      if (result < 50)
        return "bad";
      if (result < 90)
        return "good";
      return "excellent";
    })();

    對(duì)象迭代

    盡可能避免。for...in

    const shared = { foo: "foo" };
    const obj = Object.create(shared, {
      bar: {
        value: "bar",
        enumerable: true
      }
    });
    
    // bad
    for (var prop in obj) {
      if (obj.hasOwnProperty(prop))
        console.log(prop);
    }
    
    // good
    Object.keys(obj).forEach(prop => console.log(prop));

    對(duì)象作為地圖

    雖然對(duì)象具有合法的用例,但地圖通常是更好、更強(qiáng)大的選擇。當(dāng)有疑問(wèn)時(shí),請(qǐng)使用 。Map

    // bad
    const me = {
      name: "Ben",
      age: 30
    };
    var meSize = Object.keys(me).length;
    meSize; // => 2
    me.country = "Belgium";
    meSize++;
    meSize; // => 3
    
    // good
    const me = new Map();
    me.set("name", "Ben");
    me.set("age", 30);
    me.size; // => 2
    me.set("country", "Belgium");
    me.size; // => 3

    咖喱

    對(duì)于許多開發(fā)人員來(lái)說(shuō),咖喱是一種強(qiáng)大但外國(guó)的模式。不要濫用它,因?yàn)樗m當(dāng)?shù)挠美窍喈?dāng)不尋常的。

    // bad
    const sum = a => b => a + b;
    sum(5)(3); // => 8
    
    // good
    const sum = (a, b) => a + b;
    sum(5, 3); // => 8

    可讀性

    不要使用看似聰明的技巧來(lái)混淆代碼的意圖。

    // bad
    foo || doSomething();
    
    // good
    if (!foo) doSomething();
    // bad
    void function() { /* IIFE */ }();
    
    // good
    (function() { /* IIFE */ }());
    // bad
    const n = ~~3.14;
    
    // good
    const n = Math.floor(3.14);

    代碼重用

    不要害怕創(chuàng)建大量小型、高度可組合和可重用的功能。

    // bad
    arr[arr.length - 1];
    
    // good
    const first = arr => arr[0];
    const last = arr => first(arr.slice(-1));
    last(arr);
    // bad
    const product = (a, b) => a * b;
    const triple = n => n * 3;
    
    // good
    const product = (a, b) => a * b;
    const triple = product.bind(null, 3);

    依賴

    最小化依賴項(xiàng)。第三方是你不知道的代碼。不要只加載整個(gè)庫(kù),只需幾種方法即可輕松復(fù)制:

    // bad
    var _ = require("underscore");
    _.compact(["foo", 0]));
    _.unique(["foo", "foo"]);
    _.union(["foo"], ["bar"], ["foo"]);
    
    // good
    const compact = arr => arr.filter(el => el);
    const unique = arr => [...new Set(arr)];
    const union = (...arr) => unique([].concat(...arr));
    
    compact(["foo", 0]);
    unique(["foo", "foo"]);
    union(["foo"], ["bar"], ["foo"]);
    隨記

    文派博客 - 國(guó)內(nèi)版優(yōu)化WordPress博客平臺(tái)

    2020-11-22 17:28:55

    網(wǎng)站

    第一彈官網(wǎng)_好玩的動(dòng)漫ACGN社區(qū) - 有彈友,不孤單!

    2019-9-7 15:05:10

    ??
    Npcink上的部份代碼及教程來(lái)源于互聯(lián)網(wǎng),僅供網(wǎng)友學(xué)習(xí)交流,若您喜歡本文可附上原文鏈接隨意轉(zhuǎn)載。
    無(wú)意侵害您的權(quán)益,請(qǐng)發(fā)送郵件至 1355471563#qq.com 或點(diǎn)擊右側(cè) 私信:Muze 反饋,我們將盡快處理。
    0 條回復(fù) A文章作者 M管理員
      暫無(wú)討論,說(shuō)說(shuō)你的看法吧
    ?
    個(gè)人中心
    購(gòu)物車
    優(yōu)惠劵
    今日簽到
    有新私信 私信列表
    搜索
    主站蜘蛛池模板: 亚洲av永久无码一区二区三区| 天堂va视频一区二区| 亚洲欧洲∨国产一区二区三区| 国产婷婷一区二区三区| 波多野结衣在线观看一区二区三区| 日本强伦姧人妻一区二区| 视频一区二区精品的福利| 国产激情一区二区三区成人91| 国产精品视频一区二区三区四| 亚洲视频一区二区三区四区| 呦系列视频一区二区三区| 日韩一区在线视频| 高清国产精品人妻一区二区| 亚洲福利一区二区精品秒拍| 99国产精品欧美一区二区三区| 乱中年女人伦av一区二区| 一区二区免费视频| 国精产品一区一区三区MBA下载| 夜色阁亚洲一区二区三区| 色狠狠一区二区三区香蕉| 国内精品视频一区二区三区八戒| 丰满岳乱妇一区二区三区| 中文字幕一区二区三区精彩视频| 韩国资源视频一区二区三区| 国产精品女同一区二区久久| 天堂va在线高清一区| 成人一区二区免费视频| 亚洲国产韩国一区二区| 国产免费一区二区三区免费视频| 精品视频一区二区三三区四区 | av无码一区二区三区| 久久久无码一区二区三区| 国产在线观看91精品一区| 在线观看亚洲一区二区| 一区二区三区免费在线视频| 国产精品视频一区二区三区| 国产伦精品一区二区免费| 怡红院一区二区三区| 亚洲一区在线视频观看| 国产婷婷一区二区三区| 精品一区二区三区免费毛片|