1. <style>
  2. * {
  3. padding: 0;
  4. margin: 0;
  5. }
  6. img {
  7. width: 100%;
  8. height: 100%;
  9. display: block;
  10. }
  11. ul,
  12. ol {
  13. list-style: none;
  14. }
  15. .banner {
  16. width: 100%;
  17. height: 500px;
  18. position: relative;
  19. margin: 50px 0;
  20. }
  21. .banner>ul {
  22. width: 100%;
  23. height: 100%;
  24. position: relative;
  25. }
  26. .banner>ul>li {
  27. width: 100%;
  28. height: 100%;
  29. position: absolute;
  30. left: 0;
  31. top: 0;
  32. opacity: 0;
  33. transition: opacity .5s linear;
  34. }
  35. .banner>ul>li.active {
  36. opacity: 1;
  37. }
  38. .banner>ol {
  39. width: 200px;
  40. height: 30px;
  41. position: absolute;
  42. left: 30px;
  43. bottom: 30px;
  44. background: rgba(0, 0, 0, .5);
  45. display: flex;
  46. justify-content: space-around;
  47. align-items: center;
  48. border-radius: 15px;
  49. }
  50. .banner>ol>li {
  51. width: 20px;
  52. height: 20px;
  53. border-radius: 50%;
  54. background-color: #FFF;
  55. cursor: pointer;
  56. }
  57. .banner>ol>li.active {
  58. background-color: orange;
  59. }
  60. .banner>div {
  61. width: 40px;
  62. height: 60px;
  63. position: absolute;
  64. top: 50%;
  65. transform: translateY(-50%);
  66. background-color: rgba(0, 0, 0, .5);
  67. display: flex;
  68. justify-content: center;
  69. align-items: center;
  70. font-size: 30px;
  71. color: #FFF;
  72. cursor: pointer;
  73. }
  74. .banner>div.left {
  75. left: 0;
  76. }
  77. .banner>div.right {
  78. right: 0;
  79. }
  80. </style>
  81. </head>
  82. <body>
  83. <div class="banner">
  84. <ul class="imgbox">
  85. <li class="active"><img src="adv/adv2.jpg" alt=""></li>
  86. <li><img src="adv/adv3.jpg" alt=""></li>
  87. <li><img src="adv/adv4.jpg" alt=""></li>
  88. <li><img src="adv/adv5.jpg" alt=""></li>
  89. </ul>
  90. <ol>
  91. <li data-name="point" data-i="0" class="active"></li>
  92. <li data-name="point" data-i="1"></li>
  93. <li data-name="point" data-i="2"></li>
  94. <li data-name="point" data-i="3"></li>
  95. </ol>
  96. <div class="left">&lt;</div>
  97. <div class="right">&gt;</div>
  98. </div>
  99. <script>
  100. // 获取所有li
  101. var imgs = document.querySelectorAll('ul>li');
  102. var points = document.querySelectorAll('ol>li');
  103. var banner = document.querySelector('.banner')
  104. // 当前表示第几张,默认是0
  105. var index = 0;
  106. // 切换一张的函数
  107. // 参数为true,切换下一张. flase,切换上一张,为数字切换索引置顶的那一张
  108. function changeOne(type) {
  109. // 让当前这一张消失
  110. imgs[index].classList.remove('active');
  111. points[index].classList.remove('active');
  112. // 根据index传递进来的参数,来切换index的值
  113. if (type == true) {
  114. index++;
  115. } else if (type == false) {
  116. index--;
  117. } else {
  118. index = type;
  119. }
  120. // 判定index的边界值
  121. if (index >= imgs.length) {
  122. index = 0;
  123. }
  124. if (index < 0) {
  125. index = imgs.length - 1;
  126. }
  127. // 让改变后的这一张显示出来
  128. imgs[index].className = 'active';
  129. points[index].className = 'active';
  130. }
  131. banner.onclick = function (e) {
  132. if (e.target.className === 'left') {
  133. // console.log('right')
  134. changeOne(false)
  135. }
  136. if (e.target.className === 'right') {
  137. // console.log('left')
  138. changeOne(true)
  139. }
  140. if (e.target.dataset.name === 'point') {
  141. // console.log('focus')
  142. var i = e.target.dataset.i - 0;
  143. changeOne(i);
  144. }
  145. }
  146. // 自动切换
  147. var time = setInterval(function () {
  148. changeOne(true)
  149. }, 3000)
  150. </script>
  151. </body>

jquery实现

  1. <body>
  2. <div class="banner">
  3. <ul class="imgbox">
  4. <li class="active"><img src="images/01.png" alt=""></li>
  5. <li><img src="images/02.png" alt=""></li>
  6. <li><img src="images/03.png" alt=""></li>
  7. <li><img src="images/04.png" alt=""></li>
  8. <li><img src="images/05.png" alt=""></li>
  9. </ul>
  10. <ol>
  11. <li class="active"></li>
  12. <li ></li>
  13. <li ></li>
  14. <li ></li>
  15. <li></li>
  16. </ol>
  17. <button id="pre"></button>
  18. <button id="next"></button>
  19. </div>
  20. <script>
  21. var index = 0;
  22. // 点击右键,切换下一张
  23. $("#next").click(function(){
  24. index++;
  25. if(index>4){
  26. index = 0;
  27. }
  28. imgToggle(index,1000)
  29. })
  30. // 点击左键,切换下一张
  31. $("#pre").click(function(){
  32. index--;
  33. if(index<0){
  34. index = 4;
  35. }
  36. imgToggle(index,1000)
  37. })
  38. // 点击对应焦点,对应图片显示
  39. $('ol li').click(function(){
  40. var num = $(this).index();
  41. // 重置下标
  42. index = num;
  43. imgToggle(num,1000);
  44. })
  45. // 封装
  46. function imgToggle(index,speed){
  47. $('ol>li').eq(index).addClass('active').siblings().removeClass('active')
  48. $('.imgbox li').eq(index).fadeIn(speed).siblings().fadeOut(speed);
  49. }
  50. function slide(){
  51. time = setInterval(function(){
  52. // 调用右键的函数
  53. $("#next").click();
  54. },2000)
  55. }
  56. slide();
  57. </script>