Skip to content
Snippets Groups Projects
Commit c7a0856e authored by Shraddha Raghuram's avatar Shraddha Raghuram
Browse files

change navbar js

parent 2f701e98
No related branches found
No related tags found
No related merge requests found
...@@ -132,68 +132,46 @@ ...@@ -132,68 +132,46 @@
</ul> </ul>
</nav> </nav>
<script> <script>
// var navbox = document.getElementById("navbox");
// let navbar = document.getElementById("my-navbar");
// let items = navbar.children;
// function over(el){
// for (i = 0; i < 4; i++){
// items[i].children[0].style.color = "#185A4F";
// }
// el.children[0].style.color = "#62D881";
// if (el.children.length > 1){
// let son = el.children[1];
// if (getComputedStyle(son,null)["display"]=="none"){
// setTimeout(function(){
// son.style.opacity = 1;
// }, 1);
// son.style.display = "block";
// }
// if (document.documentElement.clientWidth > 1400){
// navbox.style.height = "320px";
// }
// else if (document.documentElement.clientHeight < 1120){
// navbox.style.height = "245px";
// }
// else{
// navbox.style.height = "280px";
// }
// }
// }
// function leave(el){
// for (i = 0; i < 4; i++){
// items[i].children[0].style.color = "#185A4F";
// }
// if (el.children.length > 1){
// let son = el.children[1];
// if (getComputedStyle(son,null)["display"]=="block"){
// son.style.opacity = 0;
// setTimeout(function(){
// son.style.display = "none";
// }, 350);
// }
// navbox.style.height = "78px";
// }
// }
var navbox = document.getElementById("navbox"); var navbox = document.getElementById("navbox");
let navbar = document.getElementById("my-navbar"); let navbar = document.getElementById("my-navbar");
let items = navbar.children; let items = navbar.children;
function over(el) { // Add click event listener to each dropdown menu item
resetColors(); for (let i = 0; i < items.length; i++) {
el.children[0].style.color = "#62D881"; let item = items[i];
// Show the current dropdown // Check if the item has a dropdown (sub-menu)
if (item.children.length > 1) {
item.addEventListener('click', function (event) {
toggleDropdown(item);
event.stopPropagation(); // Prevent event bubbling
});
}
}
function toggleDropdown(el) {
// Toggle visibility of the dropdown
let son = el.children[1]; let son = el.children[1];
if (son) { if (son) {
son.style.visibility = "visible"; if (son.style.visibility === "visible") {
son.style.opacity = 1; closeDropdown(el);
el.classList.add('active'); } else {
adjustNavHeight(); openDropdown(el);
}
} }
} }
function leave(el) { function openDropdown(el) {
// Close the current dropdown // Open the dropdown
let son = el.children[1];
son.style.visibility = "visible";
son.style.opacity = 1;
el.classList.add('active');
adjustNavHeight();
}
function closeDropdown(el) {
// Close the dropdown
let son = el.children[1]; let son = el.children[1];
if (son) { if (son) {
son.style.opacity = 0; son.style.opacity = 0;
...@@ -220,5 +198,60 @@ function adjustNavHeight() { ...@@ -220,5 +198,60 @@ function adjustNavHeight() {
navbox.style.height = "280px"; navbox.style.height = "280px";
} }
} }
// Close dropdowns when clicking outside
document.addEventListener('click', function () {
for (let i = 0; i < items.length; i++) {
if (items[i].classList.contains('active')) {
closeDropdown(items[i]);
}
}
});
// var navbox = document.getElementById("navbox");
// let navbar = document.getElementById("my-navbar");
// let items = navbar.children;
// function over(el) {
// resetColors();
// el.children[0].style.color = "#62D881";
// // Show the current dropdown
// let son = el.children[1];
// if (son) {
// son.style.visibility = "visible";
// son.style.opacity = 1;
// el.classList.add('active');
// adjustNavHeight();
// }
// }
// function leave(el) {
// // Close the current dropdown
// let son = el.children[1];
// if (son) {
// son.style.opacity = 0;
// setTimeout(() => {
// son.style.visibility = "hidden";
// el.classList.remove('active');
// navbox.style.height = "78px"; // Reset the height
// }, 300); // Match this delay with the CSS transition time
// }
// }
// function resetColors() {
// for (let i = 0; i < items.length; i++) {
// items[i].children[0].style.color = "#185A4F";
// }
// }
// function adjustNavHeight() {
// if (document.documentElement.clientWidth > 1400) {
// navbox.style.height = "320px";
// } else if (document.documentElement.clientHeight < 1120) {
// navbox.style.height = "245px";
// } else {
// navbox.style.height = "280px";
// }
// }
</script> </script>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment