图片放大镜jquery.jqzoom.js使用实例附放大镜图标


Posted in Javascript onJune 19, 2014

图片放大镜jquery.jqzoom.js使用实例附放大镜图标
1. jquery.jqzoom.js

//**************************************************************
// jQZoom allows you to realize a small magnifier window,close
// to the image or images on your web page easily.
//
// jqZoom version 2.2
// Author Doc. Ing. Renzi Marco(www.mind-projects.it)
// First Release on Dec 05 2007
// i'm looking for a job,pick me up!!!
// mail: renzi.mrc@gmail.com
//**************************************************************
(function ($) {
$.fn.jqueryzoom = function (options) {
var settings = {
xzoom: 200, //zoomed width default width
yzoom: 200, //zoomed div default width
offset: 10, //zoomed div default offset
position: "right",//zoomed div default position,offset position is to the right of the image
lens: 1, //zooming lens over the image,by default is 1;
preload: 1
};
if (options)
{
$.extend(settings, options);
}
var noalt = '';
$(this).hover(function () {
var imageLeft = $(this).offset().left;
var imageTop = $(this).offset().top;
var imageWidth = $(this).children('img').get(0).offsetWidth;
var imageHeight = $(this).children('img').get(0).offsetHeight;
noalt = $(this).children("img").attr("alt");
var bigimage = $(this).children("img").attr("jqimg");
$(this).children("img").attr("alt", '');
if ($("div.zoomdiv").get().length == 0)
{
$(this).after("<div class='zoomdiv'><img class='bigimg' src='" + bigimage + "'/></div>");
$(this).append("<div class='jqZoomPup'> </div>");
}
if (settings.position == "right")
{
if (imageLeft + imageWidth + settings.offset + settings.xzoom > screen.width)
{
leftpos = imageLeft - settings.offset - settings.xzoom;
} else
{
leftpos = imageLeft + imageWidth + settings.offset;
}
} else
{
leftpos = imageLeft - settings.xzoom - settings.offset;
if (leftpos < 0)
{
leftpos = imageLeft + imageWidth + settings.offset;
}
}
$("div.zoomdiv").css({ top: imageTop, left: leftpos });
$("div.zoomdiv").width(settings.xzoom);
$("div.zoomdiv").height(settings.yzoom);
$("div.zoomdiv").show();
if (!settings.lens)
{
$(this).css('cursor', 'crosshair');
}
$(document.body).mousemove(function (e) {
mouse = new MouseEvent(e);
/*$("div.jqZoomPup").hide();*/
var bigwidth = $(".bigimg").get(0).offsetWidth;
var bigheight = $(".bigimg").get(0).offsetHeight;
var scaley = 'x';
var scalex = 'y';
if (isNaN(scalex) | isNaN(scaley))
{
var scalex = (bigwidth / imageWidth);
var scaley = (bigheight / imageHeight);
$("div.jqZoomPup").width((settings.xzoom) / scalex);
$("div.jqZoomPup").height((settings.yzoom) / scaley);
if (settings.lens)
{
$("div.jqZoomPup").css('visibility', 'visible');
}
}
xpos = mouse.x - $("div.jqZoomPup").width() / 2 - imageLeft;
ypos = mouse.y - $("div.jqZoomPup").height() / 2 - imageTop;
if (settings.lens)
{
xpos = (mouse.x - $("div.jqZoomPup").width() / 2 < imageLeft) ? 0 : (mouse.x + $("div.jqZoomPup").width() / 2 > imageWidth + imageLeft) ? (imageWidth - $("div.jqZoomPup").width() - 2) : xpos;
ypos = (mouse.y - $("div.jqZoomPup").height() / 2 < imageTop) ? 0 : (mouse.y + $("div.jqZoomPup").height() / 2 > imageHeight + imageTop) ? (imageHeight - $("div.jqZoomPup").height() - 2) : ypos;
}
if (settings.lens)
{
$("div.jqZoomPup").css({ top: ypos, left: xpos });
}
scrolly = ypos;
$("div.zoomdiv").get(0).scrollTop = scrolly * scaley;
scrollx = xpos;
$("div.zoomdiv").get(0).scrollLeft = (scrollx) * scalex;
});
}, function () {
$(this).children("img").attr("alt", noalt);
$(document.body).unbind("mousemove");
if (settings.lens)
{
$("div.jqZoomPup").remove();
}
$("div.zoomdiv").remove();
});
count = 0;
if (settings.preload)
{
$('body').append("<div style='display:none;' class='jqPreload" + count + "'>sdsdssdsd</div>");
$(this).each(function () {
var imagetopreload = $(this).children("img").attr("jqimg");
var content = jQuery('div.jqPreload' + count + '').html();
jQuery('div.jqPreload' + count + '').html(content + '<img src=\"' + imagetopreload + '\">');
});
}
}
})(jQuery);
function MouseEvent(e) {
this.x = e.pageX;
this.y = e.pageY;
}

2. jqzoom.css

/*jQzoom*/
.jqzoom{
border:1px solid #BBB;
float:left;
position:relative;
padding:0px;
cursor:pointer;
}
div.zoomdiv {
z-index: 999;
position : absolute;
top:0px;
left:0px;
width : 200px;
height : 200px;
background: #ffffff;
border:1px solid #CCCCCC;
display:none;
text-align: center;
overflow: hidden;
}
div.jqZoomPup {
z-index : 999;
visibility : hidden;
position : absolute;
top:0px;
left:0px;
width : 50px;
height : 50px;
border: 1px solid #aaa;
background: #ffffff url(../images/zoomlens.gif) 50% top no-repeat;
opacity: 0.5;
-moz-opacity: 0.5;
-khtml-opacity: 0.5;
filter: alpha(Opacity=50);
}

3. html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script src="jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="jquery.jqzoom.js" type="text/javascript"></script>
<link href="jqzoom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
/*使用jqzoom*/
$(function() {
$(".jqzoom").jqueryzoom({
xzoom: 400, //放大图的宽度(默认是 200)
yzoom: 400, //放大图的高度(默认是 200)
offset: 10, //离原图的距离(默认是 10)
position: "right", //放大图的定位(默认是 "right")
preload: 1
});
});
</script>
</head>
<body>
<div class="jqzoom">
<img src="images/shoe1_small.jpg" style="width:300px; height:300px;" alt="" jqimg="images/shoe1_big.jpg" id="bigImg"/>
</div>
</body>
</html>

附件:放大镜图标(zoomlens.gif)
图片放大镜jquery.jqzoom.js使用实例附放大镜图标

Javascript 相关文章推荐
javascript中xml操作实现代码
Nov 21 Javascript
JavaScript之编码规范 推荐
May 23 Javascript
深入理解JS中的变量及作用域、undefined与null
Mar 04 Javascript
jquery操作checkbox实现全选和取消全选
May 02 Javascript
javascript瀑布流式图片懒加载实例
Jun 28 Javascript
js中获取 table节点各tr及td的内容简单实例
Oct 14 Javascript
JavaScript数据结构链表知识详解
Nov 21 Javascript
@ResponseBody 和 @RequestBody 注解的区别
Mar 08 Javascript
原生JS京东轮播图代码
Mar 22 Javascript
[js高手之路]寄生组合式继承的优势详解
Aug 28 Javascript
微信小程序 如何获取网络状态
Jul 26 Javascript
JavaScript实现刮刮乐效果
Nov 01 Javascript
jQuery常用操作方法及常用函数总结
Jun 19 #Javascript
javascript使用正则控制input输入框允许输入的值方法大全
Jun 19 #Javascript
java和javascript获取word文档的书签位置对比
Jun 19 #Javascript
js调试系列 断点与动态调试[基础篇]
Jun 18 #Javascript
jquery+css3打造一款ajax分页插件(自写)
Jun 18 #Javascript
在JavaScript中判断整型的N种方法示例介绍
Jun 18 #Javascript
input标签内容改变的触发事件介绍
Jun 18 #Javascript
You might like
提取HTML标签
2006/10/09 PHP
一个图形显示IP的PHP程序代码
2007/10/19 PHP
PHP实现仿Google分页效果的分页函数
2015/07/29 PHP
JS判断不同分辨率调用不同的CSS样式文件实现思路及测试代码
2013/01/23 Javascript
THREE.JS入门教程(6)创建自己的全景图实现步骤
2013/01/25 Javascript
判断在css加载完毕后执行后续代码示例
2014/09/03 Javascript
整理AngularJS中的一些常用指令
2015/06/16 Javascript
jquery实现仿JqueryUi可拖动的DIV实例
2015/07/31 Javascript
举例讲解JavaScript中关于对象操作的相关知识
2015/11/16 Javascript
JavaScript操作class和style样式代码详解
2016/02/13 Javascript
常用原生JS兼容性写法汇总
2016/04/27 Javascript
浅谈Javascript中的Label语句
2016/12/14 Javascript
利用node.js+mongodb如何搭建一个简单登录注册的功能详解
2017/07/30 Javascript
Node.js中DNS模块学习总结
2018/02/28 Javascript
玩转Koa之koa-router原理解析
2018/12/29 Javascript
基于Koa(nodejs框架)对json文件进行增删改查的示例代码
2019/02/02 NodeJs
jQuery实现的五星点评功能【案例】
2019/02/18 jQuery
Vue.js使用axios动态获取response里的data数据操作
2020/09/08 Javascript
python中requests和https使用简单示例
2018/01/18 Python
Python定时任务sched模块用法示例
2018/07/16 Python
Python实现将通信达.day文件读取为DataFrame
2018/12/22 Python
使用python将请求的requests headers参数格式化方法
2019/01/02 Python
python使用flask与js进行前后台交互的例子
2019/07/19 Python
Django文件存储 自己定制存储系统解析
2019/08/02 Python
基于python调用psutil模块过程解析
2019/12/20 Python
python和php学习哪个更有发展
2020/06/17 Python
python中类与对象之间的关系详解
2020/12/16 Python
HTML5在线预览PDF的示例代码
2017/09/14 HTML / CSS
STP的判定过程
2012/10/01 面试题
学生自我鉴定模板
2013/12/30 职场文书
护士在校生自荐信
2014/02/01 职场文书
信息技术毕业生自荐信范文
2014/03/13 职场文书
安全生产标语大全
2014/10/06 职场文书
单位法人授权委托书范本
2014/10/09 职场文书
2015年南京大屠杀纪念日活动总结
2015/03/24 职场文书
学校元旦晚会开场白
2015/05/29 职场文书