我在jQuery中写过一些内容,但我不确定它是否是一个优雅的解决方案。每次单击都需要检查DOM的类/并添加和删除类。任何反馈或建议将不胜感激。想法是用户只能选择3个图像。感谢
Jquery的:
$('.thumbnail-gallery-items li').click(function () {
var selectionCount = $('.selected').length;
$(this).toggleClass('selected');
if (selectionCount == 2 ) {
$('.thumbnail-gallery-items li').addClass('disabled');
} else {
$('.thumbnail-gallery-items li').removeClass('disabled');
}});
CSS:
.thumbnail-gallery-items li.selected:after {
font-family: 'Material Icons';
content: '\E876';
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
background: rgba(155, 89, 182, 0.2);
z-index: 1;
color: #fff;
font-size: 32px;
padding: 5px;}
.disabled:not(.selected) {
pointer-events: none}