尝试这个:
// BTW, if you're using newer jQuery and no other library that namespaces the $ symbol,
// you can replace that long document.ready line with the following:
// $(function() {
jQuery(document).ready(function(e){
// call just one click function
$('.plussign').click(function(e) {
// using toggle function will allow you to operate between the two clicks
$(this).toggle(function(e) { // click odd
// If both elements are recieving the same action, then call them in the same line,
// simply seperate with a comma
$('#bigdiv, .plussign').stop().animate({height : '400px'}, 200);
// the .stop() function will stop any previous animation and continue with the new,
// this is usefull for FAST CLICKING
},
function(e) { // click even
$('#bigdiv, .plussign').stop().animate({height : '80px'}, 200);
});
});
})
然而
我建议使用基类和切换类并使用 jQuery 的 .toggleClass 功能,因为它的编码要少得多并且更容易控制,因为您可以简单地调整 css。例如:
CSS
.open {
height: 400px;
}
.closed {
height: 80px;
}
脚本
$(function() {
// if you dont add the closed class to the html element itself on the HTML,
// you can add it here before setting your click event
$('#bigdiv, .plussign').addClass("closed");
// then set your click event with the toggleClass function
$('.plussign').click(function(e) {
$('#bigdiv, .plussign').toggleClass("open closed");
// if you add jQueryUI to the mix you can control the speed in time as follows
// $('#bigdiv, .plussign').toggleClass("open closed", 10000);
});
})