<!---html-->
<!doctype html>
<html lang="en-US"><head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>jQuery Image Zoom Demo</title>
<meta name="author" content="Jackson Huang">
<script src="jquery.min.js"></script>
<script src="can.js"></script>
</head>
<style>
.main-image{
position:relative;
}
.main-image .large{
position:absolute;
}
.scroll-box{
position:absolute;
}
</style>
<body>
<div class="main-image" style="width:300px;height:300px" >
<img class="small" src="1.jpg" width="300px" height="300px"/>
<div class="large" style="width:200px;height:200px"> </div>
<div class="scroll-box"> </div>
</div>
<script>
$(".main-image").imageZoom({type:2,scroll_width:150,scroll_height:150});
</script>
</body>
</html>
<!---html-->
<!--can.js-->
;
(function($) {
$.fn.imageZoom = function(options) {
var defaults = {
scaling: 0.3
};
options = $.extend(defaults, options),native_width = 0,
native_height = 0,
current_width = 0,
current_height = 0,
$small = $(".small"),
$large = $(".large");
$scroll_box = $(".scroll-box");
var my_types = 1;
if(options.type != undefined){
my_types = options.type;
}
var image_object = new Image();
image_object.src = $small.attr('src');
$(".main-image").mousemove(function(e) {
if (!native_width && !native_height) {
native_height = image_object.height;
native_width = image_object.width;
current_height = $small.height();
current_width = $small.width();
} else {
var magnify_offset = $(this).offset(),
mx = e.pageX - magnify_offset.left,
my = e.pageY - magnify_offset.top;
if (mx < $(this).width() && my < $(this).height() && mx > 0 && my > 0) {
$large.fadeIn(100);
} else {
$large.fadeOut(100);
}
if ($large.is(":visible")) {
if(my_types == 1){
var rx = Math.round(mx / $small.width() * native_width - $large.width() / 2) * -1,//mx 鼠标X坐标 my 鼠标Y坐标 图片定位X轴开始围着
ry = Math.round(my / $small.height() * native_height - $large.height() / 2) * -1,//图片定位Y轴开始位置
bgp = rx + "px " + ry + "px";//图片定位
var px = mx - $large.width() / 2;//lager 放大X轴开始位置
var py = my - $large.height() / 2;//lager 放大Y轴开始位置
$large.css({"width":"200px"});
$large.css({"height":"200px"});
}else{
var scroll_width = options.scroll_width;//box 宽度
var scroll_height = options.scroll_height;//box 高度
var scroll_left,scroll_top,scroll_end_left,scroll_end_top,b_scroll_end_left,b_scroll_end_top,rx,ry;
//box 定位
scroll_left = mx-scroll_width/2;
scroll_top = my - scroll_height/2;
if(scroll_left<0){
scroll_left = 0;
}
if(scroll_top <0){
scroll_top = 0;
}
if(scroll_left +scroll_width>$small.width() ){
scroll_left = $small.width() - scroll_width;
}
if(scroll_top + scroll_height>$small.height()){
scroll_top = $small.height() - scroll_height ;
}
scroll_end_left = scroll_left + scroll_width;
scroll_end_top = scroll_top + scroll_height;
//背景图定位
b_scroll_left = scroll_left/$small.width() * native_width;
b_scroll_top = scroll_top/$small.height() * native_height;
b_scroll_end_left = scroll_end_left/$small.width() * native_width;
b_scroll_end_top = scroll_end_top/$small.height() * native_width;
var lwidth = b_scroll_end_left - b_scroll_left;
var lheight = b_scroll_end_top - b_scroll_top;
$scroll_box.css({
width:scroll_width,
height:scroll_height,
left:scroll_left,
top:scroll_top,
background:"#000000",
opacity:0.1
})
/*if(mx<scroll_width){
scroll_left = 0;
}else{
rx = mx / $small.width() * native_width
}
if(mx<scroll_height){
scroll_right = 0;
}
var px = current_width;//lager 放大X轴开始位置
var py = 0;//lager 放大Y轴开始位
$large.css({"width":"300px"});
$large.css({"height":"300px"});*/
$large.css({"width":lwidth});
$large.css({"height":lheight});
var bgp = (-b_scroll_left) + "px " + (-b_scroll_top) + "px";//图片定位
var px = $small.width();
var py = 0;
}
$large.css({
left: px,
top: py,
background:"url("+image_object.src+") no-repeat",
backgroundPosition: bgp,
});
}
}
});
};
})(jQuery);
<!--can.js-->