如果您缓慢地执行操作(打开/关闭答案),一切运行正常,但如果您开始快速点击问题,一切都会搞砸! 这是我第一次尝试用 jQuery 写东西......你能帮我找出我做错了什么吗? 这是 jsfiddle 中的工作示例:http://jsfiddle.net/cp4Jd/3/
这是 jQuery 函数:
$('.expand').each(function(){
var reducedHeight = $(this).height();
$(this).css('height', 'auto');
var fullHeight = $(this).height();
$(this).height(reducedHeight);
$(this).data('reducedHeight', reducedHeight);
$(this).data('fullHeight', fullHeight);
}).click(function() {
$(this).animate({height: $(this).height() == $(this).data('reducedHeight') ? $(this).data('fullHeight') : $(this).data('reducedHeight')}, 500);
$('.container').animate({height: $(this).height() == $(this).data('reducedHeight') ?
($('.container').height() + $(this).data('fullHeight') - $(this).data('reducedHeight')) :
($('.container').height() - $(this).data('fullHeight') + $(this).data('reducedHeight'))}, 500);
($(this).height() == $(this).data('reducedHeight')) ?
($(this).find('.menu_ico').attr('src', 'img/menu_minus.png')) :
($(this).find('.menu_ico').attr('src', 'img/menu_plus.png'));
}
);
谢谢。
最佳答案
为了清楚起见,这里有一个更简单的版本。
HTML:
<div class="container">
<div class="expand">
<h2>This is a question?</h2>
<p class="par_menu_content blue">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur.</p>
</div>
<div class="expand">
<h2>Would you like to know the answer?</h2>
<p class="par_menu_content blue">Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit
in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur
sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.</p>
</div>
</div>
jQuery:
$(".par_menu_content").hide();
$(".expand h2").click(function(){
var $h2 = $(this);
$(".par_menu_content").slideUp();
$h2.next().stop(true).animate({height:"toggle"},500);
});
CSS:
.container {
width: 700px;
margin-top: 17px;
margin-left: 15px;
-webkit-border-radius: 26px;
-moz-border-radius: 26px;
border-radius: 26px;
}
.expand {
margin-left: 17px;
margin-right: 17px;
padding: 2px;
clear: both;
float:left;
}
.par_menu_content {
margin-right: 40px;
margin-left: 40px;
margin-top: 20px;
}
.blue {
color:#008597;
}
可以找到Fiddle here
关于jquery - 用 jQuery 编写的动态菜单中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15094691/