Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated NavBar with Buttons #1062

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions NavBarAnimated/moezmustafa/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="tab-container">
<div class="indicator"></div>
<button class="tab-button active" data-text="Home">
<ion-icon name="home" size="large"></ion-icon>
</button>
<button class="tab-button" data-text="Search">
<ion-icon name="search" size="large"></ion-icon>
</button>
<button class="tab-button" data-text="Message">
<ion-icon name="mail" size="large"></ion-icon>
</button>
<button class="tab-button" data-text="Profile">
<ion-icon name="person" size="large"></ion-icon>
</button>
</div>

<script src="script.js"></script>
</body>
</html>
32 changes: 32 additions & 0 deletions NavBarAnimated/moezmustafa/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const menuItems = document.querySelectorAll('.tab-button');
const indicator = document.querySelector('.indicator');

let activeItemIndex = 0;

function updateIndicator(index) {
const activeItem = menuItems[index];
const container = document.querySelector('.tab-container');
const containerRect = container.getBoundingClientRect();
const activeItemRect = activeItem.getBoundingClientRect();

const indicatorLeft = Math.floor(
activeItemRect.left - containerRect.left -
(indicator.offsetWidth - activeItemRect.width) / 2
);

indicator.style.transform = `translateX(${indicatorLeft}px)`;
}

menuItems.forEach((item, index) => {
item.addEventListener('click', () => {
if (activeItemIndex !== index) {
menuItems[activeItemIndex].classList.remove('active');
item.classList.add('active');
activeItemIndex = index;
updateIndicator(index);
}
});
});

// Initialize with the first active item
updateIndicator(activeItemIndex);
62 changes: 62 additions & 0 deletions NavBarAnimated/moezmustafa/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #C3F2FF;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
.tab-container {
position: relative;
display: flex;
align-items: center;
justify-content: center;
background-color: #032146;
color: #fff;
border-radius: 0 0 20px 20px;
overflow: hidden;
width: 28em;
height: 8em;
padding: 0 0.85em 0.5em;
}
.tab-button {
all: unset;
flex-grow: 1;
padding: .5em;
border: none;
background: none;
color: inherit;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
position: relative;
z-index: 10;
transition: transform 0.5s ease-in-out;
}
.tab-button:after {
content: attr(data-text);
font-size: .9em;
pointer-events: none;
transition: opacity .3s ease-in-out;
opacity: 0;
}
.tab-button.active::after {
opacity: 1;
padding-left: 0.5em;
pointer-events: all;
}

.indicator {
position: absolute;
width: 6.5em;
height: 3.5em;
left: 0;
background-color: rgba(255, 255, 255, 0.1);
color: #fff;
transform: translateX(0);
transition: transform 0.5s;
border-radius: 12px;
}
Loading