Html自定义组件
复用HTML组件,可以用这种方式创建自己的组件库。
文件结构如下
. ├── components │ ├── footer.js │ └── header.js ├── index.html └── styles.css
styles.css
文件* { margin: 0; padding: 0; box-sizing: border-box; } html, body { height: 100%; } body { color: #333; font-family: sans-serif; display: flex; flex-direction: column; } main { flex: 1 0 auto; }
index.html
文件<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" /> <link href="styles.css" rel="stylesheet" type="text/css" /> <script src="components/header.js" type="text/javascript" defer></script> <script src="components/footer.js" type="text/javascript" defer></script> </head> <body> <header-component></header-component> <main> <!-- Your page's content --> </main> <footer-component></footer-component> </body> </html>
header.js文件
const headerTemplate = document.createElement('template');
headerTemplate.innerHTML = `
<style>
nav {
height: 40px;
display: flex;
align-items: center;
justify-content: center;
background-color: #0a0a23;
}
ul li {
list-style: none;
display: inline;
}
a {
font-weight: 700;
margin: 0 25px;
color: #fff;
text-decoration: none;
}
a:hover {
padding-bottom: 5px;
box-shadow: inset 0 -2px 0 0 #fff;
}
</style>
<header>
<nav>
<ul>
<li><a href="about.html">About</a></li>
<li><a href="work.html">Work</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
</header>
`;
class Header extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const shadowRoot = this.attachShadow({ mode: 'closed' });
shadowRoot.appendChild(headerTemplate.content);
}
}
customElements.define('header-component', Header);
footer.js
文件class Footer extends HTMLElement { constructor() { super(); } connectedCallback() { this.innerHTML = ` <style> footer { height: 60px; padding: 0 10px; list-style: none; display: flex; justify-content: space-between; align-items: center; background-color: #dfdfe2; } ul li { list-style: none; display: inline; } a { margin: 0 15px; color: inherit; text-decoration: none; } a:hover { padding-bottom: 5px; box-shadow: inset 0 -2px 0 0 #333; } .social-row { font-size: 20px; } .social-row li a { margin: 0 15px; } </style> <footer> <ul> <li><a href="about.html">About</a></li> <li><a href="work.html">Work</a></li> <li><a href="contact.html">Contact</a></li> </ul> <ul class="social-row"> <li><a href="https://github.com/my-github-profile"><i class="fab fa-github"></i></a></li> <li><a href="https://twitter.com/my-twitter-profile"><i class="fab fa-twitter"></i></a></li> <li><a href="https://www.linkedin.com/in/my-linkedin-profile"><i class="fab fa-linkedin"></i></a></li> </ul> </footer> `; } } customElements.define('footer-component', Footer);
浏览器打开index.html显示如下