2012-08-09 72 views
0

我會改變div CSS樣式,當點擊時,然後改變到它的主要狀態,當點擊另一個。 我用這個代碼,但它只是重新改變它,當點擊另一個div 這裏的代碼正在嘗試:如何更改div風格,當點擊到另一個

$('.mydiv').click(
function() { 

    $(this).stop().animate({ 
    width:'960px', 
    backgroundColor : '#000' 
}, 500); 
    }, function() { 
    $(this).stop().animate({width:'300px', backgroundColor : "green"}, 300); 

}); 

回答

2

你可能尋找toggle()功能:

$('.mydiv').toggle(function() { 
    $(this).stop().animate({ 
     width: '960px', 
     backgroundColor: '#000' 
    }, 500); 
}, function() { 
    $(this).stop().animate({ 
     width: '300px', 
     backgroundColor: "green" 
    }, 300); 
});​ 

而你需要jQuery UI動畫顏色?

FIDDLE

編輯:

你可能還在尋找toggle()功能,但動畫另一個DIV當點擊一個DIV,停止使用this關鍵字和目標的div你」重新嘗試動畫:

$('#someDiv').toggle(function() { 
    $('.mydiv').stop().animate({ 
     width: '960px', 
     backgroundColor: '#000' 
    }, 500); 
}, function() { 
    $('.mydiv').stop().animate({ 
     width: '300px', 
     backgroundColor: "green" 
    }, 300); 

});​ 

FIDDLE

+0

或彩色動畫插件 – PitaJ 2012-08-09 23:11:06

+0

我需要的是改變點擊的div寬度:300像素,當我在另一個DIV – 2012-08-10 00:02:13

+0

@AmineKacem點擊 - 很簡單,更新了我的答案嗎? – adeneo 2012-08-10 00:06:29

0

試試這個:

$('.mydiv').click(function() { 

    $(this).stop().animate({ 
     width:'960px', 
     backgroundColor : '#000' 
    }, 500, function() { 
     $(this).stop().animate({width:'300px', backgroundColor : "green"}, 300); 
    }); 
}); 
相關問題