练习4_切换图片.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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>Document</title>
  7. <style>
  8. /* css reset */
  9. body{
  10. margin: 0;
  11. }
  12. li{
  13. list-style: none;
  14. }
  15. ul{
  16. padding: 0;
  17. margin:0;
  18. }
  19. .clearfix::after{
  20. content: "";
  21. display: block;
  22. clear: both;
  23. }
  24. .swiper-content{
  25. width: 600px;
  26. border:1px solid black;
  27. margin: 200px auto;
  28. }
  29. .swiper-content .big-img{
  30. border-bottom: 1px solid black;
  31. text-align: center;
  32. }
  33. .swiper-content .big-img img{
  34. width: 300px;
  35. }
  36. .swiper-content .small-img li{
  37. float: left;
  38. width: 149px;
  39. text-align: center;
  40. border-right: 1px solid black;
  41. }
  42. .swiper-content .small-img li:last-child{
  43. border:0;
  44. }
  45. .swiper-content .small-img img{
  46. width: 100px;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="swiper-content">
  52. <div class="big-img">
  53. <img class="top-img" src="./image/img1.png" alt="img">
  54. </div>
  55. <div class="small-img">
  56. <ul class="clearfix">
  57. <li>
  58. <img class="bottom-img" src="./image/img1.png" alt="img">
  59. </li>
  60. <li>
  61. <img class="bottom-img" src="./image/img2.png" alt="img">
  62. </li>
  63. <li>
  64. <img class="bottom-img" src="./image/img3.png" alt="img">
  65. </li>
  66. <li>
  67. <img class="bottom-img" src="./image/img4.png" alt="img">
  68. </li>
  69. </ul>
  70. </div>
  71. </div>
  72. <script>
  73. var bigImg = document.getElementsByClassName("top-img")[0];
  74. var smallImg1 = document.getElementsByClassName("bottom-img")[0];
  75. var smallImg2 = document.getElementsByClassName("bottom-img")[1];
  76. var smallImg3 = document.getElementsByClassName("bottom-img")[2];
  77. var smallImg4 = document.getElementsByClassName("bottom-img")[3];
  78. var smallImg = document.getElementsByClassName("bottom-img");
  79. // console.log(smallImg,smallImg.length)
  80. // var arr = [1,2,3,4,5,6,7];
  81. // console.log(arr[0]);
  82. // console.log(arr[1]);
  83. // console.log(arr[2]);
  84. // console.log(arr[3]);
  85. // console.log(arr[4]);
  86. // console.log(arr[5]);
  87. // for(var i=0;i<arr.length;i++){
  88. // console.log(arr[i]);
  89. // }
  90. // 方法二
  91. for(var i=0;i<smallImg.length;i++){
  92. smallImg[i].index = i;
  93. smallImg[i].onmouseover = function(){
  94. // console.log(smallImg[i]);
  95. // console.log(i);
  96. console.log(this.index);
  97. // bigImg.setAttribute("src",smallImg[i].getAttribute("src"));
  98. bigImg.setAttribute("src",this.getAttribute("src"));
  99. }
  100. }
  101. // 方法一
  102. // smallImg1.onmouseover = function(){
  103. // // bigImg.src = smallImg1.src;
  104. // // bigImg.setAttribute("src",smallImg1.getAttribute("src"));
  105. // bigImg.setAttribute("src",this.getAttribute("src"));
  106. // }
  107. // smallImg2.onmouseover = function(){
  108. // // bigImg.src = smallImg2.src;
  109. // bigImg.setAttribute("src",this.getAttribute("src"));
  110. // }
  111. // smallImg3.onmouseover = function(){
  112. // bigImg.setAttribute("src",this.getAttribute("src"));
  113. // }
  114. // smallImg4.onmouseover = function(){
  115. // bigImg.setAttribute("src",this.getAttribute("src"));
  116. // }
  117. // var obj = {
  118. // name:"张三"
  119. // }
  120. // obj.age = 18;
  121. // obj.tal = function(){
  122. // }
  123. // console.log(obj);
  124. </script>
  125. </body>
  126. </html>