简单实现jquery焦点图


Posted in Javascript onDecember 12, 2016

本文实例为大家分享了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焦点图特效汇总》进行学习,希望大家喜欢。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JS面向对象编程浅析
Aug 28 Javascript
jquery1.9 下检测浏览器类型和版本的方法
Dec 26 Javascript
jQuery获得指定元素坐标的方法
Apr 14 Javascript
jQuery实现首页顶部可伸缩广告特效代码
Apr 15 Javascript
AngularJS身份验证的方法
Feb 17 Javascript
JS组件系列之Bootstrap table表格组件神器【终结篇】
May 10 Javascript
简单理解vue中track-by属性
Oct 26 Javascript
jQuery Validate验证表单时多个name相同的元素只验证第一个的解决方法
Dec 24 Javascript
详解vue项目优化之按需加载组件-使用webpack require.ensure
Jun 13 Javascript
vuejs实现本地数据的筛选分页功能思路详解
Nov 15 Javascript
Vue不能观察到数组length的变化
Jun 08 Javascript
JS实现获取进今年第几天是周几的方法分析
Jun 27 Javascript
javascript中setAttribute兼容性用法分析
Dec 12 #Javascript
jQuery焦点图左右转换效果
Dec 12 #Javascript
js实现刷新页面后回到记录时滚动条的位置【两种方案可选】
Dec 12 #Javascript
jquery利用json实现页面之间传值的实例解析
Dec 12 #Javascript
多种方式实现js图片预览
Dec 12 #Javascript
JavaScript实现多栏目切换效果
Dec 12 #Javascript
解决同一页面中两个iframe互相调用jquery,js函数的方法
Dec 12 #Javascript
You might like
php通过COM类调用组件的实现代码
2012/01/11 PHP
解析php中die(),exit(),return的区别
2013/06/20 PHP
php设计模式之备忘模式分析【星际争霸游戏案例】
2020/03/24 PHP
asp 取文本框名称代码
2008/12/02 Javascript
JavaScript栏目列表隐藏/显示简单实现
2013/04/03 Javascript
使用js声明数组,对象在jsp页面中(获得ajax得到json数据)
2013/11/05 Javascript
js 事件截取enter按键页面提交事件示例代码
2014/03/04 Javascript
js时钟翻牌效果实现代码分享
2020/07/31 Javascript
js实现按钮颜色渐变动画效果
2015/08/20 Javascript
jquery实现的Accordion折叠面板效果代码
2015/09/02 Javascript
JS中input表单隐藏域及其使用方法
2017/02/13 Javascript
Vue框架中正确引入JS库的方法介绍
2017/07/30 Javascript
浅谈vue中使用图片懒加载vue-lazyload插件详细指南
2017/10/23 Javascript
实例解析ES6 Proxy使用场景介绍
2018/01/08 Javascript
js删除数组中的元素delete和splice的区别详解
2018/02/03 Javascript
如何利用@angular/cli V6.0直接开发PWA应用详解
2018/05/06 Javascript
一文读懂ES7中的javascript修饰器
2019/05/06 Javascript
微信小程序云开发详细教程
2019/05/16 Javascript
使用 node.js 模仿 Apache 小部分功能
2019/07/07 Javascript
为react组件库添加typescript类型提示的方法
2020/06/15 Javascript
如何在Express4.x中愉快地使用async的方法
2020/11/18 Javascript
浅谈JSON5解决了JSON的两大痛点
2020/12/14 Javascript
Python代码的打包与发布详解
2014/07/30 Python
Python selenium 三种等待方式详解(必会)
2016/09/15 Python
Python模拟登录的多种方法(四种)
2018/06/01 Python
详解python的sorted函数对字典按key排序和按value排序
2018/08/10 Python
浅谈Python反射 &amp; 单例模式
2019/03/21 Python
Python中调用其他程序的方式详解
2019/08/06 Python
Python将字典转换为XML的方法
2020/08/01 Python
英国设计师珠宝网站:Joshua James Jewellery
2020/03/01 全球购物
平面网站制作专科生的自我评价分享
2013/12/11 职场文书
八年级美术教学反思
2014/02/02 职场文书
爱耳日宣传活动总结
2014/07/05 职场文书
小学生交通安全寄语
2015/02/27 职场文书
违规违纪检讨书范文
2015/05/06 职场文书
新手入门Mysql--sql执行过程
2021/06/20 MySQL