I have a title that is in the h3 tag. My code works using slideUp but slideDown looks so bad. I want the slideDown animations smooth like in slideUp. Basically, slideDown from the last array to the first array, and then the cycle continues (an infinite). Here is my code: HTML:
<div class="services-slide-up">
<h3 class="services-it-consulting">IT Consulting</h3>
<h3 class="services-web-dev">Web Development</h3>
<h3 class="services-mobile-app-dev">Mobile App Development</h3>
<h3 class="services-ui-ux-design">UI/UX Design</h3>
<h3 class="services-team-dev">Team Development</h3>
<h3 class="services-software-testing">Software Testing</h3>
</div>
jQuery
$(document).ready(function() {
var divs = [$(".services-it-consulting"), $(".services-web-dev"), $(".services-mobile-app-dev"), $(".services-ui-ux-design"), $(".services-team-dev"), $(".services-software-testing")];
var i = 0;
function animateDivs() {
divs[i].slideDown(1000, function() {
divs[i].slideUp(1000, function() {
i++;
if (i >= divs.length) {
i = 0;
}
animateDivs();
});
});
}
animateDivs();
});
2
Answers
You have semantic issues and I find the nested slide up / down event very confusing. What you can do is to place a setTimeout on each event and multiply it by a counter. This counter should be consistent also for the slideDown event. A solution could look like the following.
However be aware of memory allocation issues since the setTimeout is executed immediately. So this will create 8 timeout functions in total. Not a big deal at this point but can become an issue, so please be aware of that.
I have used to setTimeout to slide up and slide own to call function in looping.see in the code