使用 jQuery Draggable 在 div 剪贴蒙版中拖动缩放图像?

发布于 2024-08-07 18:50:24 字数 734 浏览 5 评论 0原文

我正在创建一个界面,允许用户放大图像并在剪切蒙版 div 内拖动缩放版本。

我有一个 div 200px x 200px,我将其设置为 Overflow:hidden。然后我将一个 std img () 加载到 div 中,即 1000px x 1000px

然后我使用 jQuery 对

$("#my-image").draggable({ containment: [-85,83,99,222] });

数字进行硬编码。到目前为止,我只能通过反复试验找到它们...

问题是,每次我对页面进行更改(即在容器 div 上方插入另一个元素)时,页面大小都会发生变化,并且我的硬编码 [x1, y1, x2, y2] 停止正常工作。

主要问题是我不完全理解 [x1, y1, x2, y2] ...

这是关于此的 jQuery 文档:

http://docs.jquery.com/UI/Draggable#option-containment

我是否正确地认为 x1 是最左边的可拖动点,x2 是最右边的可拖动点观点? (y1 和 y2 也一样)?

如果是这样,动态计算它们的最佳策略是什么?

此外,对于“在 div 剪贴蒙版内拖动缩放图像”这一主要问题的任何其他快速且简单的解决方案将不胜感激。

I'm creating an interface to allow a user to zoom in on an image and drag the zoomed version around within a clipping mask div.

I have a div 200px by 200px that I set overflow: hidden on. Then I load a std img (<img src="etc">) into the div that is, say, 1000px by 1000px.

Then I use jQuery

$("#my-image").draggable({ containment: [-85,83,99,222] });

The numbers are hard coded. So far I can only find them by trial and error...

The problem is that each time I make a change to the page (Ie insert another element above my container div) the page size changes and my hard coded [x1, y1, x2, y2] stop working correctly.

The main issue is I don't fully understand [x1, y1, x2, y2] ...

Here is the jQuery docs regarding this:

http://docs.jquery.com/UI/Draggable#option-containment

Am I right in thinking that x1 is the left most draggable point, x2 is the right most draggable point? (and same for y1 & y2)?

If so, what would be the best strategy to calculate them dynamically?

Also, any other quick and easy solutions to the main problem of "Drag a zoomed image within a div clipping mask" would be much appreciated.

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

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

发布评论

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

评论(2

桃扇骨 2024-08-14 18:50:24
$(this).draggable({
    drag: function(event, ui) {
        if (ui.position.top > 0) {
            ui.position.top = 0;
        }
        var maxtop = ui.helper.parent().height() - ui.helper.height();
        if ( ui.position.top < maxtop) {
            ui.position.top = maxtop;
        }
        if ( ui.position.left > 0) {
            ui.position.left = 0;
        }
        var maxleft = ui.helper.parent().width() - ui.helper.width();
        if ( ui.position.left < maxleft) {
            ui.position.left = maxleft;
        }
    }
});
$(this).draggable({
    drag: function(event, ui) {
        if (ui.position.top > 0) {
            ui.position.top = 0;
        }
        var maxtop = ui.helper.parent().height() - ui.helper.height();
        if ( ui.position.top < maxtop) {
            ui.position.top = maxtop;
        }
        if ( ui.position.left > 0) {
            ui.position.left = 0;
        }
        var maxleft = ui.helper.parent().width() - ui.helper.width();
        if ( ui.position.left < maxleft) {
            ui.position.left = maxleft;
        }
    }
});
み格子的夏天 2024-08-14 18:50:24

好的。我现在已经开始工作了。这是如何在 div 剪贴蒙版中设置可拖动图像,以便它完全动态并且无论您如何调整页面大小都可以工作。

HTML/CSS

<div id="my-mask" style="width: 200px; height: 200px; overflow: hidden;">
   <img id="my-image" src="big-image.jpg" width="1000" height="1000"/>
</div>

jQuery/JavaScript

// Make sure it always starts @ zero position for below calcs to work
$("#my-image").css({top: 0, left: 0});

var maskWidth  = $("#my-mask").width();
var maskHeight = $("#my-mask").height();
var imgPos     = $("#my-image").offset();
var imgWidth   = $("#my-image").width();
var imgHeight  = $("#my-image").height();

var x1 = (imgPos.left + maskWidth) - imgWidth;
var y1 = (imgPos.top + maskHeight) - imgHeight;
var x2 = imgPos.left;
var y2 = imgPos.top;

$("#my-image").draggable({ containment: [x1,y1,x2,y2] });
$("#my-image").css({cursor: 'move'});

希望这对某人有帮助!

Ok. I've got this working now. This is how to set up a draggable image within a div clipping mask so that it is completely dynamic and works no matter how you resize the page.

The HTML/CSS

<div id="my-mask" style="width: 200px; height: 200px; overflow: hidden;">
   <img id="my-image" src="big-image.jpg" width="1000" height="1000"/>
</div>

The jQuery/JavaScript

// Make sure it always starts @ zero position for below calcs to work
$("#my-image").css({top: 0, left: 0});

var maskWidth  = $("#my-mask").width();
var maskHeight = $("#my-mask").height();
var imgPos     = $("#my-image").offset();
var imgWidth   = $("#my-image").width();
var imgHeight  = $("#my-image").height();

var x1 = (imgPos.left + maskWidth) - imgWidth;
var y1 = (imgPos.top + maskHeight) - imgHeight;
var x2 = imgPos.left;
var y2 = imgPos.top;

$("#my-image").draggable({ containment: [x1,y1,x2,y2] });
$("#my-image").css({cursor: 'move'});

Hope this helps someone!

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