修正:图片使用background-image: url();

    .item:nth-child(1){ background-image: url(); }

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>轮播图</title>
    7. <style>
    8. body{
    9. background-color: rgba(0, 110, 255, 0.1);
    10. }
    11. .contanier{
    12. width: 500px;
    13. height: 300px;
    14. position: relative;
    15. margin-left: 370px;
    16. }
    17. .item{
    18. width: 500px;
    19. height: 300px;
    20. position: absolute;
    21. opacity: 0;
    22. }
    23. img{
    24. width: 500px;
    25. height: 300px;
    26. }
    27. .active{
    28. z-index: 1;
    29. opacity: 1;
    30. transition: opcacity 500ms linear;
    31. }
    32. /* 按钮 */
    33. .pre{
    34. content: '<';
    35. display: block;
    36. position: absolute;
    37. top: 90px;
    38. left: -50px;
    39. height: 100px;
    40. line-height: 100px;
    41. text-align: center;
    42. width: 60px;
    43. font-size: 60px;
    44. font-weight:400;
    45. color:#ffffff;
    46. cursor: pointer;
    47. z-index: 6;
    48. }
    49. .next{
    50. content: '>';
    51. display: block;
    52. position: absolute;
    53. top: 90px;
    54. right: -50px;
    55. height: 100px;
    56. line-height: 100px;
    57. text-align: center;
    58. width: 60px;
    59. font-size: 60px;
    60. font-weight:400;
    61. color:#ffffff;
    62. cursor: pointer;
    63. z-index: 6;
    64. }
    65. </style>
    66. </head>
    67. <body>
    68. <div class="contanier">
    69. <div class="item active" data-index=1><img src="img/85-1.jpg" alt=""></div>
    70. <div class="item" data-index=2><img src="img/小王.jpg" alt=""></div>
    71. <div class="item" data-index=3><img src="img/85-2.jpg" alt=""></div>
    72. <div class="item" data-index=4><img src="img/85-3.jpg" alt=""></div>
    73. <div class="item" data-index=5><img src="img/85.jpg" alt=""></div>
    74. <!-- 小按钮可以点击前后 -->
    75. <div class="pre">&lt;</div>
    76. <div class="next">&gt;</div>
    77. </div>
    78. <script src="../jquery.js"></script>
    79. <script>
    80. //索引
    81. function next(index){
    82. index = Number(index);
    83. if($(".item").length===index){
    84. return 1;
    85. }
    86. return index+1;
    87. }
    88. //去下一页的方法
    89. function goNext(){
    90. //找到有active的元素
    91. var active=$('.active');
    92. //找到他的下标
    93. var index=active.attr('data-index');
    94. //删除他的active样式
    95. active.removeClass('active');
    96. //给下一个元素添加active样式
    97. $(".item[data-index="+next(index)+"]").addClass('active');
    98. }
    99. //调用方法
    100. $('.next').click(function(){
    101. goNext();
    102. });
    103. //去上一页索引
    104. function pre(index){
    105. index = Number(index);
    106. if(index===1){
    107. return $('.item').length;
    108. }
    109. return index-1;
    110. }
    111. //去上一页的方法
    112. function goPre(){
    113. //找到有active的元素
    114. var active=$('.active');
    115. //找到他的下标
    116. var index=active.attr('data-index');
    117. //删除他的active样式
    118. active.removeClass('active');
    119. //给下一个元素添加active样式
    120. $(".item[data-index="+pre(index)+"]").addClass('active');
    121. }
    122. //调用方法
    123. $('.pre').click(function(){
    124. goPre();
    125. });
    126. //周期轮播的方法
    127. setInterval(goNext,3000);
    128. </script>
    129. </body>
    130. </html>