@z_u_q

Как правильно отобразить слайды,если выполняется ajax запрос owl carousel?

<div class="carousel-wrap">
	<div class="owl-carousel">
			
	</div>
</div>


$(function() {
     $('.owl-carousel').owlCarousel();
});


$.ajax('/test.php').done(function (response) {
	if (typeof response === 'string' && response.length > 0) {
		$('.owl-carousel').html(response);
	} else {
		console.log(response);
	}
}).fail(function (jqXHR, textStatus, errorThrown) {
	console.log(textStatus);
	console.log(errorThrown);
});


test.php отдает мне:
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>
<div class="item"><img src="http://placehold.it/150x150"></div>


Собственно вопрос, как вернуть поведение слайдера? Тут я сделал просто вставку в .owl-carousel $('.owl-carousel').html(response);
  • Вопрос задан
  • 728 просмотров
Решения вопроса 1
0xD34F
@0xD34F
$(response).filter('.item').each(function() {
  $owl.trigger('add.owl.carousel', this);
});
$owl.trigger('refresh.owl.carousel');
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
var owl = $('.owl-carousel');
owl.owlCarousel();

$.ajax('/test.php').done(function (response) {
  if (typeof response === 'string' && response.length > 0) {
    $('.owl-carousel').html(response);
    owl.trigger('refresh.owl.carousel');
  } else {
    console.log(response);
  }
}).fail(function (jqXHR, textStatus, errorThrown) {
  console.log(textStatus);
  console.log(errorThrown);
});


или же инициализировать слайдер $('.owl-carousel').owlCarousel(); только при успешном запросе ajax, а не ДО него
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы