Web Components 系列(十)—— 實現 MyCard 的基本佈局
前言
前面針對 Web Components 學習了一些基本的理論知識,我們瞭解到的概念有:
- Custom Elements
- Shadow DOM
- Templates
- Slots
以及和這些概念相關的子知識點。
理論知識基本上夠用了,從現在開始我們需要將理論運用到實踐中,讓理論為實踐服務。今天,我們就使用 Web Components 的相關知識來實現 MyCard 的製作,原型呢就以我們人人都有的身份證做參照吧。
最終實現的基本佈局效果如下:
使用 Templates 佈局
這裡我們使用 HTML 模板將佈局先構建出來,程式碼如下:
<template id="card_layout"> <style> * { box-sizing: border-box; } :host { display: inline-block; width: 400px; height: 240px; border: 1px solid black; border-radius: 10px; box-shadow: -2px -2px 5px 0px #7a8489; } .container { display: flex; flex-direction: column; padding: 10px; height: 100%; } .card-body { flex: 1; display: flex; } .card-footer { padding: 10px 0; } .main-info { flex: 2; } .photo { flex: 1; display: flex; align-items: center; } .photo img{ width: 100%; } .info-row { display: flex; padding-top: 15px; } .info-column { display: flex; align-items: center; } .info-title { padding: 0 10px; color: #0e5bd3; font-size: 12px; word-break: keep-all; } .info-content { letter-spacing: 2px; } </style> <div class="container"> <div class="card-body"> <div class="main-info"> <div class="info-row"> <div class="info-column"> <div class="info-title">姓名</div> </div> <div class="info-content">程式設計三昧</div> </div> <div class="info-row"> <div class="info-column"> <div class="info-title">性別</div> <div class="info-content">男</div> </div> <div class="info-column"> <div class="info-title">民族</div> <div class="info-content">漢</div> </div> </div> <div class="info-row"> <div class="info-column"> <div class="info-title">出生</div> <div class="info-content">2022</div> </div> <div class="info-column"> <div class="info-title">年</div> <div class="info-content">12</div> </div> <div class="info-column"> <div class="info-title">月</div> <div class="info-content">12</div> </div> <div class="info-column"> <div class="info-title">日</div> </div> </div> <div class="info-row"> <div class="info-column"> <div class="info-title">住址</div> </div> <div class="info-content">xx省xx市xx區xx街道xx小區xx樓xx單元xx樓xx室</div> </div> </div> <div class="photo"> <img src="./static/photo.jpg"> </div> </div> <div class="card-footer"> <div class="info-row"> <div class="info-column"> <div class="info-title">公民身份號碼</div> </div> <div class="info-content">12345678901234567X</div> </div> </div> </div> </div> </template>
建立自定義元素
使用我們前面學習過的方法,建立一個基本的自定義元素 my-card
,在自定義元件中引入 Templates 佈局,程式碼如下:
class MyCard extends HTMLElement { constructor () { super(); this.shadow = this.attachShadow({mode: "open"}); let tempEle = document.getElementById("card_layout"); this.shadow.appendChild(tempEle.content); } } customElements.define("my-card", MyCard);
使用自定義元素
在 HTML 的 body 中引入 my-card
標籤:
<my-card></my-card>
總結
最終實現的效果如文章開頭所示。
在這篇文章中,我們只使用 Custom Elements 和 Templates 實現了基本佈局和樣式,實用價值基本為零。
在後續中,會加入 Slots 讓自定義元素實現可複用的效果。
~
~ 本文完,感謝閱讀!
~
學習有趣的知識,結識有趣的朋友,塑造有趣的靈魂!
大家好,我是〖程式設計三昧〗的作者 隱逸王 ,我的公眾號是『程式設計三昧』,歡迎關注,希望大家多多指教!
「其他文章」
- 天翼雲全場景業務無縫替換至國產原生作業系統CTyunOS!
- 以羊了個羊為例,淺談小程式抓包與響應報文修改
- 這幾種常見的 JVM 調優場景,你知道嗎?
- 如此狂妄,自稱高效能佇列的Disruptor有啥來頭?
- 為什麼要學習GoF設計模式?
- 827. 最大人工島 : 簡單「並查集 列舉」運用題
- 手把手教你如何使用 Timestream 實現物聯網時序資料儲存和分析
- 850. 矩形面積 II : 掃描線模板題
- Java 併發程式設計解析 | 基於JDK原始碼解析Java領域中的併發鎖,我們可以從中學習到什麼內容?
- 【手把手】光說不練假把式,這篇全鏈路壓測實踐探索
- 大廠鍾愛的全鏈路壓測有什麼意義?四種壓測方案詳細對比分析
- 寫個續集,填坑來了!關於“Thread.sleep(0)這一行‘看似無用’的程式碼”裡面留下的坑。
- 857. 僱傭 K 名工人的最低成本 : 列舉 優先佇列(堆)運用題
- Vue3 實現一個自定義toast(小彈窗)
- 669. 修剪二叉搜尋樹 : 常規樹的遍歷與二叉樹性質
- 讀完 RocketMQ 原始碼,我學會了如何優雅的建立執行緒
- 效能調優——小小的log大大的坑
- 1582. 二進位制矩陣中的特殊位置 : 簡單模擬題
- elementui原始碼學習之仿寫一個el-switch
- 646. 最長數對鏈 : 常規貪心 DP 運用題