Let's take a look at the effect:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head> <meta http-equiv =" Content-Type "content =" text/html; charset = GBK "/> <title> Magnifier </title> <style type =" text/css "> # magnifier {width: 342px; height: 420px; position: absolute; top: 100px; left: 250px; font-size: 0; border: 1px solid #000 ;}# img {width: 342px; height: 420px ;}# Browser {border: 1px solid #000; z-index: 100; position: absolute; background: #555 ;}# mag {border: 1px solid #000; overflow: hidden; z-index: 100 ;}</style>
Tip: the code can be modified before running!
[Program description]
The main methods are as follows:
Init: Running Method
Start: process the event of moving the cursor into the div.
Move: The event processing when the mouse moves in the div
End: processing of events after Mouse Removal
[Program Introduction]
Main thinking: When the mouse moves into the image, the DIV of the zoom-in layer appears, and then changes the top and left values of the image in the zoom-in Layer Based on the mouse movement. the reality of keeping two places consistent. the ratio of the two images to the data is set, and the width and height values are used to enlarge the image. the following is a detailed explanation:
In the init method, the size of the div layer of the browser box, the size of the enlarged box, and the size of the enlarged image are mainly processed.
View the width and height of the div and obtain the size/proportion of the original image. For details, see the code:
Css (m. cont. getElementsByTagName ('div ') [0], {// m. cont. getElementsByTagName ('div') [0] is the browser box
'Display': 'none', // start to set to invisible
'Width': m. cont. clientWidth/m. scale-borderWid + 'px', // width/proportion of the original image-border width
'Height': m. cont. clientHeight/m. scale-borderWid + 'px', /// height/proportion of the original image-border Width
'Opacity ': 0.5 // sets the transparency.
})
The size of the enlarged box is set to the same size as the original image. The Code is as follows:
Css (m. mag ,{
'Display': 'none ',
'Width': m. cont. clientWidth + 'px ', // m. cont is the original image
'Height': m. cont. clientHeight + 'px ',
'Position': 'absolute ',
'Left': m. cont. offsetLeft + m. cont. offsetWidth + 10 + 'px ', // The Position of the enlarged box is 10px far from the right of the original image.
'Top': m. cont. offsetTop + 'px'
})
The enlarged image size is the ratio of the original image size *. The Code is as follows:
Css (m. img ,{
'Position': 'absolute ',
'Width': (m. cont. clientWidth * m. scale) + 'px', // width * Ratio of the original image
'Height': (m. cont. clientHeight * m. scale) + 'px' // The height * Ratio of the original image
})
Because the scale-up is based on the scale-up, it is necessary to carefully calculate the browsing box and the scaled-up image. This is one of the main ideas of this program.
In the program written for the first time, onmouseover is saved directly, because onmousemove can satisfy the function. onmouseover is used this time to avoid the use of select. in IE6, select cannot set the z-Index value, so that the sudden appearance of the enlarged box cannot overwrite the select. the details are discussed below.
In the move method, the most important thing is that when you move the mouse, the browsing box moves with the mouse, and the enlarged image also moves, make the displayed range of the enlarged image consistent with the position of the original image in the browser box.
Let's talk about the browser box moving with the mouse. The main code is as follows:
Top: pos. y-this. offsetTop-parseInt (this. getElementsByTagName ('div ') [0]. style. height)/2
Left: pos. x-this. offsetLeft-parseInt (this. getElementsByTagName ('div ') [0]. style. width)/2
Because yes, the event is bound to m. cont, so this points to m. cont.
From the image, we can see that left = mouse x-this. offsetLeft-the browser box width/2, so the code can be obtained based on this geometric idea, and the top value is also obtained based on the same principle. I will not explain it in detail here. the following code changes the top and left values of the enlarged image while moving the mouse:
Css (magnifier. m. img ,{
'Top':-(parseInt (this. getElementsByTagName ('div ') [0]. style. top) * magniier. m. scale) + 'px ',
'Left':-(parseInt (this. getElementsByTagName ('div ') [0]. style. left) * magniier. m. scale) + 'px'
})
The code is clear and can be obtained. You only need to set the * Ratio on the top and left values of the browser box. the reason for adding a negative number is that the default coordinate is (0, 0), while during the moving process, the start coordinate will only move in the negative direction.
There are two points to note in this method:
1. this. getElementsByTagName ('div ') [0]. style. display = '';
Set this. before the top and left of getElementsByTagName ('div ') [0], the reason is that if the display is none, its width and height cannot be obtained. if you put display = ''after setting top and left, a strange phenomenon may occur. You can try it. This problem has been bothering me for a long time, the problem was discovered only after multiple attempts. The phenomenon is as follows:
2. 'top': Math. min (Math. max (pos. y-this. offsetTop-parseInt (this. getElementsByTagName ('div ') [0]. style. height)/2, 0), this. clientHeight-this. getElementsByTagName ('div ') [0]. offsetHeight) + 'px ';
This long code may be confusing. I just use Math. max () and Math. min () avoids the use of the if statement. I am a little lazy, just to make sure that the browser box does not go beyond the original image. ^
The end method is clear, that is, to hide the browser box and the enlarged box.
[Overwrite select]
In order to overwrite select in IE6, I added two release methods createIframe and removeIframe. They created an iframe IN THE onmouseover event and destroyed iframe in onmouseout.
CreateIframe: function (elem ){
Var layer = document. createElement ('iframe ');
Layer. tabIndex = '-1 ';
Layer. src = 'javascript: false ;';
Elem. parentNode. appendChild (layer );
Layer. style. width = elem. offsetWidth + 'px ';
Layer. style. height = elem. offsetHeight + 'px ';
}
- 2 pages in total:
- Previous Page
- 1
- 2
- Next Page