pulse-tracker / components /bottomNav.js
CSNET's picture
ทำแอพ HTML5 ภาษาไทย โดยมี module ดังนี้
df62a7a verified
Raw History Blame
6.05 kB
class BottomNav extends HTMLElement {
connectedCallback() {
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<style>
.bottom-nav {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background: white;
border-top: 1px solid #e5e7eb;
box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
z-index: 50;
}
.nav-container {
max-width: 480px;
margin: 0 auto;
display: flex;
justify-content: space-around;
padding: 8px 0;
}
.nav-item {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
padding: 8px 4px;
text-decoration: none;
color: #6b7280;
transition: all 0.3s ease;
cursor: pointer;
position: relative;
}
.nav-item.active {
color: #6366f1;
}
.nav-item:hover {
color: #6366f1;
}
.nav-item i {
width: 24px;
height: 24px;
margin-bottom: 4px;
}
.nav-label {
font-size: 11px;
font-weight: 500;
}
.pulse-dot {
position: absolute;
top: 8px;
right: calc(50% - 20px);
width: 8px;
height: 8px;
background: #ef4444;
border-radius: 50%;
animation: pulse 2s infinite;
}
@keyframes pulse {
0% {
transform: scale(0.95);
box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7);
}
70% {
transform: scale(1);
box-shadow: 0 0 0 10px rgba(239, 68, 68, 0);
}
100% {
transform: scale(0.95);
box-shadow: 0 0 0 0 rgba(239, 68, 68, 0);
}
}
</style>
<div class="bottom-nav">
<div class="nav-container">
<a href="#dashboard" class="nav-item active" data-page="dashboard">
<i data-feather="home"></i>
<span class="nav-label">วันนี้</span>
</a>
<a href="#sleep" class="nav-item" data-page="sleep">
<i data-feather="moon"></i>
<span class="nav-label">นอน</span>
</a>
<a href="#activity" class="nav-item" data-page="activity">
<i data-feather="activity"></i>
<span class="nav-label">กิจกรรม</span>
</a>
<a href="#nutrition" class="nav-item" data-page="nutrition">
<i data-feather="coffee"></i>
<span class="nav-label">โภชนาการ</span>
</a>
<a href="#mental" class="nav-item" data-page="mental">
<i data-feather="heart"></i>
<span class="nav-label">จิตใจ</span>
<div class="pulse-dot"></div>
</a>
</div>
</div>
`;
// Add click event listeners
this.shadowRoot.querySelectorAll('.nav-item').forEach(item => {
item.addEventListener('click', (e) => {
e.preventDefault();
const targetId = item.getAttribute('href').substring(1);
// Update active state
this.shadowRoot.querySelectorAll('.nav-item').forEach(navItem => {
navItem.classList.remove('active');
});
item.classList.add('active');
// Navigate to page
navigateToPage(targetId);
});
});
}
}
customElements.define('bottom-nav', BottomNav);
function navigateToPage(pageId) {
// Hide all pages
document.querySelectorAll('.page-content').forEach(page => {
page.classList.remove('active');
});
// Show target page
const targetPage = document.getElementById(pageId);
if (targetPage) {
targetPage.classList.add('active');
}
// Update URL hash
window.location.hash = pageId;
// Scroll to top
window.scrollTo(0, 0);
}
// Handle browser back/forward buttons
window.addEventListener('popstate', function() {
const hash = window.location.hash.substring(1);
if (hash) {
navigateToPage(hash);
// Update bottom nav active state
document.querySelectorAll('bottom-nav .nav-item').forEach(item => {
item.classList.remove('active');
if (item.getAttribute('href') === '#' + hash) {
item.classList.add('active');
}
});
}
});
// Set initial page based on URL hash
window.addEventListener('load', function() {
const hash = window.location.hash.substring(1) || 'dashboard';
navigateToPage(hash);
// Update bottom nav active state
document.querySelectorAll('bottom-nav .nav-item').forEach(item => {
item.classList.remove('active');
if (item.getAttribute('href') === '#' + hash) {
item.classList.add('active');
}
});
});