我在计算 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>
任何建议将不胜感激。我也想知道我是否接近解决这个问题。谢谢。