我有一张幻灯片,其中显示了 4 个属性图像及其描述,这些图像附加在 $.each 循环中。但是现在,我只有 8 个结果要显示在 slider 中。
所以问题是,当结果成功显示时,幻灯片将变为空白,但我想在完成生成的幻灯片后停止这些幻灯片。
这是我的 ajax 响应
jQuery.ajax({
type:'POST',
url:'<?php echo base_url("site/landing/get_work_places"); ?>',
data:{ city_name:city},
dataType: 'json',
success:function(data)
{
var ParsedObject = JSON.stringify(data);
var json = $.parseJSON(ParsedObject);
if (json=="")
{
$('#work_hider').hide();
// alert("no results found");
}else
{
$.each(json, function(key, data)
{
// All the variables from the database containing the post data.
var product_id=data.product_id;
var product_name=data.product_title;
var product_image=data.product_image;
var work_price = data.price_perhour;
var work_address = data.address;
$("#work-carousel").append('<li class="span3"><div class="thumbnail"><a href="<?php echo base_url('rental'); ?>/'+product_id+'"><img src="'+product_image+'" alt=""></a></div><div class="caption"><h2>'+work_price+'</h2></div><div class="caption"><h4>'+product_name+'</h4> <p>'+work_address+'</p></div></li>');
});
for (i = 0; i <= $('.span3').length / 4; i++) {
$('.span3').slice(i * 4, (i + 1) * 4).wrapAll('<div class="item"><ul id="work_property">');
}
}
}
});
// slider ajax ends
});
这是 slider HTML slider 代码
<div class="work_carousel slide" id="myCarousel">
<div class="carousel-inner" id="work-carousel">
<div class="item active">
<ul class="thumbnails" id="work_property">
</ul>
</div>
</div>
<div class="control-box" style="margin-bottom: 2em;">
<a data-slide="prev" href="#myCarousel" class="carousel-control left">‹</a>
<a data-slide="next" href="#myCarousel" class="carousel-control right">›</a>
</div>
</div>
</div>
</div>
</div>
这是 slider 及其结果的图片
这是带有完成结果的 slider 图片
注意:1 张幻灯片 = 4 张结果图像,我在 SQL 查询中有 8 个结果的固定限制,所以我只想运行 2 张幻灯片然后停止这些幻灯片。
请建议我如何在完成结果后停止这些幻灯片,谢谢 :)
最佳答案
var totalItems = $('.item').length;
var currentIndex = $('div.active').index() + 1;
$('.num').html(''+currentIndex+'/'+totalItems+'');
$('.work_carousel').carousel({
interval: 2000
});
$('.work_carousel').on('slide.bs.carousel', function() {
currentIndex = $('div.active').index() + 1;
if(currentIndex >= 3){
$('#work_property').attr("id")="newid";
}
});
关于javascript - 结果完成后如何在 $.each 循环中停止 slider 滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43606766/