У меня здесь проблема http://test10.bitballoon.com/ У меня есть анимация (показать / скрыть) текст. Первый текст имеет класс «активный». анимация показывает этот текст, затем удаляет класс «активный» и добавляет этот класс к следующему тексту и снова выполняет обратный вызов функции. функция также проверяет, имеет ли последний текст класс «активный», затем удаляет и добавляет класс «активный» к первому текст и снова скрыть весь текст и обратный вызов. вот как это работает. в первый раз все работает нормально, когда анимация завершает обратный вызов снова и анимирует первый текст хорошо, затем функция добавляет «активный» к нескольким текстам, затем весь текст и разрыв функции. я не знаю, почему это произошло, я пытался использовать setInterval, но та же проблема
function animateText() {
"use strict";
if ($(".text-container .active").hasClass("left")) {
$(".text-container .active").addClass("active");
$(".text-container .active").delay(1000).animate({
marginLeft: 0,
opacity: 1
}, 700, function() {
if ($(".text-container .text:last").hasClass("active")) {
$(".text-container .text:last").removeClass("active")
$(".text-container .text").animate({
opacity: 0
}, 700, function() {
$(".text-container .text:first").addClass("active");
animateText();
})
}
$(".text-container .active").removeClass("active").next().addClass("active");
animateText();
});
} else if ($(".text-container .active").hasClass("right")) {
$(".text-container .active").addClass("active");
$(".text-container .active").delay(1000).animate({
marginRight: "5px",
opacity: 1
}, 700, function() {
if ($(".text-container .text:last").hasClass("active")) {
$(".text-container .text:last").removeClass("active")
$(".text-container .text").animate({
opacity: 0
}, 700, function() {
$(".text-container .text:first").addClass("active");
animateText();
})
}
$(".text-container .active").removeClass("active").next().addClass("active");
animateText();
});
}
}<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "text-container">
<div class = "text left active">
<p>Hello World</p>
</div>
<div class = "text left">
<p>Hello World2</p>
</div>
<div class = "text right">
<p>Hello World3</p>
</div>
<div class = "text right">
<p>Hello World4</p>
</div>
<div class = "text left">
<p>Hello World5</p>
</div>
<div class = "text right">
<p>Hello World6</p>
</div>
</div>оставленный класс добавлен перед функцией анимации .. вы можете проверить test10.bitballoon.com



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Что касается этой анимации, класс .active бесполезен. Обратите внимание, что я добавил немного CSS, чтобы разместить его во фрагменте, поэтому дайте мне знать, если это поможет.
$(document).ready(function() {
animateText($(".text-container .active"));
});
function animateText($this) {
var leftParams = {'left': 0, opacity: 1};
var rightParams = {'right': 0, opacity: 1};
$this.stop(true).animate(
($this.hasClass('left') ? leftParams : rightParams),
1000, function() {
if ($this.next().presence())
animateText($this.next());
else
startOver();
}
);
}
function startOver() {
var selector = $(".text-container .text");
var next = $(".text-container > .active");
selector.animate({
opacity: 0
}, 200, function() {
selector.filter('.left').css('left', -200);
selector.filter('.right').css('right', -200);
animateText(next);
});
}
$.fn.presence = function() {
return this.length !== 0 && this;
};.text-container {
display: inline-block;
width: 260.4px;
height: 419px;
padding-top: 10px;
padding-left: 5px;
overflow: hidden;
}
.text-container .text {
max-width: 225px;
float: left;
padding: 8px 9px;
margin-bottom: 6px;
border-radius: 20px;
text-align: left;
background-color: rgba(224,230,237,.5);
clear: both;
opacity: 0;
position: relative;
}
.text-container .left {
float: left;
left: -200px;
}
.text-container .right {
float: right;
right: -200px;
background-color: #1bb45e;
}<script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "text-container">
<div class = "text left active">
<p>Hello World</p>
</div>
<div class = "text left">
<p>Hello World2</p>
</div>
<div class = "text right">
<p>Hello World3</p>
</div>
<div class = "text right">
<p>Hello World4</p>
</div>
<div class = "text left">
<p>Hello World5</p>
</div>
<div class = "text right">
<p>Hello World6</p>
</div>
</div>Большое вам спасибо, я очень ценю ваши усилия.
if ($(".text-container .active").hasClass("left")) {...Ни один из дочерних div не имеет классаleft. Когда это должно быть правдой?