Reputation: 4473
Everything runs ok if you do the actions slowly (opening/closing answers), but if you start clicking fast on a question everything is messed up! It's the first time I try to write something with jQuery...Could you help me to find out what I did wrong? Here is the working example in jsfiddle: http://jsfiddle.net/cp4Jd/3/
Here is the jQuery function:
$('.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'));
}
);
Thank you.
Upvotes: 0
Views: 87
Reputation: 2519
Here's a much simpler version for clarity.
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 can be found here
Upvotes: 1
Reputation: 10216
You cannot put a <p>
inside another <p>
.
I always do this kind of accordions like this:
<outer wrapper>
<item>
<item content wrapper>
<content>
</item content wrapper>
</item>
<item>
<item content wrapper>
<content>
</item content wrapper>
</item>
<item>
<item content wrapper>
<content>
</item content wrapper>
</item>
</outer wrapper>
you then give the <item>
a fix height and animate the items' height to it's <item content wrapper>
's height and back its original, recuded height on second click (havent tested):
$('.expand').click(function() {
if ($(this).height() == $(this).data('reducedHeight')) {
$(this).stop().animate({height: $(this).children().height() + 50 + 'px'}, slow);
}
else {
$(this).stop().animate({height: $(this).data('reducedHeight') + 'px'}, slow);
}
});
Upvotes: 1