【问题标题】:Draggable and extensible containment可拖动和可扩展的容器
【发布时间】:2010-10-06 21:16:41
【问题描述】:

我正在尝试授权仅在其容器的右侧或底部拖出一个对象,并且在不停止拖动元素的情况下无法正确执行此操作。

我的代码:

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

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

谢谢!

【问题讨论】:

  • 如果您对我的解决方案感到满意,您可以奖励赏金吗?如果没有其他人投票支持我的答案,那么 50 分就会蒸发殆尽。 :(

标签: jquery-ui draggable


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    相关资源
    最近更新 更多