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

jquery的可拖动网格

  •  5
  • Art  · 技术社区  · 14 年前

    似乎的构造函数中的“grid”选项 Draggable 相对地绑定到被拖动元素的原始坐标-所以简单地说,如果有三个可拖动的分隔带 顶部 分别设置为相对于其父级100、200、254像素:

    <div class="parent-div" style="position: relative;">
        <div id="div1" class="draggable" style="top: 100px; position: absolute;"></div>
        <div id="div2" class="draggable" style="top: 200px; position: absolute;"></div>
        <div id="div3" class="draggable" style="top: 254px; position: absolute;"></div>
    </div>
    

    ADN所有这些都可以在“grid”设置为[1,100]的情况下拖动:

    draggables = $('.draggable');
    $.each(draggables, function(index, elem) {
        $(elem).draggable({
                           containment: $('#parent-div'),
                           opacity: 0.7,
                           revert: 'invalid',
                           revertDuration: 300,
                           grid: [1, 100],
                           refreshPositions: true
        });
    });
    

    问题是你一拖 DIV3 比如说,向下,它的顶部增加了100,移动到 354PX 而不是仅仅增加 46PX (254+46=300),将到达电网的下一站- 300 PX ,如果我们看到 亲本div 作为参考点和“网格支架”。

    我看了一下可拖动的源,它似乎是一个内置的缺陷——它们只做了与可拖动元素的原始位置相关的所有计算。

    我想避免猴子修补可拖动库的代码,我在这里真正寻找的是如何使可拖动计算相对于包含父对象的网格位置。但是,如果猴子修补是不可避免的,我想我将不得不忍受它。

    4 回复  |  直到 12 年前
        1
  •  2
  •   Pez Cuckow    13 年前

    我通过简单地添加我自己的脚本来解决这个问题:在draggable下,设置它来划分math.floor(x/100)*100,Y也是这样。

    如果你不介意在源代码中搜索,请看 http://labs.pezcuckow.com/solveit/game.html 以及make draggable功能!(游戏非本地接近完成)

    您还将发现Revert有问题,它不会将它们重新对齐到网格。所以我在最后写了自己的“检查是否无效”函数:dragabble(同样可以在上面的游戏中看到)。

        2
  •  11
  •   ThiefMaster    13 年前

    因为@pez的答案丢失了(404),我是这样做的:

    $('#elem').draggable({
        ....,
        drag: function(e, ui) {
            ui.position.left = Math.floor(ui.position.left / 10) * 10;
            ui.position.top = Math.floor(ui.position.top / 10) * 10;
        }
    });
    

    演示: http://jsfiddle.net/ThiefMaster/yGsSE/

        3
  •  0
  •   CResults    14 年前

    在过去的几天里,我已经开始使用jquery draggable,并找到了一个简单的解决方法。

    将position属性设置为absolute,并将顶部和左侧的控件设置为与网格对齐的值。

    由于顶部/左侧的值是绝对值,因此在关闭并将其存储在数据库中时,它们也更加合理。

        4
  •  0
  •   misima    13 年前

    桌面查询:

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js"></script>
    <link rel="stylesheet/less" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/themes/base/jquery-ui.css">
    
    <div class="parent-div" style="position: relative;">
        <div id="div1" class="draggable" style="top: 100px; position: absolute;">1</div>
        <div id="div2" class="draggable" style="top: 200px; position: absolute;">2</div>
        <div id="div3" class="draggable" style="top: 254px; position: absolute;">3</div>
    </div>
    
    <script>
    var Z_INDEX_APP = 0;
    var Z_INDEX_WINDOW_COUNTER = 1;
    var Z_INDEX_NOTE_COUNTER = 999;
    var Z_INDEX_FOR_DRAGGED = 99999;
    var Z_INDEX_FOR_NOTIF = 999999; 
    var ICONS_GRID_CELL_SIZE = 75;
    var ICONS_GRID_PADDING_LEFT = 20;
    var ICONS_GRID_PADDING_TOP = 50; 
    draggables = $('.draggable');
    $.each(draggables, function(index, elem) {
        $(elem).draggable({
            containment: $('#parent-div'),
            start: function(event) {
            icon = $(this);
            icon.css('z-Index', Z_INDEX_FOR_DRAGGED);
            dragStartLeft = icon.css('left');
            dragStartTop = icon.css('top');
            icon.addClass('desktop-app-dragging');
            icon.removeClass('desktop-app-pressed');
        },
        stop: function(event) {         
            icon.css('z-Index', Z_INDEX_APP);
            var appId = icon.attr('id').split('_')[1];
            icon.removeClass('desktop-app-dragging');
            var alignedX = ICONS_GRID_PADDING_LEFT + Math.floor(parseInt(icon.css('left'))/ICONS_GRID_CELL_SIZE) * ICONS_GRID_CELL_SIZE;
            var alignedY = ICONS_GRID_PADDING_TOP + Math.floor(parseInt(icon.css('top'))/ICONS_GRID_CELL_SIZE)  * ICONS_GRID_CELL_SIZE;
            if ( alignedX < ICONS_GRID_PADDING_LEFT ) alignedX = ICONS_GRID_PADDING_LEFT;
            if ( alignedY < ICONS_GRID_PADDING_TOP ) alignedY = ICONS_GRID_PADDING_TOP;         
            var iconToSwitch = null;
            $(".desktop-app").each(function(index, app) {           
                if ( app.style.top == ( alignedY + 'px' ) && app.style.left == ( alignedX + 'px' ) ) {
                    iconToSwitch = app;
                }
            });
            if ( iconToSwitch != null ) {
                var appToSwitchId = iconToSwitch.id.split('_')[1];
                var updateUrl = 'api/desktop?cmd=update&id=' + appToSwitchId + '&x=' + parseInt(dragStartLeft) + '&y=' + parseInt(dragStartTop);
                //$.getJSON(updateUrl);
                iconToSwitch.style.left = dragStartLeft;
                iconToSwitch.style.top = dragStartTop;
            }       
            icon.css('left', alignedX + 'px');
            icon.css('top', alignedY + 'px');
            var updateUrl = 'api/desktop?cmd=update&id=' + appId + '&x=' + alignedX + '&y=' + alignedY;
            //$.getJSON(updateUrl);
        }
    
        });
    });
    </script>