Example code of the jquery image magnifier function

Source: Internet
Author: User

Copy codeThe Code is as follows:/* magnifier */
. ZoomMain {margin: 100px; width: 395px; height: 460px; float: left; position: relative ;}
. ZoomMain. zoom {height: 393px; width: 393px; position: relative; border: 1px solid # dcdddd ;}
. ZoomMain. zoom. move {position: absolute; left: 0; top: 0; display: none; width: 195px; height: 195px; background: #000; opacity: 0.2; filter: alpha (Opacity = 20 );}
. ZoomMain. zoomDetail {display: none; border: 1px solid # DCDDDD; width: 393px; height: 393px; position: absolute; right:-405px; top: 0px; overflow: hidden ;}
. LittleImg {margin-top: 10px; height: 54px; overflow: hidden; position: relative ;}
. LittleImg span {position: absolute; display: block; width: 10px; height: 55px; background: #999; cursor: pointer ;}
. LittleImg span em {display: none; width: 10px; height: 55px ;}
. LittleImg span. btnL {left: 0; background: url (oohdear/images/cssPos/UltimatePageCssPos.gif) no-repeat left top ;}
. LittleImg span. btnL em {background: url (oohdear/images/cssPos/UltimatePageCssPos.gif) no-repeat left-57px ;}
. LittleImg span. btnR em {background: url (oohdear/images/cssPos/UltimatePageCssPos.gif) no-repeat-10px-57px ;}
. LittleImg span. btnR {right: 0; background: url (oohdear/images/cssPos/UltimatePageCssPos.gif) no-repeat-10px top ;}
. LittleImg span. hover em {display: block ;}
. LittleImg. slideMain {width: 343px; height: 55px; margin-left: 26px; overflow: hidden; position: relative ;}
. LittleImg. slideMain ul {position: absolute; left: 0; width: 355px; padding-top: 1px ;}
. LittleImg. slideMain ul li {float: left; margin-right: 6px; cursor: pointer; width: 50px; height: 50px; border: 1px solid # dbdbdb ;}
. LittleImg. slideMain ul li. selected {border-color: #999 ;}
. LittleImg. slideMain ul li img {float: left; width: 50px; height: 50px ;}
/* Magnifier end */
</Style>
</Head>
<Body>
<! -- Magnifier -->
<Div class = "ZoomMain">
<Div class = "zoom">
<Span class = "move"> </span>

</Div>
<Div class = "littleImg">
<Span class = "btnL"> <em> </span>
<Span class = "btnR"> <em> </span>
<Div class = "slideMain">
<Ul class = "clearfix">
<Li class = "selected"> </ li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
<Li> </li>
</Ul>
</Div>
</Div>
<Div class = "zoomDetail">

</Div>
</Div>
<! -- Magnifier end -->
  

? <Script type = "text/javascript">
/**
@ Development: Yang Yong
@ Function: Zoom in on details, switch between images, customize css styles, and freely overlay the view.
@ Note: the jQ-based magnifier plug-in allows you to adjust the layout as needed to adapt to various design effects.
*/
(Function (){
Function Zoom (object ){
This. zoomArea = $ (". zoom", object); // Save the area of the boost and zoom effect.
This. moveArea = $ (". move", object); // Save the moving area
This. zoomDetail = $ (". zoomDetail", object); // Save the magnifier Area
This. zoomDetailImg = $ ("img", this. zoomDetail); // Save the figure in the magnifier
This. zoomAreaWidth = this. zoomArea. width ();
This. moveAreaWidth = this. moveArea. width ();
This. zoomAreaHeight = this. zoomArea. height ();
This. moveAreaHeight = this. moveArea. height ();
This. zoomDetailWidth = this. zoomDetail. width ();
This. zoomDetailHeight = this. zoomDetail. height ();
This. zoomAreaOffset = this. zoomArea. offset (); // initialize the relative offset of the enlarged area in the viewport;
This. XY = null; // initialize the Offset Value of the mouse relative to the enlarged area
This. moveBili = null ;//
Var _ this _ = this;
This. zoomArea. mousemove (function (e) {// execute when the mouse moves in the enlarged area
_ This _. move (e. pageX, e. pageY );
}). Mouseover (function (){
_ This _. moveArea. show ();
_ This _. zoomDetail. show ();
}). Mouseout (function (){
_ This _. moveArea. hide ();
_ This _. zoomDetail. hide ();
});
This. calculate (); // initialize and calculate the required ratio value.
// The following is the function implementation of the thumbnail.
This. l = 0;
This. scrolobj = $ (". slideMain ul", object); // Save the ul scrolling object
This. lis = this. scrolobj. children (); // Save the small image list
This. btnR = $ (". btnR", object); // Save the button on the right
This. btnL = $ (". btnL", object); // Save the left button
This. lis. click (function (){
_ This _. changeImgSrc (this );
});
If (this. lis. length> 6) {// if the number of images exceeds the display area, the scroll event is registered.
This. s = this. lis. length-6; // gets the number of excess images.
This. scrolobj. width (60 * this. lis. length + "px"); // when the number of images exceeds the default value, set the ul width.
This. btnL. click (function () {_ this _. scrollRight ();}). mouseover (function () {$ (this ). addClass ("hover ")}). mouseout (function () {$ (this ). removeClass ("hover ");});
This. btnR. click (function () {_ this _. scrollLeft ();}). mouseover (function () {$ (this ). addClass ("hover ")}). mouseout (function () {$ (this ). removeClass ("hover ");});;
}
};
Zoom. prototype = {
ScrollLeft: function (){
If (Math. abs (this. l) = this. s) {return };
This. l --;
This. scrolobj. animate ({left: this. l * 58 + "px"}, "fast ");
},
ScrollRight: function (){
If (this. l = 0) {return };
This. l ++;
This. scrolobj. animate ({left: this. l * 58 + "px"}, "fast ");
},
ChangeImgSrc: function (o ){
// Change the logo Style
$ (O). addClass ("selected"). siblings (). removeClass ("selected ");
This. zoomArea. find ("img"). attr ("src", $ (o). find ("img"). attr ("medium-img "));
This. zoomDetailImg. attr ("src", $ (o). find ("img"). attr ("medium-img "));

},
Move: function (x, y) {// function executed when the mouse moves in the enlarged area
This. XY = this. mousePosAndSetPos (x, y); // calculates the x and y values of the mouse relative to the enlarged area.
// Set the slider position
This.moveArea.css ({
Left: this. XY. offsetX + "px ",
Top: this. XY. offsetY + "px"
});
// Set the details of a large image.
This.zoomDetailImg.css ({
MarginLeft:-this. XY. offsetX * this. moveBili + "px ",
MarginTop:-this. XY. offsetY * this. moveBili + "px"
});
},
MousePosAndSetPos: function (x, y) {// calculate and set the slider position in real time
X = x-this.zoomAreaOffset.left-this.moveArea.width ()/2;
Y = y-this.zoomAreaOffset.top-this.moveArea.height ()/2;
X = x <0? 0: x;
Y = y <0? 0: y;
X = x> (this. zoomAreaWidth-this.moveAreaWidth )? This. zoomAreaWidth-this.moveAreaWidth: x;
Y = y> (this. zoomAreaHeight-this.moveAreaHeight )? This. zoomAreaHeight-this.moveAreaHeight: y;
Return {
OffsetX: x,
OffsetY: y
};
},
Calculate: function () {// calculate a function
Var widthBili, heightBili;
// Calculate the ratio and height of the slider to the display of the magnifier.
WidthBili = (this. zoomAreaWidth * this. zoomDetailWidth)/this. moveAreaWidth;
HeightBili = (this. zoomAreaHeight * this. zoomDetailHeight)/this. moveAreaHeight;
// Compare the width and height
This.zoomDetailImg.css ({width: widthBili + "px", height: heightBili + "px "});
// Returns the ratio of the move.
This. moveBili = (widthBili-this.zoomDetailWidth)/(this. zoomAreaWidth-this.moveAreaWidth );
}
};
Var zoom = new Zoom ($ (". ZoomMain"). eq (0 ));
})();

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.