How to simulate magnifying glass on Web-page image (Javascript)?

痴心易碎 提交于 2019-11-26 22:53:59

问题


Google has the coolest effects - once it was a Pac-man game, today is apparently the 160th anniversary of the first World Fair, and Google's logo has an image of it. They also turn the mouse into a magnifying glass that can sweep over the picture (the gold ring).

I'm wondering how they do that. It's obviously Javascript, and I looked at the page source, but it's not especially readable (no surprise).


回答1:


Looking at their source code, it seems they are using rather basic techniques to achieve this.

Ignoring all the embedded nifty animated gif's, there are basically two images - large, and small of the entire scene. The larger image is repeated thrice in the document. Look at the annotated image below to get a better idea of how the zoom works.

The portion inside the magnifying circle is split up in three div's - top, mid, and bottom. The overflow for each div should be hidden. Each div is relatively positioned inside the zoom circle. On mouse move, change the absolute position of the zoom circle to the mouse coordinates. Their example also uses CSS3 for the scaling and adding some animation delays.

Here's a sorta minimal reconstructed example.

Another example where we don't hide the div overflow to reveal the entire thing as a square.




回答2:


Well, firstly, for anyone who wants to use such an effect, there are loads of jQuery plugins. Here are just a few:

  1. Power Zoomer
  2. Featured Image Zoomer
  3. Cloud Zoom

Secondly, it's quite easy to achieve. Just load the full-size image, but give it a width smaller than it's actual width, so it is scaled by CSS. Then, use JavaScript+CSS to create a Div (the magnifying glass) with the same image as background, but change the background-position property to the corresponding scaled x,y coordinate that the user's mouse is currently on.

There are other ways of doing it I suppose, and Google might be doing it differently, but this is the most obvious way for me that comes to mind.




回答3:


Visit http://codeblab.com/glass/ for an real life example and in depth explanation of this technique. Flash and CSS v3 have ample functionality to construct a round magnifying glass. However, simulate-a-circle-with-overlapping-rectangles works on (many) more platforms.

(DISCLOSURE: codeblab.com is my personal hobby blog with some weak links to my work in The Netherlands.)




回答4:


There is a full example of magnifying any sort of HTML, including HTML5 at http://www.aplweb.co.uk/blog/js/magnifying-glass/. Works cross-browser too - although rounded corners are a bit iffy on most browsers - so you are going to have to use a box rather than circle.

Here is how to works:

  1. Duplicate the content you want to zoom
  2. Place the duplicated content into another element and set the visible width/height and overflow hidden
  3. Use JavaScript to move the duplicated content so that it moves by the zoom amount * mouse movement. Also move the visible div by the mouse movement.

That is pretty much it too it. There are lots of little things to look out for though to make it work on all browsers.




回答5:


I don't know how Google does it, since the logo is no longer showing in my area; but this effect can be achieved by clipping the enlarged animated GIF over the regular image using canvas. Alternatively, it is also possible to create create a circular clipping using CSS border-radius (commonly used to implement rounded corners).

EDIT: I've hacked this up together to show the basic technique that you need if you used CSS border-radius: http://jsfiddle.net/yjBuS/




回答6:


Looks like they're using two images, one for the logo and one for the zoom (the zoomed one is actually sliced, to run the animations separately...?) They probably detect if the mouse is over the normal logo, then show the yellow circle and attach it to the mouse position. Then showing the other image, shifting it opposite of the mouse. Or something similar.



来源:https://stackoverflow.com/questions/5849438/how-to-simulate-magnifying-glass-on-web-page-image-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!