lit_card_example.html
· 1.4 KiB · HTML
Originalformat
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lit 模組範例</title>
<script type="module" src="https://unpkg.com/lit@2.0.0/lit.min.js"></script>
</head>
<body>
<my-card title="歡迎訊息" content="這是一張使用 Shadow DOM 的卡片。"></my-card>
<style>
/* 這個樣式不會影響 Shadow DOM 內的元素 */
p {
color: red;
font-size: 24px;
}
</style>
<p>這是頁面上的一般段落,樣式不會影響到卡片內容。</p>
<script type="module">
import { LitElement, html, css } from 'https://unpkg.com/lit@2.0.0?module';
class MyCard extends LitElement {
// 定義樣式
static styles = css`
.card {
border: 1px solid #ccc;
border-radius: 4px;
padding: 16px;
margin: 16px;
font-family: Arial, sans-serif;
}
h2 {
color: #333;
font-size: 18px;
}
p {
color: #666;
font-size: 14px;
}
`;
// 定義屬性
static properties = {
title: { type: String },
content: { type: String },
};
// 元件渲染模板
render() {
return html`
<div class="card">
<h2>${this.title}</h2>
<p>${this.content}</p>
</div>
`;
}
}
customElements.define('my-card', MyCard);
</script>
</body>
</html>
| 1 | <!DOCTYPE html> |
| 2 | <html lang="zh-TW"> |
| 3 | <head> |
| 4 | <meta charset="UTF-8"> |
| 5 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | <title>Lit 模組範例</title> |
| 7 | <script type="module" src="https://unpkg.com/lit@2.0.0/lit.min.js"></script> |
| 8 | </head> |
| 9 | <body> |
| 10 | |
| 11 | <my-card title="歡迎訊息" content="這是一張使用 Shadow DOM 的卡片。"></my-card> |
| 12 | |
| 13 | <style> |
| 14 | /* 這個樣式不會影響 Shadow DOM 內的元素 */ |
| 15 | p { |
| 16 | color: red; |
| 17 | font-size: 24px; |
| 18 | } |
| 19 | </style> |
| 20 | |
| 21 | <p>這是頁面上的一般段落,樣式不會影響到卡片內容。</p> |
| 22 | |
| 23 | <script type="module"> |
| 24 | import { LitElement, html, css } from 'https://unpkg.com/lit@2.0.0?module'; |
| 25 | |
| 26 | |
| 27 | class MyCard extends LitElement { |
| 28 | // 定義樣式 |
| 29 | static styles = css` |
| 30 | .card { |
| 31 | border: 1px solid #ccc; |
| 32 | border-radius: 4px; |
| 33 | padding: 16px; |
| 34 | margin: 16px; |
| 35 | font-family: Arial, sans-serif; |
| 36 | } |
| 37 | h2 { |
| 38 | color: #333; |
| 39 | font-size: 18px; |
| 40 | } |
| 41 | p { |
| 42 | color: #666; |
| 43 | font-size: 14px; |
| 44 | } |
| 45 | `; |
| 46 | |
| 47 | // 定義屬性 |
| 48 | static properties = { |
| 49 | title: { type: String }, |
| 50 | content: { type: String }, |
| 51 | }; |
| 52 | |
| 53 | // 元件渲染模板 |
| 54 | render() { |
| 55 | return html` |
| 56 | <div class="card"> |
| 57 | <h2>${this.title}</h2> |
| 58 | <p>${this.content}</p> |
| 59 | </div> |
| 60 | `; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | customElements.define('my-card', MyCard); |
| 65 | </script> |
| 66 | |
| 67 | </body> |
| 68 | </html> |
| 69 |