jq商品展示图放大镜 and 原生js和html5写的放大镜效果 ~~效果不错

<!DOCTYPE HTML>

<html >

<head>

<meta charset="UTF-8">

<title>原生Js结合html5做出放大镜的效果</title>

<style>

img{

margin:100px 500px;

}

div{

width:200px;

height:200px;

display:none;

border:1px solid #ccc;

border-radius:100px;

z-index:1000;

pointer-events:none;

}

</style>

</head>

<body>

<img src="http://d.hiphotos.baidu.com/image/w%3D2048/sign=529c3afe39c79f3d8fe1e3308e99cc11/7a899e510fb30f24d9069302ca95d143ad4b0316.jpg" onload="core(this)"></img>

<div ;

obj.style.opacity = 1;

}

};

}

</script>

</body>

</html>

-------------------------------------------------------------

用于商品详情页的商品图放大镜

---------------------------------------------------------------

Imgzoom.prototype = {

constructor: Imgzoom,

init: function(){

this.createHtml();

this.bindEvents();

},

createHtml: function(){

if($('#'+this.previewId).length) return;

var $container = $(this.container);

!$container.length && console && console.log('can not find element, param "container" need checked.');

var $zoomDiv = $('<div /></div></div>');

$zoomDiv.css({"position":"absolute","top":$container.offset().top,"width":this.prevW, "height":this.prevH, "overflow":"hidden", "display":"none", "border":"1px solid #aaa", "zIndex":1000}).appendTo('body');

$container.css({"position":"relative"});

var $holder = $('<div class="zoomglass" : this.prevH / this.scale}).appendTo('body');

if( this.dir == 'right'){

$zoomDiv.css({'left': $container.offset().left + $container.outerWidth() + this.margin});

}else if(this.dir == 'bottom'){

$zoomDiv.css({"top": $container.offset().top + $container.outerHeight() + this.margin, "left": $container.offset().left });

}

this.zoomDiv = $zoomDiv;

this.holder = $holder;

this.container = $(this.container);

},

bindEvents: function(){

var $simg, scale = this.scale, $zoomDiv = this.zoomDiv, $wrapper = $zoomDiv.find('.zoomDiv-con');

var $container = this.container, $holder = this.holder;

var offset = $container.offset();

var halfW = $holder.outerWidth()/2, halfH = $holder.outerHeight()/2;

var leftMin = offset.left+halfW, leftMax = leftMin+$container.width()-2*halfW;

var topMin = offset.top+halfH, topMax = topMin + $container.height()-2*halfH;

var leftBind = leftMin - halfW, rightBind = leftMax + halfW;

var topBind = topMin - halfH, btmBind = topMax + halfH;

var diffX= leftMax - leftMin, diffY = topMax -topMin;

var min = Math.min, max= Math.max;

// debugger;

$container.mouseenter(function(e){//初始化 大图

$holder.show(); $zoomDiv.show();

if($(e.target).hasClass('zoomimg')){

$simg = $(e.target);

$wrapper.css({"width": scale * $simg.outerWidth(), "height":scale * $simg.outerHeight()});//大图的大小

$zoomDiv.find('.bigimg').attr('src', $simg.attr('src')); //大图路径

}

});

$('body').mousemove(function(e){

// $holder.css({'left':e.pageX-halfW, 'top':e.pageY-halfH});

var x,y,position;

if(e.pageX < leftBind || e.pageX > rightBind || e.pageY < topBind || e.pageY > btmBind){

if(!$holder.ishide){ $holder.ishide=true; $holder.hide(); $zoomDiv.hide();}

return;

}else {

$holder.ishide = false;

x = min( max(e.pageX-halfW, leftBind), leftMax-halfW);

y = min( max(e.pageY-halfH, topBind), topMax-halfH);

$holder.css({"left":x,"top":y});

position = $holder.position();

$wrapper.css({"marginLeft":scale * (offset.left - position.left), "marginTop": scale * (offset.top - position.top)});

}

});

}

}

// $('.viewport').css({"outline":"1px solid red"});

var iz = new Imgzoom({scale:3, prevW:500, prevH:500});