jQuery 图像悬停效果
我正在尝试使用 jQuery 实现这种效果。
我写了一些代码,但它有错误(移至右下角,你就会看到)。
检查一下
基本上,如果您知道有一个已经构建的 jQuery 插件可以做到这一点,我我会很高兴使用它,如果没有,任何有关我的公式的帮助将不胜感激。这就是我在数学课上不专心所得到的结果:)
提前致谢。
麦克尔
I'm trying to achieve this effect with jQuery.
I wrote some of the code, but it's buggy (move to the bottom-right corder and you'll see).
check it out
Basically, if there's an already-built jQuery plugin that you know of that does this, I'd be very happy using it, if not, any help with my formula would be appreciated. This is what I get for not paying attention in Maths classes :)
Thanks in advance.
Maikel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
总的来说,我认为这就是您正在寻找的:
您可以在此处进行测试。
显着变化:
new_x
和new_y
应除以图像高度/宽度,而不是容器的高度/宽度,容器的高度/宽度更宽。this
已经是$.fn.plugin
函数中的 jQuery 对象,无需包装它。i
和p
也是 jQuery 对象,无需继续包装它们mouseenter< 上绑定
mousemove
/code> (重新绑定)mousemove
仅当您在室内时才会发生。Overall I think this is what you're looking for:
You can test it here.
Notable changes:
new_x
andnew_y
should be divided by the images height/width, not the container's height/width, which is wider.this
is already a jQuery object in a$.fn.plugin
function, no need to wrap it.i
andp
were also jQuery objects, no need to keep wrapping themmousemove
onmouseenter
(which rebinds) themousemove
will only occur when you're inside anyway.Nick Craver 比我早了大约 10 分钟给出了答案,但这是我的代码,使用
background-image
来定位图像而不是实际图像。存在大量变量是因为
mousemove
事件处理程序必须尽可能高效。它的限制性稍大一些,因为您需要知道尺寸,但我认为可以轻松更改代码以使用可以轻松计算尺寸的img
。一个简单的演示: http://www.jsfiddle.net/yijian/fq2te/1/
Nick Craver beat me to an answer by about 10 minutes, but this is my code for this, using
background-image
to position the image instead of an actual image.The huge amount of variables are there because the
mousemove
event handler has to be as efficient as possible. It's slightly more restrictive, because you need to know the dimensions, but I think the code can be easily altered to work withimg
s for which the size can be calculated easily.A simple demo of this: http://www.jsfiddle.net/yijiang/fq2te/1/