0%
前言
jQuery实现淡入淡出轮播图
源代码
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style>
* { margin: 0; padding: 0; }
body { background: #EEE; }
.container { position: relative; width: 600px; height: 400px; margin: 30px auto; border: 1px solid black; overflow: hidden; }
ul { position: absolute; width: 100%; height: 100%; list-style: none; }
ul li { display: none; }
li.cur { display: block; }
img { width: 600px; height: 400px; }
.lbtn { position: absolute; width: 40px; height: 20px; left: 0; top: 40%; cursor: pointer; }
.rbtn { position: absolute; width: 40px; height: 20px; right: 0; top: 40%; }
</style> <script src="js/jquery.min.js"></script> </head> <body>
<div class="container"> <ul> <li class="cur"><img src="img/WallPaper01.jpg" alt=""></li> <li><img src="img/WallPaper02.jpg" alt=""></li> <li><img src="img/WallPaper03.jpg" alt=""></li> <li><img src="img/WallPaper04.jpg" alt=""></li> </ul> <button class="lbtn"><</button> <button class="rbtn">></button> </div>
</body> </html> <script>
let idx = 0;
$(".rbtn").click(function(){ $(`li:eq(${idx})`).fadeOut(function(){ idx += 1; if (idx > 3) { idx = 0; } $(`li:eq(${idx})`).fadeIn(); }); });
$(".lbtn").click(function(){ $(`li:eq(${idx})`).fadeOut(function(){ idx -= 1; if (idx < 0) { idx = 3; } $(`li:eq(${idx})`).fadeIn(); }); });
</script>
|
完成
参考文献
哔哩哔哩——web前端小清风