【发布时间】:2021-04-06 17:15:00
【问题描述】:
我正在尝试使用按钮过滤器制作一个投资组合,以便在单击按钮过滤器时将图像从黑白更改为正确类别的原始颜色。
我的图像已正确更改为黑白。 现在,当我单击它时,我设法使按钮变为活动状态,但单击后它并没有保持活动状态,我无法设法删除按钮类别图像上的黑白。
我不确定我是否正确执行了代码 sn-p,因为它显示错误。
var container = document.getElementById('filters-button-group');
container.addEventListener('click', event => {
var activeItem = container.querySelector('.button-selected');
if (activeItem !== null) {
activeItem.classList.remove('button-selected');
}
if (event.target === activeItem) {
return;
}
event.target.classList.add('button-selected');
$('#img').css('filter', 'none');
});
button {
/* BUTTON SET UP */
border: 4px solid green;
border-top: 0;
border-right: 0;
font-size: 10px;
text-decoration: none;
color: green;
display: block;
margin-bottom: 22px;
}
.button:active,
.button-selected {
/* SELECTED */
background: rgba(8, 140, 126, 50%);
border: 4px solid green;
}
#portfolio { /* POSITION TOTAL PORTFOLIO ZONE WITH BUTTONS */
text-align: center;
background: transparent;
position: absolute;
}
#portfolio img {
/* --- IMAGES BLACK AND WHITE --- */
filter: grayscale(100%) opacity(30%);
}
<section id="portfolio">
<div class="button-group filters-button-group">
<button class="button button-effect" data-filter=".A"><a href="#" class="opc-main-bg filter" >A</a></button>
<button class="button button-effect" data-filter=".B"><a href="#" class="opc-main-bg filter" >B</a></button>
<button class="button button-effect" data-filter=".C"><a href="#" class="opc-main-bg filter" >C</a></button>
<button class="button button-selected" data-filter="*"><a href="#" class="selected opc-main-bg filter">ALL</a></button>
</div>
<div
<div class="A"><img src="http://fakeimg.pl/365x365/ff0000/" width="50%" height="auto">
<div class="B"><img src="http://fakeimg.pl/365x365/ff0000/" width="50%" height="auto">
<div class="C"><img src="http://fakeimg.pl/365x365/ff0000/" width="50%" height="auto">
</section>
【问题讨论】:
-
filters-button-group是 class 而不是 Id。将document.getElementById('filters-button-group');替换为document.getElementsByClassName('filters-button-group')[0];
标签: javascript html css image portfolio