<!DOCTYPE html>
<html>
<head>
<style>
.timeline {
position: relative;
margin: 20px 0;
}
.timeline::before {
content: '';
position: absolute;
top: 0;
left: 50%;
width: 2px;
height: 100%;
background-color: #***c;
transform: translateX(-50%);
}
.event {
position: relative;
margin: 20px 0;
}
.event::before {
content: '';
position: absolute;
top: 0;
left: -6px;
width: 12px;
height: 12px;
border-radius: 50%;
background-color: #007bff;
}
.event .content {
margin-left: 20px;
}
</style>
</head>
<body>
<timeline>
<div class="event">Event 1</div>
<div class="event">Event 2</div>
<div class="event">Event 3</div>
</timeline>
<script>javascript">
class Timeline extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: 'open' });
const container = document.createElement('div');
container.classList.add('timeline');
const slot = document.createElement('slot');
container.appendChild(slot);
shadow.appendChild(container);
}
}
customElements.define('timeline', Timeline);
</script>
</body>
</html>
In the above example, we define a custom element called timeline
using the customElements.define()
method. Inside the Timeline
class, we extend the HTMLElement
class to create our custom element.
In the constructor, we create a shadow DOM using attachShadow({ mode: 'open' })
. The shadow DOM encapsulates the internal structure and styling of the custom element.
We create a <div>
element with the class timeline
to act as the container for the timeline ***ponent.
Inside the container, we create a <slot>
element to allow the user to provide the events as child elements of the <timeline>
element.
We append the <slot>
element to the container.
We attach the container element to the shadow DOM.
Each event in the timeline is represented by a <div>
element with the class event
. The events are displayed vertically along a timeline.
The timeline is represented by a vertical line using the ::before
pseudo-element of the .timeline
class.
Each event is displayed as a circle using the ::before
pseudo-element of the .event
class.
You can customize the timeline ***ponent further by adding more styling, additional event information, and functionality as needed.