This article explains how to use css to achieve PictureMagnifying glass effect ? The following is a detailed explanation of the CSS example of the picture magnifying glass effect.
The effect is as shown in the figure (in the example, I secretly linked a picture of Tmall, I hope it’s okay-.-):
The implementation process is simple, but we still start the analysis from css. The process is as follows (the picture is a square as an example):
css:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
What you need to pay attention to in the above css are several positions and scaling ratios. Pay attention to the adjustments.
After writing the style, let’s take a look at the layout:
html:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Next is the main js code, with comments as always:
js:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
|
bug fixing picture:
After reading this, do you feel that it is not too simple? Next, let’s take a look at the more practical knowledge points that can be extracted from the above code in daily development:
Element .getBoundingClientRect()
Element.getBoundingClientRect() method returns the size of the element and its position relative to the viewport
Example:
1 2 3 4 5 6 7 8 9 10 11 |
|
Effect As shown in the picture:
It is not difficult to see from the renderings that when I move the view and then click the body, the printed object Can correctly return the size of the element and its position relative to the viewport
This method can also be used to trigger the corresponding event when an element is scrolled to the bottom/top, which is quite convenient.
1.event.target and event.currentTarget
target: points to the element that triggered the event
currentTarget: points to the element that is bound to the event handle
Only when the bound
event handlerprogram and the program that triggers the event handler are the same object, the two are the same Example code:
html:
1 2 3 4 |
|
js:
1 2 3 4 5 6 7 8 9 |
|
Rendering:
From the rendering, we It can be seen that when 456 is clicked, the target points to the bp where 456 is located, and the currentTarget points to the ap, because the event is bound to the ap, but the trigger is on the bp, and the bp is within the ap. When 123 is clicked , then the target is consistent with currentTarget, and both binding and triggering are on the ap.
2.event.preventDefault() & event.stopPropagation()
preventDefault: If the event can be canceled, cancel the event without stopping the further propagation of the event
stopPropagation: Prevent further propagation of the current event in the capture and bubbling phases
3.event.stopPropagation() & event.stopImmediatePropagation()
stopPropagation: Prevent the current event in the capture and bubbling phases Further propagation
stopImmediatePropagation: Prevent other event listening on the element from calling the same event and prevent bubbling
Example of the difference between the two:
html:
1 2 3 4 |
|
js:
1 2 3 4 5 6 7 8 9 10 |
|
The execution result of the above code is:
1 |
|
Comment out e.stopImmediatePropagation(); The result is:
1 2 3 |
|
Although these are simple knowledge points, they are also very practical in daily development. I hope I can start from the details and review them as much as possible. -~
The above is the detailed content of Detailed explanation of the example of using css to achieve the picture magnifying glass effect (picture). For more information, please follow other related articles on the PHP Chinese website!