更新されたソリューション
jQuery 1.8で非推奨になり、1.9で削除されたため.toggle
、頼りになるソリューションでは、何をすべきかを決定するために、クリックを手動で追跡する必要があります。トグルの一般的なドロップイン置換は次のようになります。
var handlers = [
// on first click:
function() {
jQuery('.div1').animate({width: 'toggle'});
jQuery('.div2').animate({width: '50%'});
},
// on second click:
function() {
jQuery('.div1').animate({width: 'toggle'});
jQuery('.div2').animate({width: '100%'});
}
// ...as many more as you want here
];
var counter = 0;
$(".button").click(function() {
// call the appropriate function preserving this and any arguments
handlers[counter++].apply(this, Array.prototype.slice.apply(arguments));
// "wrap around" when all handlers have been called
counter %= handlers.length;
});
元の回答
あなたは単に使用することができます.toggle
:
jQuery('.button').toggle(function() {
jQuery('.div1').animate({width: 'toggle'});
jQuery('.div2').animate({width: '50%'});
}, function() {
jQuery('.div1').animate({width: 'toggle'});
jQuery('.div2').animate({width: '100%'});
});
クリックするたびに、の表示が.div1
完全に切り替わり、幅が.div2
50%から100%の間で切り替わります。
最初に非表示にした後で再度表示したくない場合は.div1
、単に
jQuery('.button').toggle(function() {
jQuery('.div1').animate({width: 'hide'});
jQuery('.div2').animate({width: '50%'});
}, function() {
jQuery('.div2').animate({width: '100%'});
});