Common js magnifiers on e-commerce websites

Source: Internet
Author: User

Copy codeThe Code is as follows: jsFiddleRun again Edit this fiddle Result HTML
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "gb2312"/>
<Title> magnifiers </title>
<Style>
Img {vertical-align: bottom ;}
. Mod_zoom {overflow: hidden; zoom: 1 ;}
. Mod_zoom. p1 {float: left; position: relative; height: 350px; width: 350px; margin-right: 5px ;}
. Mod_zoom. p1. mask {position: absolute; left: 0; top: 0; width: 175px; height: 175px; background-color: # fede4f; opacity: 0.3; filter: alpha (opacity = 30); display: none ;}
. Mod_zoom. p2 {position: relative; width: 400px; height: 400px; overflow: hidden; display: none ;}
. Mod_zoom. p2. img {position: absolute; left: 0; top: 0 ;}
. Mod_zoom. ph {width: 100%; height: 350px; position: absolute; top: 0; left: 0; cursor: crosshair;
/* Background-color: red */
/* If you bind an event handler to it, the event will not be triggered if the background-color attribute is not set in IE */}
</Style>
</Head>
<Body>
<Div>
<Div class = "mod_zoom">
<Div class = "p1" id = "p1">

<Span class = "mask" id = "m"> </span>
<Span class = "ph" id = "eventproxy"> </span>
</Div>
<Div class = "p2" id = "p2">

</Div>
</Div>
</Div>
<Script>
Function PhotoZoomer (elements ){
This. mask = elements. mask; // mask
This. container = elements. container // source image container
This. originimg = elements. originimg; // source Image
This. eventproxy = elements. eventproxy;
This. bigContainer = elements. bigContainer; // large image container
This. bigimg = elements. bigimg; // large image
This. visible = false;
This. _ bind ();
}
PhotoZoomer. prototype = {
Display: function (style ){
Var self = this;
Self. mask. style. display = style;
Self. bigContainer. style. display = style;
},
// Calculate the position of the zoom in mask
Zoom: function (clientX, clientY ){
Var self = this,
// Location Ratio
Rate = {},
// Enlarge the maximum activity range of the mask.
Maxrange = {
OffsetLeft: self. container. offsetWidth-self. mask. offsetWidth,
OffsetTop: self. container. offsetHeight-self. mask. offsetHeight
},
// Mask left
Left = clientX-self. container. offsetLeft-self. mask. offsetWidth/2,
// Mask top
Top = clientY-self. container. offsetTop-self. mask. offsetHeight/2;
If (left <0 ){
Left = 0;
} Else if (left> maxrange. offsetLeft ){
Left = maxrange. offsetLeft;
}
If (top <0 ){
Top = 0;
} Else if (top> maxrange. offsetTop ){
Top = maxrange. offsetTop;
}
// Alert (maxrange. offsetTop );
Rate. left = left/maxrange. offsetLeft;
Rate. top = top/maxrange. offsetTop;
Self. mask. style. left = left + 'px ';
Self. mask. style. top = top + 'px ';
Self. bigimg. style. left =-rate. left * (self. bigimg. offsetWidth-self. bigContainer. offsetWidth) + "px ";
Self. bigimg. style. top =-rate. top * (self. bigimg. offsetHeight-self. bigContainer. offsetHeight) + "px ";
},
_ Bind: function (){
Var self = this;
Self. container. onmouseover = function (e ){
E = e | window. event;
Var target = e.tar ge | e. srcElement;
Self. display ("block ");
This. visible = true;
};
Self. container. onmouseout = function (e ){
E = e | window. event;
Var target = e.tar ge | e. srcElement;
Self. display ("none ");
This. visible = false;
};
Self. container. onmousemove = function (e ){
E = e | window. event;
If (! This. visible) return; // prevents miscalculation of element size
Self. zoom (e. clientX, e. clientY );
};
}
};
Function get (id ){
Return document. getElementById (id)
}
Var elements = {
Mask: get ("m "),
Container: get ("p1 "),
Originimg: get ("z1 "),
BigContainer: get ("p2 "),
Bigimg: get ("z2 "),
Eventproxy: get ("eventproxy ")
};
Var zoomer = new PhotoZoomer (elements );
// Alert (elements. container. offsetParent. tagName)
</Script>
</Body>
</Html>
<! DOCTYPE html>
<Html>
<Head>
<Meta charset = "gb2312"/>
<Title> magnifiers </title>
<Style>
Img {vertical-align: bottom ;}
. Mod_zoom {overflow: hidden; zoom: 1 ;}
. Mod_zoom. p1 {float: left; position: relative; height: 350px; width: 350px; margin-right: 5px ;}
. Mod_zoom. p1. mask {position: absolute; left: 0; top: 0; width: 175px; height: 175px; background-color: # fede4f; opacity: 0.3; filter: alpha (opacity = 30); display: none ;}
. Mod_zoom. p2 {position: relative; width: 400px; height: 400px; overflow: hidden; display: none ;}
. Mod_zoom. p2. img {position: absolute; left: 0; top: 0 ;}
. Mod_zoom. ph {width: 100%; height: 350px; position: absolute; top: 0; left: 0; cursor: crosshair;
/* Background-color: red */
/* If you bind an event handler to it, the event will not be triggered if the background-color attribute is not set in IE */}
</Style>
</Head>
<Body>
<Div>
<Div class = "mod_zoom">
<Div class = "p1" id = "p1">

<Span class = "mask" id = "m"> </span>
<Span class = "ph" id = "eventproxy"> </span>
</Div>
<Div class = "p2" id = "p2">

</Div>
</Div>
</Div>
<Script>
Function PhotoZoomer (elements ){
This. mask = elements. mask; // mask
This. container = elements. container // source image container
This. originimg = elements. originimg; // source Image
This. eventproxy = elements. eventproxy;
This. bigContainer = elements. bigContainer; // large image container
This. bigimg = elements. bigimg; // large image
This. visible = false;
This. _ bind ();
}
PhotoZoomer. prototype = {
Display: function (style ){
Var self = this;
Self. mask. style. display = style;
Self. bigContainer. style. display = style;
},
// Calculate the position of the zoom in mask
Zoom: function (clientX, clientY ){
Var self = this,
// Location Ratio
Rate = {},
// Enlarge the maximum activity range of the mask.
Maxrange = {
OffsetLeft: self. container. offsetWidth-self. mask. offsetWidth,
OffsetTop: self. container. offsetHeight-self. mask. offsetHeight
},
// Mask left
Left = clientX-self. container. offsetLeft-self. mask. offsetWidth/2,
// Mask top
Top = clientY-self. container. offsetTop-self. mask. offsetHeight/2;
If (left <0 ){
Left = 0;
} Else if (left> maxrange. offsetLeft ){
Left = maxrange. offsetLeft;
}
If (top <0 ){
Top = 0;
} Else if (top> maxrange. offsetTop ){
Top = maxrange. offsetTop;
}
// Alert (maxrange. offsetTop );
Rate. left = left/maxrange. offsetLeft;
Rate. top = top/maxrange. offsetTop;
Self. mask. style. left = left + 'px ';
Self. mask. style. top = top + 'px ';
Self. bigimg. style. left =-rate. left * (self. bigimg. offsetWidth-self. bigContainer. offsetWidth) + "px ";
Self. bigimg. style. top =-rate. top * (self. bigimg. offsetHeight-self. bigContainer. offsetHeight) + "px ";
},
_ Bind: function (){
Var self = this;
Self. container. onmouseover = function (e ){
E = e | window. event;
Var target = e.tar ge | e. srcElement;
Self. display ("block ");
This. visible = true;
};
Self. container. onmouseout = function (e ){
E = e | window. event;
Var target = e.tar ge | e. srcElement;
Self. display ("none ");
This. visible = false;
};
Self. container. onmousemove = function (e ){
E = e | window. event;
If (! This. visible) return; // prevents miscalculation of element size
Self. zoom (e. clientX, e. clientY );
};
}
};
Function get (id ){
Return document. getElementById (id)
}
Var elements = {
Mask: get ("m "),
Container: get ("p1 "),
Originimg: get ("z1 "),
BigContainer: get ("p2 "),
Bigimg: get ("z2 "),
Eventproxy: get ("eventproxy ")
};
Var zoomer = new PhotoZoomer (elements );
// Alert (elements. container. offsetParent. tagName)
</Script>
</Body>
</Html>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.