首页 > 代码库 > js之图片放大

js之图片放大

/*
*
* JQUERY 之简易放大镜插件
* Author:翅膀的初衷 4585839
* Date:2011-11-16
*
demo:
<input type="text" value="http://www.mamicode.com/c-img.jpg" id="source" />
<script>
var div = ‘<div id="ShowBigPictureBox" style=" position:absolute; top:20px; left:250px; display:none; background-repeat:no-repeat; background-color:#fff; width:500px;"></div>‘;
$("body").append(div);
$("#source").JNMagnifier({
renderTo:"#ShowBigPictureBox"
});
</script>
*/
(function ($) {
$.fn.JNMagnifier = function (targetContainer) {

if (targetContainer && targetContainer.renderTo) {
if (typeof (targetContainer.renderTo) == "string") {
targetContainer.renderTo = $(targetContainer.renderTo);
}
} else {
return;
}
var _img_org_ = this;

var __w = 0;
var __h = 0;

var __left = this.offset().left;
var __top = this.offset().top;

if (this.offsetParent()) {
__left += this.offsetParent().offset().left;
__top += this.offsetParent().offset().top;
}

var _move_x = 0;
var _move_y = 0;

var _val_w = (targetContainer.renderTo.width() / 2);
var _val_h = (targetContainer.renderTo.height() / 2);

_img_org_.mouseover(function () {


targetContainer.renderTo.html(‘<img src="http://www.mamicode.com/‘ + _img_org_.val() + ‘" style="position:absolute; max-width:100%" id="JNMagnifierrenderToImg" />‘);
targetContainer.renderTo.show();
var timer = setInterval(function () {
__w = $("#JNMagnifierrenderToImg").width() / _img_org_.width();
__h = $("#JNMagnifierrenderToImg").height() / _img_org_.height();
if (__w > 0) {
clearInterval(timer);
}
}, 100);
});

_img_org_.mouseout(function () {
targetContainer.renderTo.hide();
});

_img_org_.mousemove(function (e) {
_move_x = 0 - Math.round((document.documentElement.scrollLeft + e.clientX - __left) * __w - _val_w);
_move_y = 0 - Math.round((document.documentElement.scrollTop + e.clientY - __top) * __h - _val_h);
//$("#JNMagnifierrenderToImg").css({"left":_move_x + "px ","top":_move_y + "px"});
});
}
})(jQuery);

js之图片放大