<div class="slider"> <div class="slides"> <img src="image1.jpg"> <img src="image2.jpg"> <img src="image3.jpg"> </div> <div class="thumbnails"> <img src="image1.jpg"> <img src="image2.jpg"> <img src="image3.jpg"> </div> </div>
.slider { position: relative; } .slides { width: 100%; height: 300px; overflow: hidden; } .slides img { width: 100%; height: auto; } .thumbnails { width: 100%; height: 100px; overflow-x: auto; white-space: nowrap; } .thumbnails img { width: 100px; height: auto; display: inline-block; margin-right: 10px; }
$(document).ready(function() { var slides = $('.slides img'); var thumbnails = $('.thumbnails img'); var currentIndex = 0; var interval; function showSlide(index) { slides.hide(); slides.eq(index).show(); thumbnails.removeClass('active'); thumbnails.eq(index).addClass('active'); } function startInterval() { interval = setInterval(function() { currentIndex = (currentIndex + 1) % slides.length; showSlide(currentIndex); }, 2000); } function stopInterval() { clearInterval(interval); } thumbnails.click(function() { var index = thumbnails.index($(this)); currentIndex = index; showSlide(index); }); $('.slider').hover(function() { stopInterval(); }, function() { startInterval(); }); showSlide(currentIndex); startInterval(); });
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com