JS magnifier special effect (compatible Version)

Source: Internet
Author: User

JS magnifier special effect (compatible Version)
Principle 1. when moving the mouse over a small image, capture the position of the mouse over the small image to locate the corresponding position of the large image. page elements: small images, large images, magnifiers 2. technical point: Event capture and positioning 1) onmouseover: it will occur when the mouse pointer moves to the specified object 2) onmouseout: it will occur when the mouse pointer moves out of the specified object 3) onmousemove: it will occur when the mouse pointer moves. 4) Each margin indicates 5) ofgsetLeft and style. left comparison style. left returns a string, for example, 30px. offsetLeft returns 30 styles. left is readable and read-only, and offsetLeft is read-only. Therefore, to change the position of the div, you can only modify the style. left style. the left value must be defined by the event. Otherwise, the obtained value is null. 3. difficulty: Calculate objMark. onmousemove = function (ev) {var _ event = ev | wind Ow. event; // compatible with the event parameter mode of multiple browsers var left = _ event. clientX-objDemo. offsetLeft-objSmallBox. offsetLeft-objFloatBox. offsetWidth/2; var top = _ event. clientY-objDemo. offsetTop-objSmallBox. offsetTop-objFloatBox. offsetHeight/2; // if (left <0) {left = 0;} else if (left> (objMark. offsetWidth-objFloatBox. offsetWidth) {left = objMark. offsetWidth-objFloatBox. offsetWidth;} // If (top <0) {top = 0;} else if (top> (objMark. offsetHeight-objFloatBox. offsetHeight) {top = objMark. offsetHeight-objFloatBox. offsetHeight;} objFloatBox. style. left = left + "px"; // oSmall. the value of offsetLeft is objFloatBox. style. top = top + "px"; var percentX = left/(objMark. offsetWidth-objFloatBox. offsetWidth); var percentY = top/(objMark. offsetHeight-objFloatBox. OffsetHeight); objBigBoxImage. style. left =-percentX * (objBigBoxImage. offsetWidth-objBigBox. offsetWidth) + "px"; objBigBoxImage. style. top =-percentY * (objBigBoxImage. offsetHeight-objBigBox. offsetHeight) + "px" ;}source Code <! Doctype html>

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.