jQuery 图像悬停效果

发布于 2024-10-02 05:16:13 字数 359 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七秒鱼° 2024-10-09 05:16:13

总的来说,我认为这就是您正在寻找的:

$.fn.sexyImageHover = function() { 
    var p = this, // parent
        i = this.children('img'); // image

    i.load(function(){
        // get image and parent width/height info
        var pw = p.width(),
            ph = p.height(),
            w = i.width(),
            h = i.height();

        // check if the image is actually larger than the parent
        if (w > pw || h > ph) {
            var w_offset = w - pw,
                h_offset = h - ph;

            // center the image in the view by default
            i.css({ 'margin-top':(h_offset / 2) * -1, 'margin-left':(w_offset / 2) * -1 });

            p.mousemove(function(e){
                var new_x = 0 - w_offset * e.offsetX / w,
                    new_y = 0 - h_offset * e.offsetY / h;

                i.css({ 'margin-top':new_y, 'margin-left':new_x });
            });
        }
    });
}

您可以在此处进行测试

显着变化:

  • new_xnew_y 应除以图像高度/宽度,而不是容器的高度/宽度,容器的高度/宽度更宽。
  • this 已经是 $.fn.plugin 函数中的 jQuery 对象,无需包装它。
    • ip 也是 jQuery 对象,无需继续包装它们
  • 不需要在 mouseenter< 上绑定 mousemove /code> (重新绑定)mousemove 仅当您在室内时才会发生。

Overall I think this is what you're looking for:

$.fn.sexyImageHover = function() { 
    var p = this, // parent
        i = this.children('img'); // image

    i.load(function(){
        // get image and parent width/height info
        var pw = p.width(),
            ph = p.height(),
            w = i.width(),
            h = i.height();

        // check if the image is actually larger than the parent
        if (w > pw || h > ph) {
            var w_offset = w - pw,
                h_offset = h - ph;

            // center the image in the view by default
            i.css({ 'margin-top':(h_offset / 2) * -1, 'margin-left':(w_offset / 2) * -1 });

            p.mousemove(function(e){
                var new_x = 0 - w_offset * e.offsetX / w,
                    new_y = 0 - h_offset * e.offsetY / h;

                i.css({ 'margin-top':new_y, 'margin-left':new_x });
            });
        }
    });
}

You can test it here.

Notable changes:

  • new_x and new_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 and p were also jQuery objects, no need to keep wrapping them
  • no need to bind mousemove on mouseenter (which rebinds) the mousemove will only occur when you're inside anyway.
心碎无痕… 2024-10-09 05:16:13

Nick Craver 比我早了大约 10 分钟给出了答案,但这是我的代码,使用 background-image 来定位图像而不是实际图像。

var img = $('#outer'),
    imgWidth = 1600,
    imgHeight = 1200,
    eleWidth = img.width(),
    eleHeight = img.height(),
    offsetX = img.offset().left,
    offsetY = img.offset().top,
    moveRatioX = imgWidth / eleWidth - 1,
    moveRatioY = imgHeight / eleHeight - 1;


img.mousemove(function(e){
    var x = imgWidth - ((e.pageX - offsetX) * moveRatioX),
        y = imgHeight - ((e.pageY - offsetY) * moveRatioY);
    this.style.backgroundPosition =  x + 'px ' + y + 'px';
});

存在大量变量是因为 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.

var img = $('#outer'),
    imgWidth = 1600,
    imgHeight = 1200,
    eleWidth = img.width(),
    eleHeight = img.height(),
    offsetX = img.offset().left,
    offsetY = img.offset().top,
    moveRatioX = imgWidth / eleWidth - 1,
    moveRatioY = imgHeight / eleHeight - 1;


img.mousemove(function(e){
    var x = imgWidth - ((e.pageX - offsetX) * moveRatioX),
        y = imgHeight - ((e.pageY - offsetY) * moveRatioY);
    this.style.backgroundPosition =  x + 'px ' + y + 'px';
});

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 with imgs for which the size can be calculated easily.

A simple demo of this: http://www.jsfiddle.net/yijiang/fq2te/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文