前端指南 - HTML/CSS/Javascript

    無規矩不成方圓,一起來看看大神的經驗。前端指南 - HTML/CSS/Javascript

    無規矩不成方圓,一起來看看大神的經驗。

    HTML

    語義

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

    <!-- 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>

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

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

    簡潔

    保持代碼簡潔。忘記你舊的XHTML習慣。

    <!-- 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>

    輔助功能

    輔助功能不應該是事后考慮的。您不必是 WCAG 專家來改進您的網站,您可以立即開始修復能帶來巨大變化的小事情,例如:

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

    語言和字符編碼

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

    HTML 標準要求頁面使用 UTF-8 字符編碼。必須聲明它,雖然可以在內容類型 HTTP 標頭中聲明它,但建議始終在文檔級別聲明它。

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

    性能

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

    <!-- 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

    分號

    雖然分號在技術上是 CSS 中的分隔符,但始終將其視為終止符。

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

    箱模型

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

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

    Flow

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

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

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

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

    定位

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

    選擇

    最小化與 DOM 緊密耦合的選擇器??紤]在選擇器超過 3 個結構偽類、后代或同級組合器時,將類添加到要匹配的元素。

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

    避免在不需要時使選擇器過載。

    /* 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;
    }

    重寫

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

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

    繼承

    不要復制可以繼承的樣式聲明。

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

    簡潔

    保持代碼簡潔。使用速記屬性,避免在不需要時使用多個屬性。

    /* 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;
    }

    語言

    比起數學,更喜歡英語。

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

    供應商前綴

    積極終止過時的供應商前綴。如果需要使用它們,請在標準屬性之前插入它們。

    /* 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;
    }

    動畫

    有利于轉換而不是動畫。避免對 和 以外的其他屬性進行動畫處理。opacitytransform

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

    單位

    可以時使用無單位值。如果您使用相對單位,則有利。首選秒數而不是毫秒。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;
    }

    顏色

    如果需要透明度,請使用 。否則,始終使用十六進制格式。rgba

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

    繪圖

    當資源易于使用 CSS 進行復制時,請避免 HTTP 請求。

    /* 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

    性能

    比起性能,更有利于可讀性、正確性和表達性。JavaScript 基本上永遠不會成為您的性能瓶頸。而是優化圖像壓縮、網絡訪問和 DOM 回流等內容。如果您只記住本文檔中的一個準則,請選擇此準則。

    // 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);

    無 國籍

    盡量保持功能純凈。理想情況下,所有函數都不應產生副作用,不使用外部數據并返回新對象,而不是突變現有對象。

    // 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" }

    當地人

    盡可能依賴本機方法。

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

    強制

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

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

    循環

    不要使用循環,因為它們會強制您使用可變對象。依賴于方法。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

    如果你不能,或者使用方法可以說是濫用,使用遞歸。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);

    下面是一個通用循環函數,使遞歸更易于使用。

    參數

    忘記對象。其余參數始終是一個更好的選項,因為:arguments

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

    應用

    忘記 。改用點差運算符。apply()

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

    綁定

    當有一個更慣用的方法時,不要這樣做。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()}`;
      }
    }

    高階函數

    不必嵌套函數。

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

    組成

    避免多次嵌套函數調用。改用合成。

    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

    緩存

    緩存功能測試、大型數據結構和任何昂貴的操作。

    // 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");

    條件

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

    // 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";
    })();

    對象迭代

    盡可能避免。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));

    對象作為地圖

    雖然對象具有合法的用例,但地圖通常是更好、更強大的選擇。當有疑問時,請使用 。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

    咖喱

    對于許多開發人員來說,咖喱是一種強大但外國的模式。不要濫用它,因為它適當的用例是相當不尋常的。

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

    可讀性

    不要使用看似聰明的技巧來混淆代碼的意圖。

    // 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);

    代碼重用

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

    // 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);

    依賴

    最小化依賴項。第三方是你不知道的代碼。不要只加載整個庫,只需幾種方法即可輕松復制:

    // 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"]);

    Karo - 手工制作的珠寶WooCommerce WordPress主題

    2019-8-1 18:02:24

    國內主題

    html5blank - WordPress的開源樣板主題

    2019-11-5 7:15:17

    ??
    Npcink上的部份代碼及教程來源于互聯網,僅供網友學習交流,若您喜歡本文可附上原文鏈接隨意轉載。
    無意侵害您的權益,請發送郵件至 1355471563#qq.com 或點擊右側 私信:Muze 反饋,我們將盡快處理。
    0 條回復 A文章作者 M管理員
      暫無討論,說說你的看法吧
    ?
    個人中心
    購物車
    優惠劵
    今日簽到
    有新私信 私信列表
    搜索
    主站蜘蛛池模板: 国产自产对白一区| 男人的天堂av亚洲一区2区| 91麻豆精品国产自产在线观看一区 | 国产福利电影一区二区三区,亚洲国模精品一区 | 91麻豆精品国产自产在线观看一区 | 亚洲一区在线免费观看| 国产av福利一区二区三巨| 久久国产一区二区三区| 亚洲AⅤ无码一区二区三区在线 | 国产在线精品一区二区三区不卡| 亚洲精品一区二区三区四区乱码| 国产在线精品一区二区不卡| 亚洲.国产.欧美一区二区三区| 久久综合精品国产一区二区三区| 国产一区二区草草影院| 日本一区二三区好的精华液| 久久精品国产亚洲一区二区| 99久久精品国产高清一区二区 | 波多野结衣一区二区| 亚洲欧洲一区二区| 亚洲一区综合在线播放| 亚洲一区二区电影| 久久精品视频一区二区三区| 亚洲国产精品一区二区第一页| 又硬又粗又大一区二区三区视频| 国产午夜精品一区二区三区嫩草 | 亚洲一本一道一区二区三区| 国产精品第一区揄拍| 男人的天堂av亚洲一区2区| 在线视频国产一区| 国产伦一区二区三区高清| 国产乱码精品一区二区三区中文| 成人午夜视频精品一区| 日本在线不卡一区| 国产精品一级香蕉一区| 亚洲av乱码中文一区二区三区| 国产精品视频免费一区二区三区| 一区二区网站在线观看| 亚洲午夜福利AV一区二区无码| 日本不卡一区二区视频a| 久久无码人妻一区二区三区午夜 |