jquery-ui - 可拖动和可扩展的遏制

标签 jquery-ui draggable

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

我的代码:

<!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>

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

谢谢!

最佳答案

简单的解决方案(请参阅 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>

关于jquery-ui - 可拖动和可扩展的遏制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3876967/

相关文章:

javascript - 如果在 JavaScript 函数中声明参数没有被使用,会降低性能吗

JQuery UI 工具栏按钮布局

javascript - jQuery UI Slider - 如何在页面中使用多个 slider

jquery - 拖动和调整 div 与其他 div 重叠

javascript - jQuery 可拖动和透明 PNG - 可拖动区域被透明背景遮挡

javascript - jQuery 可拖动/可内容编辑代码未正确响应

jquery - 限制 jQuery 可拖动项与同级元素重叠/碰撞

xcode - 在 Mac OS X 中使用 swift 我想在运行时移动标签/文本框控件?

javascript - 从另一个回调访问一个回调中定义的变量

javascript - 使用 noConflict jQuery 将 jQuery UI 包含在 requireJS 中