我正在构建一个包含可过滤元素的网站。我基本上想更改过滤类中元素的不透明度。所以 100% 的不透明度元素具有 'active' 类,其他所有不具有 'active' 类的元素的不透明度为 50%。单击每个元素应淡入或淡出。这是代码。我正在为这个而挠头……
$(document).ready(function() {
$('#filters li a').click(function() {
// fetch the class of the clicked item
var ourClass = $(this).attr('class');
// reset the active class on all the buttons
$('#filters li').removeClass('active');
// update the active state on our clicked button
$(this).parent().addClass('active');
if(ourClass == 'all') {
// show all our items
$('#projects').children('li.two').show();
}
else {
// 50% Opacity of all elements that don't share ourClass
$('#projects').children('li:not(.' + ourClass + ')').fadeTo('slow', 0.5, function();
// !00% Opacity of all elements that do share ourClass
$('#projects').children('li.' + ourClass).fadeTo('slow', 1, function();
}
return false;
});
});
最佳答案
$('#filters li a').click(function() {
// fetch the class of the clicked item
var ourClass = $(this).attr('class');
// reset the active class on all the buttons
$('#filters li').removeClass('active');
// update the active state on our clicked button
$(this).parent().addClass('active');
if(ourClass == 'all') {
// show all our items
$('#projects').children('li.two').animate({opacity:1}, 400);
}
else {
// hide all elements that don't share ourClass
$('#projects').children('li:not(.' + ourClass + ')').animate({opacity:0.15}, 400);
// show all elements that do share ourClass
$('#projects').children('li.' + ourClass).animate({opacity:1}, 400);
}
return false;
});`
关于jquery - 使用 jquery 改变不透明度的数据过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15424638/