如何使用 jQuery 正确平移大于其容器的图像?

发布于 2024-12-13 17:05:30 字数 1263 浏览 1 评论 0原文

我正在创建一个非常酷的图像查看器,但卡在一个特定的部分:放大时平移图像。这似乎是一个微不足道的问题,我已经尝试了几乎所有类似问题的答案,但每次都会出现一些问题工作不正常。我需要一双新鲜的眼睛。

我暂时在我的开发服务器上打开了一个 URL。看看这个页面:

[URL 已关闭]

接下来,向上移动鼠标滚轮以触发缩放。我们到了。放大后,单击并拖动以尝试平移图像。平移一切正常,但有些地方不太对劲。这是目前用于平移的代码:

var clicking = false;
var previousX;
var previousY;

$("#bigimage").mousedown(function(e) {

    e.preventDefault();
    previousX = e.clientX;
    previousY = e.clientY;
    clicking = true;
});

$(document).mouseup(function() {
    clicking = false;
});

$("#bigimage").mousemove(function(e) {

    if (clicking) {
        e.preventDefault();
        var directionX = (previousX - e.clientX) > 0 ? 1 : -1;
        var directionY = (previousY - e.clientY) > 0 ? 1 : -1;
        $("#bigimage").scrollLeft($("#bigimage").scrollLeft() + 10 * directionX);
        $("#bigimage").scrollTop($("#bigimage").scrollTop() + 10 * directionY);
        previousX = e.clientX;
        previousY = e.clientY;
    }
});

我正在寻找的解决方案具有以下特征:

  • 在 X 和 Y 轴上平移的正确方向
  • 不可能平移到图像边缘之外
  • 相当流畅的平移
  • 很高兴拥有:窗口调整大小不应引起任何问题

虽然我很感谢我能得到的任何帮助,但请不要向我指出通用插件,我已经尝试了太多插件,我正在寻找适合我的特定场景的答案。我非常绝望,我什至会为满足上述特征的完美解决方案设置奖金。

PS:请在 Firefox 或 Webkit 浏览器中尝试该链接

I'm creating quite a cool image viewer but am stuck in one particular part: panning the image when zoomed in. It seems a trivial problem and I've tried out pretty much all answers to similar questions on SO, but each time, something isn't working right. I need a fresh pair of eyes.

I've temporarily opened a URL on my dev server. Have a look at this page:

[URL closed]

Next, move up your mouse wheel to trigger the zoom. Here we are. Once zoomed in, click and drag to try and pan the image. It is panning alright, but something isn't right. This is currently the code used for the panning:

var clicking = false;
var previousX;
var previousY;

$("#bigimage").mousedown(function(e) {

    e.preventDefault();
    previousX = e.clientX;
    previousY = e.clientY;
    clicking = true;
});

$(document).mouseup(function() {
    clicking = false;
});

$("#bigimage").mousemove(function(e) {

    if (clicking) {
        e.preventDefault();
        var directionX = (previousX - e.clientX) > 0 ? 1 : -1;
        var directionY = (previousY - e.clientY) > 0 ? 1 : -1;
        $("#bigimage").scrollLeft($("#bigimage").scrollLeft() + 10 * directionX);
        $("#bigimage").scrollTop($("#bigimage").scrollTop() + 10 * directionY);
        previousX = e.clientX;
        previousY = e.clientY;
    }
});

The solution I'm looking for has these characteristics:

  • Correct direction of panning over the X and Y axis
  • It should not be possible to pan outside the edges of the image
  • Reasonably fluent panning
  • Nice to have: window resize should not cause any issues

Although I appreciate any help I can get, please don't point me to a generic plugin, I've tried too many of them that I am in search of an answer that works for my specific scenario. I'm so desperate I'll even set a money bounty for the perfect solution that meets the characteristic above.

PS: Please try the link in Firefox or a Webkit browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

苦妄 2024-12-20 17:05:30

我已经组装了一个 jsFiddle ,它可以完成我认为你想要它做的事情。

http://jsfiddle.net/CqcHD/2/

它满足您的所有 4 个标准。如果我误解了您的预期结果,请告诉我

I have put together a jsFiddle which does what I think you want it to do.

http://jsfiddle.net/CqcHD/2/

It satisfies all 4 of your criteria. Let me know if I have misinterpreted your expected result

恰似旧人归 2024-12-20 17:05:30

我有一些适合你工作的替代插件。尝试一下这些 jquery 插件。

http://kvcodes.com/2014/02/jquery-plugins- for-containerdiv-zoom/

i have some alternative plugins for your work . try among these jquery plugins.

http://kvcodes.com/2014/02/jquery-plugins-for-containerdiv-zoom/

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