jQuery实现的鼠标滑过弹出放大图片特效

前端技术 2023/09/07 JavaScript

本章节介绍一下一种比较常用的效果,那就是当鼠标滑过链接的时候,能够出现跟随鼠标指针移动的图层,在实际应用中,一般是对于链接的一些说明文字或者图片等等,

我们先来看个演示图

下面是代码实例:

<link rel=\"stylesheet\" href=\"../css/common.css\" type=\"text/css\" />
<script type=\"text/javascript\" src=\"../js/jquery-1.2.6.pack.js\"></script>
<script type=\"text/javascript\" src=\"../js/jquery.imagePreview.1.0.js\"></script>
<script type=\"text/javascript\">
$(function(){
	$(\"a.preview\").preview();	 
});
</script>
<style type=\"text/css\">
html{overflow-y:scroll;}
a.preview,a.preview:hover{text-decoration:none;}
a.preview img{margin:20px 10px;}
</style>
</head>

<body>
<div class=\"zxx_out_box\">
 <div class=\"zxx_in_box\">
  <h3 class=\"zxx_title\">图片放大显示的jQuery插件演示页面</h3>
  <div class=\"zxx_main_con\">
			<div class=\"zxx_test_list\">
   	<a class=\"preview\" href=\"http://image.phpstudy.net/image/study/s/s256/mm1.jpg\" title=\"张含韵\">
    	<img src=\"http://image.phpstudy.net/image/study/s/s128/mm1.jpg\" />
    </a>
    <a class=\"preview\" href=\"http://image.phpstudy.net/image/study/s/s256/mm2.jpg\" title=\"某不知名美女\">
    	<img src=\"http://image.phpstudy.net/image/study/s/s128/mm2.jpg\" />
    </a>
    <a class=\"preview\" href=\"http://image.phpstudy.net/image/study/s/s256/mm3.jpg\" title=\"某不知名美女\">
    	<img src=\"http://image.phpstudy.net/image/study/s/s128/mm3.jpg\" />
    </a>
    <a class=\"preview\" href=\"http://image.phpstudy.net/image/study/s/s256/mm4.jpg\" title=\"某不知名美女\">
    	<img src=\"http://image.phpstudy.net/image/study/s/s128/mm4.jpg\" />
    </a>
    <a class=\"preview\" href=\"http://image.phpstudy.net/image/study/s/s256/mm5.jpg\" title=\"某不知名美女\">
    	<img src=\"http://image.phpstudy.net/image/study/s/s128/mm5.jpg\" />
    </a>
   </div>   
  </div>

 </div>
</div>
</body>
</html>

以上代码实现了我们的要求,小伙伴们觉着怎么样呢

接下来我们看看使用方法简要说明:

1.需要借助a标签的href属性,此jQuery插件的原理是当鼠标移至缩略图(或链接文字时),会加载一段含有href指向路径的大图html片段,该片段根据鼠标的位置绝对定位。于是产生了鼠标移到缩略图上显示大图的效果。大图的地址就是a标签的href属性的内容。例如:<a href=”xx.jpg”>缩略图</a> 如果此a标签含有显示大图的方法,则页面就会显示href所指向的“xx.jpg”这个图片。

2.使用的方法是:目标选择器.preview();例如上面的<a href=”xx.jpg”>缩略图</a>就可以使用$(“a”).preview();这段代码实现鼠标移到“缩略图”这个文字链接上显示xx.jpg这张图片的效果。

3.仅支持png,gif,jpg,bmp四种格式的图片,您可以修改插件代码的正则表达式扩展支持的图片格式类型。

下面简单介绍一下实现过程:

一.代码注释:

1.this.screenshotPreview=function(){ },声明一个函数用来实现跟随效果,在本效果中,this其实是可以省略,它指向window。

2.xOffset=10,声明一个变量,用来规定鼠标指针距离弹出图片的横向距离。

3.yOffset=30,声明一个变量,用来规定鼠标指针距离弹出图片的纵向距离。

4.$(\"a.screenshot\").hover(function(e){},function(e){}),规定当鼠标移到链接和离开链接所要执行的函数。

5.this.t = this.title,将链接的title属性值赋值给t属性,这里的this是指向当前鼠标悬浮的链接对象。

6.var c = (this.t != \"\") ? \"<br/>\" + this.t : \"\",如果this.t不为空,也就是存在title属性值,那么插入一个换行符并且连接当前标题内容,否则将c设置为空。

7.$(\"body\").append(\"<p id=\'screenshot\'><img src=\'\"+ this.rel +\"\'/>\"+ c +\"</p>\"),将图片和相关说明添加到body。

8.$(\"#screenshot\").css(\"top\",(e.pageY-xOffset)+\"px\").css(\"left\",(e.pageX+yOffset)+\"px\").fadeIn(\"fast\"),设置p元素的top和left属性值,并且采用淡入效果展现。

9.this.title=this.t,将title内容赋值给this.title,其实不要这一句也没有任何问题,有点多余。

10.$(\"#screenshot\").remove(),移出p元素。

11.$(\"a.screenshot\").mousemove(function(e){}),用来设置当鼠标指针移动时,图片能够跟随。

12.$(\"#screenshot\").css(\"top\",(e.pageY-xOffset)+\"px\") .css(\"left\",(e.pageX+yOffset)+\"px\"),设置p元素的top和left属性值,能够实现跟随效果。

二.相关阅读:

1.hover()函数可以参阅jQuery的hover事件一章节。
2.append()函数可以参阅jQuery的append()方法一章节。
3.css()函数可以参阅jQuery的css()方法一章节。
4.pageY属性可以参阅jQuery的event.pageY属性一章节。
5.fadeIn()函数可以参阅jQuery的fadeIn()方法一章节。
6.remove()函数可以参阅jQuery的remove()方法一章节。
7.mousemove事件可以参阅jQuery的mousemove事件一章节。

本文地址:https://www.stayed.cn/item/19473

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。