可拖动和可扩展的遏制

发布于 2024-09-26 02:01:26 字数 1283 浏览 2 评论 0原文

我试图授权仅在其遏制的右侧或底部拖出一个对象,并且如果不停止拖动该元素就无法正确执行此操作。

我的代码:

<!DOCTYPE html>
<html>
<head>
  <style>
  #parent { height: 500px; width: 500px; border: 1px black solid; }
  #images { height: 100px; width: 100px; background-color:orange; }
  </style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js"></script>
</head>
<body>
<div id="parent">
    <div id="images"></div>
</div>
<script>
$(document).ready(function() {
    $("#images").draggable({
        containment: $("#parent"),
        drag: function(event, ui) {
            var helper = ui.helper, pos = ui.position;
            if(pos.left + parseInt(helper.outerWidth(), 10) == parseInt($("#parent").css("width"), 10)) {
                $("#parent").animate({width: "+=100"});
            }
            if(pos.top + parseInt(helper.outerHeight(), 10) == parseInt($("#parent").css("height"), 10)) {
                $("#parent").animate({height: "+=100"});
            }
        }
    });
});
</script>
</body>
</html>

您知道如何刷新容器大小以便继续在右侧或底部拖动吗?

谢谢 !

i'm trying to the authorize to drag out an object only on the right or bottom side of his containment and cannot do it properly whitout stop dragging the element.

My code:

<!DOCTYPE html>
<html>
<head>
  <style>
  #parent { height: 500px; width: 500px; border: 1px black solid; }
  #images { height: 100px; width: 100px; background-color:orange; }
  </style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js"></script>
</head>
<body>
<div id="parent">
    <div id="images"></div>
</div>
<script>
$(document).ready(function() {
    $("#images").draggable({
        containment: $("#parent"),
        drag: function(event, ui) {
            var helper = ui.helper, pos = ui.position;
            if(pos.left + parseInt(helper.outerWidth(), 10) == parseInt($("#parent").css("width"), 10)) {
                $("#parent").animate({width: "+=100"});
            }
            if(pos.top + parseInt(helper.outerHeight(), 10) == parseInt($("#parent").css("height"), 10)) {
                $("#parent").animate({height: "+=100"});
            }
        }
    });
});
</script>
</body>
</html>

Have you an idea how about refreshing the containment size in order to continue dragging on the right or bottom side ?

Thanks !

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

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

发布评论

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

评论(1

秋心╮凉 2024-10-03 02:01:26

简单的解决方案(参见DEMO):

<!DOCTYPE html>
<html>
<head>
  <style>
  #parent { height: 300px; width: 300px; border: 1px black solid; position: relative; left: 0; }
  #images { height: 100px; width: 100px; background-color:orange; }
  </style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js"></script>
</head>
<body>
<div id="parent">
    <div class="container">
        <div id="images"></div>
    </div>
</div>
<script>
$(document).ready(function() {
    var
        $document = $(document),
        $parent = $("#parent"),
        $container = $(".container", $parent),
        offset = $container.offset(),
        scrollbarSafety = 15; // use this to avoid dragging outside of window, thus creating scrollbars and wreaking all kinds of havoc on draggables containment checks

    $container
        .width($document.width() - offset.left - scrollbarSafety)
        .height($document.height() - offset.top - scrollbarSafety);

    $("#images")
            .draggable(
            {
                containment: $container,
                drag:
                    function(event, ui)
                    {
                        var
                            $draggee = $(this),
                            draggeePosition = $draggee.position(),
                            shouldWidth = draggeePosition.left + $draggee.width(),
                            shouldHeight = draggeePosition.top + $draggee.height();
                        if($parent.width() < shouldWidth)
                        {
                            $parent.width(shouldWidth);
                        }
                        if($parent.height() < shouldHeight)
                        {
                            $parent.height(shouldHeight);
                        }
            }
            }
        );
});
</script>
</body>
</html>

Simple solution (see DEMO):

<!DOCTYPE html>
<html>
<head>
  <style>
  #parent { height: 300px; width: 300px; border: 1px black solid; position: relative; left: 0; }
  #images { height: 100px; width: 100px; background-color:orange; }
  </style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.js"></script>
</head>
<body>
<div id="parent">
    <div class="container">
        <div id="images"></div>
    </div>
</div>
<script>
$(document).ready(function() {
    var
        $document = $(document),
        $parent = $("#parent"),
        $container = $(".container", $parent),
        offset = $container.offset(),
        scrollbarSafety = 15; // use this to avoid dragging outside of window, thus creating scrollbars and wreaking all kinds of havoc on draggables containment checks

    $container
        .width($document.width() - offset.left - scrollbarSafety)
        .height($document.height() - offset.top - scrollbarSafety);

    $("#images")
            .draggable(
            {
                containment: $container,
                drag:
                    function(event, ui)
                    {
                        var
                            $draggee = $(this),
                            draggeePosition = $draggee.position(),
                            shouldWidth = draggeePosition.left + $draggee.width(),
                            shouldHeight = draggeePosition.top + $draggee.height();
                        if($parent.width() < shouldWidth)
                        {
                            $parent.width(shouldWidth);
                        }
                        if($parent.height() < shouldHeight)
                        {
                            $parent.height(shouldHeight);
                        }
            }
            }
        );
});
</script>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文