gpt4 book ai didi

javascript - 导航栏 : Tab icons disapear when I click on them

转载 作者:太空宇宙 更新时间:2023-11-04 06:10:35 24 4
gpt4 key购买 nike

我正在处理这个导航栏。每当我单击选项卡时,我都会将导航栏更改为不同的颜色,但这导致了一个问题:当我单击一个图标时,导航栏会更改颜色但选项卡的 Logo 会消失。它仅在我单击另一个选项卡时才会重新出现,该选项卡也会消失……换句话说,选项卡在单击并处于事件状态时消失。我找不到这是怎么发生的,我对此很陌生,所以如果有人有空的话,任何提示都将不胜感激。

非常感谢

HTML:

<!--Header-->
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.0/css/all.min.css'>

<link rel="stylesheet" href="css/style.css">
<!--End Header-->

</head>

<body>

<!--Header-->
<div class="tab-nav-container">

<div class="tab active purple">
<i class="fas fa-home"></i>
</div>
<div class="tab pink">
<i class="far fa-code"></i>
</div>
<div class="tab teal">
<i class="far fa-user"></i>
</div>
<div class="tab yellow">
<i class="fas fa-search"></i>
</div>
<div class="tab yellow">
<i class="far fa-at"></i>
</div>

</div>

<script src="js/index.js"></script>
<!--End Header-->

CSS:

    box-sizing: border-box;
}

body {

display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
transition: background 0.4s linear;

text-align: center;

}

.tab-nav-container {
background-color: #fff;
border-bottom-right-radius: 50px;
border-bottom-left-radius: 50px;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
display: flex;
justify-content: space-between;
padding: 30px;
width: 500px;
}

.tab {
background-color: transparent;
border-radius: 50px;
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
padding: 0 0px;
margin: 0 0px;
transition: tab-nav-container 0.4s linear;
}

.tab i {
font-size: 1.2em;
}

.tab p {
font-weight: bold;
overflow: hidden;
max-width: 0;
}

.tab.active p {
margin-left: 0px;
max-width: 300px;
transition: max-width 0.1s linear;
}

.tab.active.purple {
background-color: rgba(91, 55, 183, 0.2);
color: rgba(91, 55, 183, 1);
}

.tab.active.pink {
background-color: rgba(201, 55, 157, 0.2);
color: rgba(201, 55, 157, 1);
}

.tab.active.yellow {
background-color: rgba(230, 169, 25, 0.2);
color: rgba(230, 169, 25, 1);
}

.tab.active.teal {
background-color: rgba(28, 150, 162, 0.2);
color: rgba(28, 150, 162, 1);
}

@media (max-width: 450px) {
.tab-nav-container {
padding: 20px;
width: 350px;
}

.tab {
padding: 0 10px;
margin: 0;
}

.tab i {
font-size: 1em;
}
}

JS:

const tabs = document.querySelectorAll('.tab');

tabs.forEach(clickedTab => {
// Add onClick event listener on each tab
clickedTab.addEventListener('click', () => {
// Remove the active class from all the tabs (this acts as a "hard" reset)
tabs.forEach(tab => {
tab.classList.remove('active');
});

// Add the active class on the clicked tab
clickedTab.classList.add('active');
const clickedTabBGColor = getComputedStyle(clickedTab).getPropertyValue('color');
console.log(clickedTabBGColor);
banner.style.backgroundColor = clickedTabBGColor;
});
});

最佳答案

我更改了一些 CSS 属性,我认为它对我来说效果很好:

enter image description here

编辑:确保 FontAwesome 类是正确的,我使用了一些其他图标。

body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
transition: 0.4s linear;
text-align: center;
}

.tab-nav-container {
border-bottom-right-radius: 50px;
border-bottom-left-radius: 50px;
box-shadow: 0 3px 6px rgba(0, 0, 0, 0.16), 0 3px 6px rgba(0, 0, 0, 0.23);
display: flex;
justify-content: space-between;
padding: 30px;
width: 500px;
}

.tab {
background-color: transparent;
border-radius: 50px;
cursor: pointer;
display: flex;
transition: tab-nav-container 0.4s linear;
}

.tab i {
font-size: 2.2em;
}

.tab p {
font-weight: bold;
}

.tab.active p {
transition: max-width 0.1s linear;
}

.tab.active.purple {
background-color: rgba(91, 55, 183, 0.2);
color: rgba(91, 55, 183, 1);
}

.tab.active.pink {
background-color: rgba(201, 55, 157, 0.2);
color: rgba(201, 55, 157, 1);
}

.tab.active.yellow {
background-color: rgba(230, 169, 25, 0.2);
color: rgba(230, 169, 25, 1);
}

.tab.active.teal {
background-color: rgba(28, 150, 162, 0.2);
color: rgba(28, 150, 162, 1);
}

@media (max-width: 450px) {
.tab-nav-container {
padding: 20px;
width: 350px;
}

.tab {
padding: 0 10px;
margin: 0;
}

.tab i {
font-size: 1em;
}
}

关于javascript - 导航栏 : Tab icons disapear when I click on them,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56466162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com