非博主原创,学习过程中,记录一下,代码依赖jquery。
HTML代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <div class="slide-container"> <div class="slide-item fade"> <img src="images/img_fjords_wide.jpg" /> <p class="slide-text">文本信息1</p> </div> <div class="slide-item fade"> <img src="images/img_mountains_wide.jpg" /> <p class="slide-text">文本信息2</p> </div> <div class="slide-item fade"> <img src="images/img_nature_wide.jpg" /> <p class="slide-text">文本信息3</p> </div> <a href="javascript:;" class="prev"><</a> <a href="javascript:;" class="next">></a> <div class="slide-num"></div> </div>
|
CSS代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| * { margin: 0; padding: 0 }
body { font-family: verdana, sans-serif; }
.slide-container { width: 1000px; position: relative; margin: 50px auto; }
.slide-item { position: relative; }
.slide-text { position: absolute; bottom: 8px; width: 100%; color: #fff; font-size: 16px; text-align: center; }
.prev, .next { position: absolute; top: 50%; margin-top: -30px; cursor: pointer; color: #fff; font-size: 30px; font-weight: bold; font-family: "宋体"; transition: all .6s ease; z-index: 10; text-decoration: none; }
.prev { left: 10px; }
.next { right: 10px; }
.slide-num { margin-top: 10px; text-align: center; }
.dot { margin: 0 4px; display: inline-block; width: 14px; height: 14px; cursor: pointer; background: #bbb; border-radius: 50%; transition: all .3s ease; }
.dot:hover, .dot.active { background: #717171; }
.fade { animation-name: fade; animation-duration: 1.5s; }
@keyframes fade { from { opacity: .4 } to { opacity: 1 } }
|
JS代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| var sIndex = 1; var timer = null; var $prev = $(".prev"), $next = $(".next"); showSlides(sIndex);
function showSlides(n) { var $slideItem = $(".slide-item"); var $slideNum = $(".slide-num"); var str = ''; if ($slideNum.find("span").length == 0) { for (var i = 0; i < $(".slide-item").length; i++) { str += '<span class="dot" onclick="currentSlide(' + (i + 1) + ')"></span>'; } $slideNum.append(str); } if (n > $slideItem.length) { sIndex = 1; } if (n < 1) { sIndex = $slideItem.length; } $slideItem.hide(); $slideItem.eq(sIndex - 1).show(); $slideNum.find("span").eq(sIndex - 1).addClass("active").siblings().removeClass("active") }
function plusSlides(n) { sIndex += n; showSlides(sIndex); }
function currentSlide(n) { sIndex = n; showSlides(sIndex); }
$prev.click(function () { plusSlides(-1); })
$next.click(function () { plusSlides(1); })
function autoShowSlide() { sIndex++; showSlides(sIndex); } timer = setInterval(autoShowSlide, 2000);
$(".slide-container").hover(function () { clearInterval(timer); }, function () { timer = setInterval(autoShowSlide, 2000); })
|