简单实现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 相关文章推荐
自适应图片大小的弹出窗口
Jul 27 Javascript
javascript setTimeout和setInterval 的区别
Dec 08 Javascript
jquery ajax 同步异步的执行示例代码
Jun 23 Javascript
js超时调用setTimeout和间歇调用setInterval实例分析
Jan 28 Javascript
理解javascript函数式编程中的闭包(closure)
Mar 08 Javascript
JavaScript字符集编码与解码详谈
Feb 02 Javascript
JavaScript函数柯里化原理与用法分析
Mar 31 Javascript
微信小程序开发教程之增加mixin扩展
Aug 09 Javascript
Bootstrap 中data-[*] 属性的整理
Mar 13 Javascript
vee-validate vue 2.0自定义表单验证的实例
Aug 28 Javascript
实例讲解JS中pop使用方法
Jan 27 Javascript
JavaScript实现随机五位数验证码
Sep 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网站来路获取代码(针对搜索引擎)
2010/06/08 PHP
php使用Cookie控制访问授权的方法
2015/01/21 PHP
CentOS下PHP安装Oracle扩展
2015/02/15 PHP
php使用str_replace替换多维数组的实现方法分析
2017/06/15 PHP
PHP基于socket实现的简单客户端和服务端通讯功能示例
2017/07/10 PHP
PHP7内核之Reference详解
2019/03/14 PHP
基于laravel where的高级使用方法
2019/10/10 PHP
分享一个用Mootools写的鼠标滑过进度条改变进度值的实现代码
2011/12/12 Javascript
使用js声明数组,对象在jsp页面中(获得ajax得到json数据)
2013/11/05 Javascript
浅析jquery的作用与优势
2013/12/02 Javascript
前端性能优化及技巧
2016/05/06 Javascript
BootStrap 智能表单实战系列(二)BootStrap支持的类型简介
2016/06/13 Javascript
BootStrap与Select2使用小结
2017/02/17 Javascript
Vuex之理解Mutations的用法实例
2017/04/19 Javascript
基于js中style.width与offsetWidth的区别(详解)
2017/11/12 Javascript
Python 文件管理实例详解
2015/11/10 Python
python检测空间储存剩余大小和指定文件夹内存占用的实例
2018/06/11 Python
Flask模拟实现CSRF攻击的方法
2018/07/24 Python
解决每次打开pycharm直接进入项目的问题
2018/10/28 Python
Python获取好友地区分布及好友性别分布情况代码详解
2019/07/10 Python
python将字母转化为数字实例方法
2019/10/04 Python
Django数据库操作之save与update的使用
2020/04/01 Python
利用python3筛选excel中特定的行(行值满足某个条件/行值属于某个集合)
2020/09/04 Python
Python 测试框架unittest和pytest的优劣
2020/09/26 Python
css3实现背景图片拉伸效果像桌面壁纸一样
2013/08/19 HTML / CSS
CSS3对背景图片的裁剪及尺寸和位置的设定方法
2016/03/07 HTML / CSS
关于爱情的广播稿
2014/01/16 职场文书
求职信的最佳写作思路
2014/02/01 职场文书
共产党员承诺书
2014/03/25 职场文书
学习教师法的心得体会
2014/09/03 职场文书
综治工作汇报材料
2014/10/27 职场文书
校园环境卫生倡议书
2015/04/29 职场文书
美丽人生观后感
2015/06/03 职场文书
Redis基于Bitmap实现用户签到功能
2021/06/20 Redis
浅谈Redis位图(Bitmap)及Redis二进制中的问题
2021/07/15 Redis
Windows11里微软已经将驱动程序安装位置A盘删除
2021/11/21 数码科技