我遇到的問題是計算div中的圖像數量。計算Div中的圖像
例如1/4,2/4,3/4,4/4
這我試圖搞清楚
//count image
var currentNum = $(this).iniShow() + 1;
var count = $(this).closest('.slideshow').find('img:first').length;
var item = $(this).closest('.slideshow').attr('id');
$('.' + item).html(currentNum + ' of ' + count);
這裏的小碼是我的html代碼至今:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"> </script>
</head>
<style>
.slideshow img { display: none; cursor: pointer; }
</style>
<script type="text/javascript">
jQuery(function($){
//previous slide
$('.slideshow .prev').click(function() {
prevSlide($(this).closest('.slideshow').find('.slides'));
});
//next slide
$('.slideshow .next, .slideshow img,').click(function() {
nextSlide($(this).closest('.slideshow').find('.slides'));
});
//initialize show
iniShow();
function iniShow() {
// show first slide with caption
$('.slideshow').each(function() {
showSlide($(this).find('.slides'));
});
}
// move previous slide
function prevSlide($slides) {
$slides.find('img:last').prependTo($slides);
showSlide($slides);
}
// move next slide
function nextSlide($slides) {
$slides.find('img:first').appendTo($slides);
showSlide($slides);
}
// show slide with caption
function showSlide($slides) {
var $nextSlide = $slides.find('img:first');
//hide (reset) all slides
$slides.find('img').hide();
//fade in next slide
$nextSlide.fadeIn(500);
//show caption
$('#caption').text($nextSlide[0].alt);
//count image
var currentNum = $(this).iniShow() + 1;
var count = $(this).closest('.slideshow').find('img').length;
$('#count').html(currentNum + ' of ' + count);
}
});
</script>
<body>
<div class="slideshow">
<div class="slides">
<img src="image/1a.jpg" width="500" height="500" alt="Caption 1 Image is ..." />
<img src="image/2a.jpg" width="500" height="500" alt="Caption 2 Image is ..." />
<img src="image/3a.jpg" width="500" height="500" alt="Caption 3 Image is ..." />
<img src="image/4a.jpg" width="500" height="500" alt="Caption 4 Image is ..." />
</div>
<p id="caption"></p>/<p id="count"></p>
<div class="controls">
<a class="prev" href="javascript:void(0);">Previous</a> -
<a class="next" href="javascript:void(0);">Next</a>
</div>
</div>
</body>
</html>
任何建議,將不勝感激。我也想知道我接近解決這個問題。謝謝。
使用了':first',計數將始終爲0或1 –
@Kevin B,感謝您的回覆和信息 – LightM1