以上是一個簡單地焦點圖事例,思路:圖片路徑寫在a標簽的href屬性里,點擊a得到$(this).attr("href");並把這個值給img的src。用簡單地jQuery寫一個點擊事件。 ...
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>焦點圖</title> 6 <style type="text/css"> 7 img{position: relative;} 8 ul{list-style: none;width: 545px;position: absolute;top: 280px;left: 170px;} 9 li{float: left;width: 20px;line-height: 18px;border: 1px solid #ccc;background-color:#494a93;} 10 a:hover{background-color: red;} 11 a{display: block;width: 20px;line-height: 18px;color: white;text-decoration: none;text-align: center;font-size: 12px;font-family: arial;} 12 p{width: 480px;text-align: center;} 13 </style> 14 </head> 15 <body> 16 <img src="images/1.jpg" alt=""> 17 <ul> 18 <li><a href="images/1.jpg" title="日落">1</a></li> 19 <li><a href="images/2.jpg" title="鋼琴">2</a></li> 20 <li><a href="images/3.jpg" title="大海">3</a></li> 21 <li><a href="images/4.jpg" title="秋色">4</a></li> 22 </ul> 23 <p>這是一段測試文字</p> 24 <script src="js/jquery-3.0.0.js"></script> 25 <script type="text/javascript">
//方法一:超級簡單易懂的方法 26 /*$("ul li:nth-child(1) a").click(function(event){ 27 $("img").attr("src","images/1.jpg") 28 29 var imgsrc=$(this).attr("href") 30 $("img").attr("src",imgsrc) 31 32 $("img").attr("src",$(this).attr("href")) 33 34 $("ul li:nth-child(1)").css("background-color","red") 35 $("ul li:nth-child(2)").css("background-color","#494a93") 36 $("ul li:nth-child(3)").css("background-color","#494a93") 37 $("ul li:nth-child(4)").css("background-color","#494a93") 38 event.preventDefault(); 39 }) 40 $("ul li:nth-child(2) a").click(function(event){ 41 $("img").attr("src","images/2.jpg") 42 43 var imgsrc=$(this).attr("href") 44 $("img").attr("src",imgsrc) 45 46 $("ul li:nth-child(2)").css("background-color","red") 47 $("ul li:nth-child(1)").css("background-color","#494a93") 48 $("ul li:nth-child(3)").css("background-color","#494a93") 49 $("ul li:nth-child(4)").css("background-color","#494a93") 50 event.preventDefault(); 51 }) 52 $("ul li:nth-child(3) a").click(function(event){ 53 $("img").attr("src","images/3.jpg") 54 55 var imgsrc=$(this).attr("href") 56 $("img").attr("src",imgsrc) 57 58 $("ul li:nth-child(3)").css("background-color","red") 59 $("ul li:nth-child(2)").css("background-color","#494a93") 60 $("ul li:nth-child(1)").css("background-color","#494a93") 61 $("ul li:nth-child(4)").css("background-color","#494a93") 62 event.preventDefault(); 63 }) 64 $("ul li:nth-child(4) a").click(function(event){ 65 $("img").attr("src","images/4.jpg") 66 67 var imgsrc=$(this).attr("href") 68 $("img").attr("src",imgsrc) 69 70 $("ul li:nth-child(4)").css("background-color","red") 71 $("ul li:nth-child(2)").css("background-color","#494a93") 72 $("ul li:nth-child(3)").css("background-color","#494a93") 73 $("ul li:nth-child(1)").css("background-color","#494a93") 74 event.preventDefault(); 75 })*/
//方法二:簡化了方法一重覆的代碼量 ,利用.parent().siblings().find("a")選擇到父級的其他兄弟元素
76 $("ul li a").click(function(event){ 77 /*$("img").attr("src","images/4.jpg")*/ 78 79 var imgsrc=$(this).attr("href"); 80 $("img").attr("src",imgsrc); 81 82 $(this).css({"background-color":"red","color":"yellow"}); 83 $(this).parent().siblings().find("a").css({"background-color":"#494a93","color":"white"}); 84 event.preventDefault(); 85 86 var txt=$(this).attr("title"); 87 console.log(txt); //在控制台輸出 88 $("p").text(txt); 89 }) 90 /*$("ul li a").hover(function(event){ 91 $(this).css("background-color","red"); 92 },function(){ 93 $(this).css("background-color","#494a93") 94 })*/ 95 </script> 96 </body> 97 </html>
以上是一個簡單地焦點圖事例,思路:圖片路徑寫在a標簽的href屬性里,點擊a得到$(this).attr("href");並把這個值給img的src。用簡單地jQuery寫一個點擊事件。