2013-03-15 66 views
0

我正在構建一個可過濾項目的網站。我基本上想要改變過濾類中元素的不透明度。因此,100%不透明元素具有「主動」類別,50%不透明度不具有「主動」類別。點擊每個元素應該淡入淡出。這是代碼。我抓我的頭,這一個...使用jquery更改不透明度的數據過濾器

$(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; 
    }); 
}); 
+0

究竟什麼是你面對了問題? – exexzian 2013-03-15 04:19:28

+0

我意識到這不是跨瀏覽器,但你可以考慮只使用toggleClass(),並使用-webkit轉換:不透明度0.5s緩解;不透明度:0.5 ...也考慮使用hasClass('all'),而不是attr('class') – captainclam 2013-03-15 04:19:57

+0

基本上我可以得到這個作爲一個表演和隱藏。但是我想要做的就是保留所有元素,並且如果他們沒有「主動」類,則淡化到50%,如果他們這樣做,就會褪去100%。合理? – user1337537 2013-03-15 04:35:34

回答

1
$('#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; 
});` 
+0

這是我的工作... – user1337537 2013-03-18 00:50:53