国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

簡單實現(xiàn)jquery焦點圖

2019-11-19 18:33:03
字體:
來源:轉載
供稿:網(wǎng)友

本文實例為大家分享了jquery焦點圖的實現(xiàn)代碼,供大家參考,具體內容如下

<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>焦點圖</title> <style type="text/css"> img{position: relative;} ul{list-style: none;width: 545px;position: absolute;top: 280px;left: 170px;} li{float: left;width: 20px;line-height: 18px;border: 1px solid #ccc;background-color:#494a93;} a:hover{background-color: red;} a{display: block;width: 20px;line-height: 18px;color: white;text-decoration: none;text-align: center;font-size: 12px;font-family: arial;} p{width: 480px;text-align: center;} </style></head><body> <img src="images/1.jpg" alt=""> <ul> <li><a href="images/1.jpg" title="日落">1</a></li> <li><a href="images/2.jpg" title="鋼琴">2</a></li> <li><a href="images/3.jpg" title="大海">3</a></li> <li><a href="images/4.jpg" title="秋色">4</a></li> </ul> <p>這是一段測試文字</p> <script src="js/jquery-3.0.0.js"></script> <script type="text/javascript">      //方法一:超級簡單易懂的方法 /*$("ul li:nth-child(1) a").click(function(event){  $("img").attr("src","images/1.jpg")   var imgsrc=$(this).attr("href")  $("img").attr("src",imgsrc)  $("img").attr("src",$(this).attr("href"))  $("ul li:nth-child(1)").css("background-color","red")  $("ul li:nth-child(2)").css("background-color","#494a93")  $("ul li:nth-child(3)").css("background-color","#494a93")  $("ul li:nth-child(4)").css("background-color","#494a93")  event.preventDefault(); }) $("ul li:nth-child(2) a").click(function(event){  $("img").attr("src","images/2.jpg")  var imgsrc=$(this).attr("href")  $("img").attr("src",imgsrc)  $("ul li:nth-child(2)").css("background-color","red")  $("ul li:nth-child(1)").css("background-color","#494a93")  $("ul li:nth-child(3)").css("background-color","#494a93")  $("ul li:nth-child(4)").css("background-color","#494a93")  event.preventDefault(); }) $("ul li:nth-child(3) a").click(function(event){  $("img").attr("src","images/3.jpg")  var imgsrc=$(this).attr("href")  $("img").attr("src",imgsrc)  $("ul li:nth-child(3)").css("background-color","red")  $("ul li:nth-child(2)").css("background-color","#494a93")  $("ul li:nth-child(1)").css("background-color","#494a93")  $("ul li:nth-child(4)").css("background-color","#494a93")  event.preventDefault(); }) $("ul li:nth-child(4) a").click(function(event){  $("img").attr("src","images/4.jpg")  var imgsrc=$(this).attr("href")  $("img").attr("src",imgsrc)  $("ul li:nth-child(4)").css("background-color","red")  $("ul li:nth-child(2)").css("background-color","#494a93")  $("ul li:nth-child(3)").css("background-color","#494a93")  $("ul li:nth-child(1)").css("background-color","#494a93")  event.preventDefault(); })*/            //方法二:簡化了方法一重復的代碼量 ,利用.parent().siblings().find("a")選擇到父級的其他兄弟元素 $("ul li a").click(function(event){  /*$("img").attr("src","images/4.jpg")*/  var imgsrc=$(this).attr("href");  $("img").attr("src",imgsrc);    $(this).css({"background-color":"red","color":"yellow"});  $(this).parent().siblings().find("a").css({"background-color":"#494a93","color":"white"});  event.preventDefault();  var txt=$(this).attr("title");  console.log(txt); //在控制臺輸出  $("p").text(txt); }) /*$("ul li a").hover(function(event){   $(this).css("background-color","red"); },function(){  $(this).css("background-color","#494a93") })*/ </script></body></html>

以上是一個簡單地焦點圖事例,思路:圖片路徑寫在a標簽的href屬性里,點擊a得到$(this).attr("href");并把這個值給img的src。用簡單地jQuery寫一個點擊事件。

更多精彩內容大家還可以參考《jQuery焦點圖特效匯總》進行學習,希望大家喜歡。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 荔波县| 伊宁市| 二连浩特市| 柏乡县| 太白县| 遵化市| 元氏县| 江阴市| 潢川县| 黎平县| 玛纳斯县| 邳州市| 邯郸市| 亳州市| 卢龙县| 河曲县| 赤水市| 静乐县| 武川县| 曲靖市| 娄烦县| 方山县| 威海市| 同仁县| 东乌珠穆沁旗| 廊坊市| 洛南县| 宁强县| 江川县| 安图县| 大英县| 高碑店市| 疏勒县| 莱州市| 桂阳县| 来宾市| 永川市| 天门市| 舒兰市| 青冈县| 密云县|