html 鼠标悬停时缩放
我想在网站上放一张照片,当我将鼠标悬停在上面时,我想要一点放大镜缩放效果。
我可以用canvas之类的html来做到这一点吗?也许是JavaScript?
谢谢
I want to have a photo in a site, and when I mouse over, I want a little magnifier zoom effect.
Can I do this in html with canvas or something? javascript maybe?
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将您的照片放在 div 中,并在悬停时通过 CSS 添加 缩放。您可能希望在悬停时增加 z-index。您可以添加以下 CSS 以使缩放照片的外观/风格更好。如果您不想重新发明轮子,请寻找一些 Jquery 插件,它可以以优雅的方式轻松完成相同的事情。
CSS:
Jquery/Javascript:
Enclose your photo in a div and add Zoom via CSS on hover. You may want to increase the z-index upon hover. You can add to the below CSS to make the zoomed photo look/style better. If you don't want to reinvent the wheel, look out for some Jquery plugin which may accomplish the same thing in an elegant manner with less effort.
CSS:
Jquery/Javascript:
如果没有第三方插件,IE 不支持 Canvas。您需要在 JavaScript 中执行此操作。 jQuery 使这变得非常简单和干净。使用
.hover()
为要缩放的图像元素绑定悬停入/出事件的处理程序。 “绑定处理程序”只是意味着您将两个函数传递给 .hover() ,这两个函数将分别在用户将鼠标悬停在内部和外部时执行。这些函数将使用animate()
,您只需传递一个新的大小即可。查看 .animate() 和 .hover()。如果您对 jQuery 完全陌生,请查看教程。
Canvas isn't supported by IE without third-party plug-ins. You'll want to do this in JavaScript. jQuery makes this very easy and clean. Bind handlers for the hover in / out events for the image element you want to zoom using
.hover()
. "Binding handlers" simply means you pass two functions to .hover() which will be executed when the user hovers in and out, respectively. These functions will useanimate()
, which you can simply pass a new size.Have a look at the documentation for .animate() and .hover(). If you're totally new to jQuery, check out the tutorials.
您可以将 div 中的图像显示为背景图像,并使用一些 javascript 更改位置。
这是一个包含一些示例的库: http://www.mind-projects.it/项目/jqzoom/demos.php
You can show the image in a div as a background-image and change the position with a little javascript.
Here's a library with some examples: http://www.mind-projects.it/projects/jqzoom/demos.php