css

  1. *{margin:0;padding:0}
  2. .content{
  3. cursor: pointer;
  4. position: relative;
  5. width:600px;height:400px;
  6. margin-left: auto;margin-right: auto;
  7. border:1px solid #333;
  8. }
  9. #list{
  10. position: absolute;
  11. width:600px;
  12. height:400px;
  13. }
  14. #list img,#prev,#next,#btns{
  15. position: absolute;
  16. }
  17. #list img:not(:first-child){
  18. display: none;
  19. }
  20. #prev,#next{
  21. cursor: pointer;
  22. top:50%;transform: translateY(-50%);
  23. z-index: 100;width:40px;height:70px;
  24. background:url("../images/icon-slides.png");
  25. border:none;
  26. }
  27. #prev{
  28. background-position-x:80px;
  29. }
  30. #prev:hover{
  31. background-position: 0;
  32. }
  33. #next:hover{background-position-x: -43px}
  34. #next{
  35. right:0;
  36. background-position-x:-125px;
  37. }
  38. #btns{z-index: 101;
  39. transform: translateX(-50%);
  40. bottom: 20px;left:50%;}
  41. #btns .current{
  42. background:orangered;
  43. }
  44. #btns>span{
  45. cursor: pointer;
  46. width:20px;
  47. height:20px;
  48. display: inline-block;
  49. border-radius: 50%;
  50. border:1px solid #fff;
  51. background-color:rgba(44,44,44,.3);
  52. }

html

  1. <div class="content">
  2. <div id="list">
  3. <img src="images/01.png" alt="">
  4. <img src="images/02.png" alt="">
  5. <img src="images/03.png" alt="">
  6. <img src="images/04.png" alt="">
  7. <img src="images/05.png" alt="">
  8. </div>
  9. <button id="prev"></button>
  10. <button id="next"></button>
  11. <div id="btns">
  12. <span class="current"></span>
  13. <span></span>
  14. <span></span>
  15. <span></span>
  16. <span></span>
  17. </div>
  18. </div>

jqurey

  1. <script>
  2. $(function () {
  3. var index = 0;
  4. var timer;
  5. // 1.点击next
  6. $("#next").click(function () {
  7. index++;
  8. if (index > 4) {
  9. index = 0;
  10. }
  11. animate(index);
  12. })
  13. // 2.点击prev
  14. $("#prev").click(function () {
  15. index--;
  16. console.log(index);
  17. if (index < 0) {
  18. index = 4;
  19. }
  20. animate(index);
  21. })
  22. // 3.焦点随左右按钮变化
  23. function animate(index) {
  24. $("#list img").eq(index).fadeIn(300).siblings().fadeOut(300);
  25. $("#btns span").eq(index).addClass("current").siblings().removeClass("current");
  26. }
  27. // 4.点击按钮,让对应的图片出现
  28. $("#btns span").click(function () {
  29. $(this).addClass("current").siblings().removeClass("current");
  30. index = $(this).index();
  31. $("#list img").eq(index).fadeIn(300).siblings().fadeOut(300);
  32. })
  33. //5.自动播放
  34. function play() {
  35. timer = setInterval(function () {
  36. $("#next").click()
  37. }, 1000)
  38. }
  39. function stop() {
  40. clearInterval(timer)
  41. }
  42. $(".content").mouseover(function(){
  43. stop();
  44. })
  45. $(".content").mouseout(function(){
  46. play();
  47. })
  48. play();
  49. })
  50. </script>