简单实现jquery焦点图

所属分类: 网络编程 / JavaScript 阅读数: 1967
收藏 0 赞 0 分享

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

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

更多精彩内容其他人还在看

Angular使用Md5加密的解决方法

这篇文章主要介绍了Angular使用Md5加密的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

详解JS构造函数中this和return

本文通过实例代码给大家介绍了JS构造函数中this和return,需要的朋友参考下吧
收藏 0 赞 0 分享

ES6中Array.find()和findIndex()函数的用法详解

ES6为Array增加了find(),findIndex函数。find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined,而findIndex()函数也是查找目标元素,找到就返回元素的位置,找不到就返回-1。下面通过实例详解,需要的朋友参考下吧
收藏 0 赞 0 分享

JS闭包的几种常见形式实例详解

本文通过实例代码给大家详细介绍了js闭包的几种常见形式,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下
收藏 0 赞 0 分享

ES6中Array.copyWithin()函数的用法实例详解

ES6为Array增加了copyWithin函数,用于操作当前数组自身,用来把某些个位置的元素复制并覆盖到其他位置上去。下面重点给大家介绍ES6中Array.copyWithin()函数的用法,需要的朋友参考下
收藏 0 赞 0 分享

Javascript 严格模式use strict详解

严格模式:由ECMA-262规范定义的JavaScript标准,对javascrip的限制更强。这篇文章主要介绍了Javascript 严格模式use strict详解 ,需要的朋友可以参考下
收藏 0 赞 0 分享

引入JavaScript时alert弹出框显示中文乱码问题

今天在HTML中引入JavaScript文件运行时,alert弹出的提示框中文显示为乱码,怎么解决此问题呢?下面小编给大家带来了引入JavaScript时alert弹出框显示中文乱码问题的解决方法,一起看看吧
收藏 0 赞 0 分享

AngularJs 延时器、计时器实例代码

这篇文章主要介绍了AngularJs 延时器、计时器实例代码,需要的朋友可以参考下
收藏 0 赞 0 分享

JS分页的实现(同步与异步)

这篇文章主要介绍了JS分页的实现(同步与异步),需要的朋友可以参考下
收藏 0 赞 0 分享

Angularjs自定义指令实现分页插件(DEMO)

由于最近的一个项目使用的是angularjs1.0的版本,涉及到分页查询数据的功能,后来自己就用自定义指令实现了该功能,下面小编把实例demo分享到脚本之家平台,需要的朋友参考下
收藏 0 赞 0 分享
查看更多