代碼:html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> * { margin: 0; padding: 0; } #nav { width: 120px; height: 80px; position: absolute; right: 310px; top: 60px; z-index: 2; } #nav img { width: 100%; height: 100%; margin: 10px 0; transition: 0.5s; } #nav img:hover { border: 5px solid lightcoral; box-sizing: border-box; } #pic { width: 1000px; height: 600px; position: relative; margin: 50px auto; } #pic img { width: 100%; height: 100%; position: absolute; display: none; } </style> <script src="js/jquery.min.js"></script> </head> <body> <div id="nav"> <img src="img/1.jpg" alt="1" /> <img src="img/2.jpg" alt="2" /> <img src="img/3.jpg" alt="3" /> <img src="img/4.jpg" alt="4" /> <img src="img/5.jpg" alt="5" /> </div> <div id="pic"> <img src="img/1.jpg" alt="1" /> <img src="img/2.jpg" alt="2" /> <img src="img/3.jpg" alt="3" /> <img src="img/4.jpg" alt="4" /> <img src="img/5.jpg" alt="5" /> </div> <script> var count = 0; //記錄圖片是第幾張 $("#pic img").eq(0).fadeIn(1); //初始圖片顯示狀態,不寫的話,第一張圖片不會顯示 $("#nav img").on("click", changePic); //給導航框小圖片添加點擊事件 var list = Array.from($("#nav img")); //將小圖圖片列表,放在數組中 function changePic(e) { //小圖點擊事件 var index = list.indexOf(this); //在數組中查找被點擊的那一項索引 count = index; //當點擊時將自動切換的索引換成點擊的那個 $("#pic img:visible").fadeOut(500); //初始化全部圖片,將顯示的隱藏 $("#pic img").eq(index).fadeIn(500); //將被單擊的顯示 } setInterval(autoMove, 3000); //每三秒切換一次 function autoMove() { count++; //切換圖片 if (count > 4) { count = 0; } $("#pic img:visible").fadeOut(500); //初始化全部圖片,將顯示的隱藏 $("#pic img").eq(count).fadeIn(500); //將被單擊的顯示 } </script> </body> </html>