这次给大家带来jquery实现鼠标经过时放大图片功能,jquery实现鼠标经过时放大图片功能的注意事项有哪些,下面就是实战案例,一起来看一下。
我们先来看个演示图
下面是代码实例:
<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>
<p class="zxx_out_box">
<p class="zxx_in_box">
<h3 class="zxx_title">图片放大显示的jquery插件演示页面</h3>
<p class="zxx_main_con">
<p class="zxx_test_list">
<a class="preview" href="http://image.jb51.net/image/study/s/s256/mm1.jpg" title="张含韵">
<img src="http://image.jb51.net/image/study/s/s128/mm1.jpg" />
</a>
<a class="preview" href="http://image.jb51.net/image/study/s/s256/mm2.jpg" title="某不知名美女">
<img src="http://image.jb51.net/image/study/s/s128/mm2.jpg" />
</a>
<a class="preview" href="http://image.jb51.net/image/study/s/s256/mm3.jpg" title="某不知名美女">
<img src="http://image.jb51.net/image/study/s/s128/mm3.jpg" />
</a>
<a class="preview" href="http://image.jb51.net/image/study/s/s256/mm4.jpg" title="某不知名美女">
<img src="http://image.jb51.net/image/study/s/s128/mm4.jpg" />
</a>
<a class="preview" href="http://image.jb51.net/image/study/s/s256/mm5.jpg" title="某不知名美女">
<img src="http://image.jb51.net/image/study/s/s128/mm5.jpg" />
</a>
</p>
</p>
</p>
</p>
</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属性值,能够实现跟随效果。
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
以上就是jquery实现鼠标经过时放大图片功能的详细内容。