代码之家  ›  专栏  ›  技术社区  ›  Paul

设置可拖动对象的边界限制

  •  7
  • Paul  · 技术社区  · 16 年前

    1. overflow:hidden

    <style type="text/css">
        .myList {list-style:none; margin:0px; padding:1px;}
        .myList li{ height:50px;  margin:4px; padding:2px;}
        .dragPanel {height:230px; border:solid; overflow:hidden;}
    </style>
    
    <div class="dragPanel">
        <ul class="myList">
            <li>1</li>
            <li>2</li>
            ...   ....
            <li>8</li>
            <li>9</li>
        </ul>
    </div>
    

    我希望能够在父div中上下拖动列表。我正在使用jquery ui draggable插件实现垂直拖动,但我不确定如何在父div中约束列表。

    <script type="text/javascript" src="http://jqueryui.com/latest/jquery-1.3.2.js"></script>
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.core.js"></script>
    <script type="text/javascript" src="http://jqueryui.com/latest/ui/ui.draggable.js"></script>
    
    <script type="text/javascript">
        $(document).ready(function() {
            $('.dragPanel').addClass("hover");
            $('.myList').draggable({ axis: 'y' });
        });
    </script>
    

    我该如何设置列表位置的垂直上下限?

    4 回复  |  直到 13 年前
        1
  •  10
  •   moff    16 年前

    使用 containment option :

    $('.myList').draggable({
        axis: 'y',
        containment: 'parent'
    });
    
        2
  •  8
  •   Davide Pastore    10 年前

    我也有同样的问题,答案对我没有帮助。我认为下面是一个更好的解决方案。

    <!DOCTYPE html>
    <html>
    <head>
        <title>Draggable limited to the container</title>
        <style type="text/css">
        #container {
            cursor: move;
            overflow: hidden;
            width: 300px;
            height: 300px;
            border: 1px solid black;
        }
        </style>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
        <script type="text/javascript">
        $(function() {
            $.globalVars = {
                originalTop: 0,
                originalLeft: 0,
                maxHeight: $("#draggable").height() - $("#container").height(),
                maxWidth: $("#draggable").width() - $("#container").width()
            };
            $("#draggable").draggable({
                start: function(event, ui) {
                    if (ui.position != undefined) {
                        $.globalVars.originalTop = ui.position.top;
                        $.globalVars.originalLeft = ui.position.left;
                    }
                },
                drag: function(event, ui) {
                    var newTop = ui.position.top;
                    var newLeft = ui.position.left;
                    if (ui.position.top < 0 && ui.position.top * -1 > $.globalVars.maxHeight) {
                        newTop = $.globalVars.maxHeight * -1;
                    }
                    if (ui.position.top > 0) {
                        newTop = 0;
                    }
                    if (ui.position.left < 0 && ui.position.left * -1 > $.globalVars.maxWidth) {
                        newLeft = $.globalVars.maxWidth * -1;
                    }
                    if (ui.position.left > 0) {
                        newLeft = 0;
                    }
                    ui.position.top = newTop;
                    ui.position.left = newLeft;
                }
            });
        });
        </script>
    </head>
    <body>
        <div id="container">
            <img id="draggable" src="avatar.jpg" />
        </div>
    </body>
    </html>
    
        3
  •  4
  •   Akhil Jain Thedaego    13 年前

    好啊

    这是我走过的路线。。。

    加载页面时,我在可拖动列表周围添加一个container div。我将其高度设置为列表的两倍,然后将其在页面上稍微向上定位:

    <script type="text/javascript">
      $(document).ready(function() {
          CreateContainerDiv();
          $('.dragPanel').addClass("hover");
          $('.myList').draggable({ axis: 'y', containment: 'parent' });
      });
    
        function CreateContainerDiv() {
            var dragPanel = $('.dragPanel');
            var dragTarget = $('.myList');
    
            // add the container panel
            var newPanelHeight = dragTarget.outerHeight() * 2 - dragPanel.outerHeight();
            dragTarget.wrap(document.createElement("div"));
    
            // set its height and put it in the right place
            dragTarget.parent().css("height", newPanelHeight);
            dragTarget.parent().css("position", "relative");
            var newPanelPosition = ((dragTarget.outerHeight() * -1) / 2);
            dragTarget.parent().css("top", newPanelPosition);         
        }
    </script>
    

    然后,列表将包含在此新元素中。

    好的,我想我已经解决了安置问题。我已经把它变成了一个插件,这样其他人就不用花时间创建这个功能了。

    jQuery Drag-gable List at Github

        4
  •  0
  •   Criss    8 年前

    如果要隐藏滚动条,只需放置以下属性:.dragPanel::-webkit滚动条{宽度:0!重要}就这样!我真的不知道你为什么想拖动元素而不是仅仅滚动内容(这是解决此类“问题”的正确方法)。无论如何,我希望它能帮助任何需要满足同样需求的人。问候语!

    推荐文章