练习题4_图片切换.html 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. *{
  9. margin: 0;
  10. padding: 0;
  11. }
  12. li{
  13. list-style: none;
  14. }
  15. .clearfix::after{
  16. content: "";
  17. display: block;
  18. clear: both;
  19. }
  20. .content{
  21. width: 500px;
  22. border:2px solid #999;
  23. margin:100px auto;
  24. text-align: center;
  25. }
  26. .big-img img{
  27. width: 400px;
  28. height: 300px;
  29. }
  30. .small-img{
  31. border-top:2px solid #999;
  32. }
  33. .small-img img{
  34. width: 100px;
  35. height: 80px;
  36. }
  37. .small-img ul li{
  38. float: left;
  39. border-right: 2px solid #999;
  40. width: 125px;
  41. box-sizing: border-box;
  42. text-align: center;
  43. }
  44. .small-img ul li:last-child{
  45. border-right: 0;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div class="content">
  51. <div class="big-img">
  52. <img id="bigImg" src="./img/phone.png" alt="phone">
  53. </div>
  54. <div class="small-img">
  55. <ul class="clearfix">
  56. <li><img src="./img/phone.png" alt="phone"></li>
  57. <li><img src="./img/phone2.png" alt="phone2"></li>
  58. <li><img src="./img/ad1.jpg" alt="ad1"></li>
  59. <li><img src="./img/ad2.png" alt="ad2"></li>
  60. </ul>
  61. </div>
  62. </div>
  63. <script>
  64. // 获取元素
  65. var bigImg = document.getElementById("bigImg");
  66. // var smallImgs = document.getElementsByClassName("small-img")[0].getElementsByTagName("img");
  67. var smallImgs = document.querySelectorAll(".small-img img");
  68. // smallImgs[0].onmouseover = function(){
  69. // var thisSrc = smallImgs[0].getAttribute("src");
  70. // bigImg.setAttribute("src",thisSrc);
  71. // }
  72. // smallImgs[1].onmouseover = function(){
  73. // var thisSrc = smallImgs[1].getAttribute("src");
  74. // bigImg.setAttribute("src",thisSrc);
  75. // }
  76. // smallImgs[2].onmouseover = function(){
  77. // var thisSrc = smallImgs[2].getAttribute("src");
  78. // bigImg.setAttribute("src",thisSrc);
  79. // }
  80. // smallImgs[3].onmouseover = function(){
  81. // var thisSrc = smallImgs[3].getAttribute("src");
  82. // bigImg.setAttribute("src",thisSrc);
  83. // }
  84. // 循环遍历smallImgs数组
  85. for(var i=0;i<smallImgs.length;i++){
  86. smallImgs[i].onmouseover = function(){
  87. // console.log("鼠标移入");
  88. console.log(i);
  89. // var thisSrc = smallImgs[i].getAttribute("src");
  90. var thisSrc = this.getAttribute("src");
  91. bigImg.setAttribute("src",thisSrc);
  92. }
  93. }
  94. </script>
  95. </body>
  96. </html>