简单实现jquery焦点图
2016-12-12 11:19
405 查看
本文实例为大家分享了jquery焦点图的实现代码,供大家参考,具体内容如下
<!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焦点图特效汇总》进行学习,希望大家喜欢。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- 最简单jquery实现带左右箭头和数字焦点的图片轮播
- jQuery实现简单的导航按钮焦点背景更换(代替css hover)以及弹出子菜单 我的第一篇博文
- jquery实现input框获取焦点的简单实例
- jQuery的简单应用-实现文本框获取焦点时改变样式
- 一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
- 用jquery实现简单的年月日下拉框选择
- jQueryAPI+Ajax技术实现的简单例子(续4)
- jquery简单应用篇——焦点图片切换
- 用jQuery实现最简单的滚屏效果
- 传智播客-Ajax(2)-利用jQuery简单模拟实现股票信息的实时显示
- jQuery+ROW_NUMBER实现超级简单分页(不可错过)
- JQuery实现简单下拉菜单
- Jquery中幻灯片效果的简单实现
- 传智播客 AJAX简单应用之JQuery实现
- jQuery+ROW_NUMBER结合Repeater实现简单分页
- jquery 简单导航实现代码
- jQuery插件---实现简单的圆角
- 简单自定义实现jQuery验证
- jquery返回json类型数据集合简单实现ajax返回多个数据
- 简单JS实现走马灯效果的文字(无需jQuery)