有人可以帮助我使用切换方法吗?请看下面我的代码
我的问题是,通过按下图像切换会添加活动类,从而为其添加一些 CSS 属性,但是通过连续按下同一图像,它不会从中删除活动类。
我无法删除 removeActiveClass 函数,因为它必须通过 NodeList 并检查哪一个具有活动类并将其删除,因此仅将活动添加到单击的图像中。
const panels = document.querySelectorAll('.panel');
panels.forEach((panel) => {
panel.addEventListener('click', () => {
removeActiveClasses();
panel.classList.toggle('active');
});
});
function removeActiveClasses() {
panels.forEach(panel => {
panel.classList.remove('active');
});
}
CSS 代码
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
* {
box-sizing: border-box;
}
body {
font-family: 'Muli', sans-serif;
margin: 0;
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
overflow: hidden;
}
.container {
display: flex;
width: 90vw;
}
.panel {
background-size: cover;
background-position: center;
background-repeat: no-repeat;
height: 80vh;
border-radius: 50px;
color: white;
cursor: pointer;
flex: 0.5;
margin: 10px;
position: relative;
transition: flex 0.7s ease-in;
}
.panel h3 {
font-size: 24px;
position: absolute;
bottom: 20px;
left: 20px;
margin: 0;
opacity: 0;
}
.panel.active {
flex: 5;
}
.panel.active h3 {
opacity: 1;
transition: opacity 0.3s ease-in 0.6s;
}
@media(max-width: 768px) {
.container {
width: 100vw;
}
.panel:nth-of-type(4),
.panel:nth-of-type(5) {
display: none;
}
}
提前致谢 :)