简体   繁体   English

使用.offset()使用jQuery缩放图像

[英]Zoom an image with jQuery using .offset()

I've got a very simple bit of code. 我有一个非常简单的代码。 It changes the max-width of the image to give the illusion of zooming in. I am using jQuery's offset() with the mouse's coordinates so the user can pan the image. 它会改变图像的max-width ,从而产生放大的错觉。我使用jQuery的offset()和鼠标的坐标,这样用户就可以平移图像。

http://jsfiddle.net/tmyie/YcbDM/ http://jsfiddle.net/tmyie/YcbDM/

$('.image-container img').mouseover(function () {
    $(this).css('max-width', '200%');
    $(this).mousemove(function (e) {
        $(this).offset({
             top: e.pageY - 300,
            left: e.pageX - 300   
        });
    });
 });

This works exactly as I want it to, however, I'd like the pan to be reversed . 这完全符合我的要求,但是,我希望平移可以反转 Currently, if you move your cursor up, you see the bottom half of the image and vice versa. 目前,如果向上移动光标,则会看到图像的下半部分,反之亦然。 I've tried reversing the offset, like so: 我试过反转偏移量,如下:

             top: e.pageY + 300,
            left: e.pageX + 300 

But the image is removed from view completely. 但图像完全从视图中删除。

If possible I'd like to avoid using a plugin for this. 如果可能的话,我想避免使用插件。

Reversing e.pageX and e.pageY seem to achieve the effect you wanted fairly well 反转e.pageXe.pageY似乎可以很好地达到你想要的效果

$('.image-container img').mouseover(function () {
    $(this).css('max-width', '200%');
    $(this).mousemove(function (e) {
        $(this).offset({
             top: -e.pageY,
            left: -e.pageX

        });
    });
 });

http://jsfiddle.net/zSjL9/ http://jsfiddle.net/zSjL9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM