jQuery UI 可拖动约束

发布于 2024-10-21 06:13:59 字数 388 浏览 1 评论 0原文

我想做的是在一个较小的 div 中包含一个大图像,用户可以在包含的 div 中拖动它(足够简单)...类似于 http://oneblackbear.com/draggable/index.html 但我想防止用户将其拖动到容器边界之外。通过上面的示例,用户可以将图像完全拖到包含的 div 之外...我想阻止用户将图像拖到父 div 之外。

我尝试过使用 jQuery UI 可拖动,但问题是,如果我在拖动图像时使用约束选项,它会锁定到右下角,并且不再可拖动,因为子元素大于父约束。我不确定 ui 可拖动约束是否仅适用于比父容器更小的对象。

有谁有什么想法怎么办?最好使用 jQuery Draggable?

what I am trying to do is have an large image contained within a smaller div that the user can drag around within the containing div (easy enough)... similar to http://oneblackbear.com/draggable/index.html but I want to prevent users from dragging it any further then the container boundary. With the above example the user can drag the image completely outside of the containing div... I want to prevent the user from ever dragging the image outside of the parent div at all.

I have tried using jQuery UI draggable but the problem is if I use the constraint option as soon as you drag the image it locks to the bottom right and is no longer draggable because the child element is larger then the parent constraint. I am not sure if the ui draggable constraint is only intended for smaller objects then the parent container.

Does anyone have any ideas how do do with? preferably with jQuery Draggable?

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

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

发布评论

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

评论(2

欲拥i 2024-10-28 06:13:59
var productHeadOffset = jQuery('#productHead').offset();
var productHeadHeight = jQuery('#productHead').height();
var productHeadWidth = jQuery('#productHead').width();
var productHeadImageHeight = jQuery('.productHeadImage').height();

var right = productHeadOffset.left;
var bottom = productHeadOffset.top;

var left = (img.width > productHeadWidth) ? (productHeadWidth + productHeadOffset.left) - img.width : 0;
var top = (img.height > productHeadImageHeight) ? (productHeadHeight + productHeadOffset.left) - img.height : 0;

jQuery('.productHeadImage').draggable({ containment: [left, top, right, bottom] });
var productHeadOffset = jQuery('#productHead').offset();
var productHeadHeight = jQuery('#productHead').height();
var productHeadWidth = jQuery('#productHead').width();
var productHeadImageHeight = jQuery('.productHeadImage').height();

var right = productHeadOffset.left;
var bottom = productHeadOffset.top;

var left = (img.width > productHeadWidth) ? (productHeadWidth + productHeadOffset.left) - img.width : 0;
var top = (img.height > productHeadImageHeight) ? (productHeadHeight + productHeadOffset.left) - img.height : 0;

jQuery('.productHeadImage').draggable({ containment: [left, top, right, bottom] });
安穩 2024-10-28 06:13:59

这是对我有用的解决方案,尽管我在 Chrome 中滚动页面时仍然遇到问题:

var cropBoundsOffset = $("cropBounds").offset();
var cropBoundsHeight = $("cropBounds").height();
var cropBoundsWidth = $("cropBounds").width();
var imageHeight = $("cropImage").height();
var imageWidth = $("cropImage").width();

var right = cropBoundsOffset.left;
var bottom = cropBoundsOffset.top;
var left = (imageWidth > cropBoundsWidth) ? (cropBoundsWidth + cropBoundsOffset.left) - imageWidth : 0;
var top = (imageHeight > cropBoundsHeight) ? (cropBoundsHeight + cropBoundsOffset.top) - imageHeight : 0;

var border_left = parseInt($("cropBounds").css("border-left-width"));
var border_top = parseInt($("cropBounds").css("border-top-width"));

$("cropImage").draggable("option", "containment",  [
    left + border_left,
    top + border_top,
    right,
    bottom
]);

This is the solution that worked for me, although I'm still having issues in Chrome when the page is scrolled:

var cropBoundsOffset = $("cropBounds").offset();
var cropBoundsHeight = $("cropBounds").height();
var cropBoundsWidth = $("cropBounds").width();
var imageHeight = $("cropImage").height();
var imageWidth = $("cropImage").width();

var right = cropBoundsOffset.left;
var bottom = cropBoundsOffset.top;
var left = (imageWidth > cropBoundsWidth) ? (cropBoundsWidth + cropBoundsOffset.left) - imageWidth : 0;
var top = (imageHeight > cropBoundsHeight) ? (cropBoundsHeight + cropBoundsOffset.top) - imageHeight : 0;

var border_left = parseInt($("cropBounds").css("border-left-width"));
var border_top = parseInt($("cropBounds").css("border-top-width"));

$("cropImage").draggable("option", "containment",  [
    left + border_left,
    top + border_top,
    right,
    bottom
]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文