YUI 3使用约束进行拖放

时间:2015-01-28 16:15:55

标签: javascript drag-and-drop draggable yui3

我是YUI的新手。我想用contstraints实现拖放操作。我遵循简单的YUI 3指南,并能够根据他们给出的代码实现拖放。 http://yuilibrary.com/yui/docs/dd/scroll-list.html

HTML

    <body>
  <div id="demo">
   <ul id="list1">
    <li class="list1">Item #1</li>
    <li class="list1">Item #2</li>
    <li class="list1">Item #3</li>
    <li class="list1">Item #4</li>
    <li class="list1">Item #5</li>
    <li class="list1">Item #6</li>
    <li class="list1">Item #7</li>
    <li class="list1">Item #8</li>
    <li class="list1">Item #9</li>
    <li class="list1">Item #10</li>
    <li class="list1">Item #11</li>
    <li class="list1">Item #12</li>  
</ul>
<ul id="list2">
    <li class="list2">Item #1</li>
    <li class="list2">Item #2</li>
    <li class="list2">Item #3</li>
    <li class="list2">Item #4</li>
    <li class="list2">Item #5</li>
    <li class="list2">Item #6</li>
    <li class="list2">Item #7</li>
    <li class="list2">Item #8</li>
    <li class="list2">Item #9</li>
    <li class="list2">Item #10</li>
    <li class="list2">Item #11</li>
    <li class="list2">Item #12</li>  
</ul>
</div>

CSS

yui3-dd-proxy {
        text-align: left;
    }
#demo {
    width: 600px;
}
#demo {
        border: 1px solid black;
        padding: 10px;
        margin: 10px;
        zoom: 1;
    }
#demo ul li {
    border: 1px solid black;
    background-color: #8DD5E7;
    cursor: move;
    margin: 3px;
    list-style-type: none;
}
#demo:after { display: block; clear: both; visibility: hidden; content: '.'; height: 0;}
#demo ul {
        border: 1px solid black;
        margin: 10px;
        width: 200px;
        height: 400px;
        float: left;
        padding: 0;
        zoom: 1;
        position: relative;
        overflow: auto;
    } 

 #demo ul li.list1 {
        background-color: #8DD5E7;
        border:1px solid #004C6D;
    }   
 #demo ul li.list2 {
        background-color: #EDFF9F;
        border:1px solid #CDCDCD;
    }  

的Javascript

YUI().use('dd-constrain', 'dd-proxy', 'dd-drop', 'dd-scroll', function(Y) {
    //Listen for all drop:over events
    //Y.DD.DDM._debugShim = true;   


    Y.DD.DDM.on('drop:over', function(e) {
        //Get a reference to our drag and drop nodes
        var drag = e.drag.get('node'),
            drop = e.drop.get('node');

        //Are we dropping on a li node?
        if (drop.get('tagName').toLowerCase() === 'li') {
            //Are we not going up?
            if (!goingUp) {
                drop = drop.get('nextSibling');
            }
            //Add the node to this list
            e.drop.get('node').get('parentNode').insertBefore(drag, drop);
            //Set the new parentScroll on the nodescroll plugin
            e.drag.nodescroll.set('parentScroll', e.drop.get('node').get('parentNode'));
            //Resize this nodes shim, so we can drop on it later.
            e.drop.sizeShim();
        }
    });
    //Listen for all drag:drag events
    Y.DD.DDM.on('drag:drag', function(e) {
        //Get the last y point
        var y = e.target.lastXY[1];
        //is it greater than the lastY var?
        if (y < lastY) {
            //We are going up
            goingUp = true;
        } else {
            //We are going down.
            goingUp = false;
        }
        //Cache for next check
        lastY = y;
        Y.DD.DDM.syncActiveShims(true);
    });
    //Listen for all drag:start events
    Y.DD.DDM.on('drag:start', function(e) {
        //Get our drag object
        var drag = e.target;
        //Set some styles here
        drag.get('node').setStyle('opacity', '.25');
        drag.get('dragNode').set('innerHTML', drag.get('node').get('innerHTML'));
        drag.get('dragNode').setStyles({
            opacity: '.5',
            borderColor: drag.get('node').getStyle('borderColor'),
            backgroundColor: drag.get('node').getStyle('backgroundColor')
        });
    });
    //Listen for a drag:end events
    Y.DD.DDM.on('drag:end', function(e) {
        var drag = e.target;
        //Put our styles back
        drag.get('node').setStyles({
            visibility: '',
            opacity: '1'
        });
    });
    //Listen for all drag:drophit events
    Y.DD.DDM.on('drag:drophit', function(e) {
        var drop = e.drop.get('node'),
            drag = e.drag.get('node');

        //if we are not on an li, we must have been dropped on a ul
        if (drop.get('tagName').toLowerCase() !== 'li') {
            if (!drop.contains(drag)) {
                drop.appendChild(drag);
                //Set the new parentScroll on the nodescroll plugin
                e.drag.nodescroll.set('parentScroll', e.drop.get('node'));
            }
        }
    });

    //Static Vars
    var goingUp = false, lastY = 0;

    //Get the list of li's in the lists and make them draggable
    var lis = Y.all('#demo ul li');
    lis.each(function(v, k) {


        var dd = new Y.DD.Drag({
            node: v,
            target: {
                padding: '0 0 0 20'
            }
        }).plug(Y.Plugin.DDProxy, {
            moveOnEnd: false
        }).plug(Y.Plugin.DDConstrained, {
            constrain2node: '#demo'
        }).plug(Y.Plugin.DDNodeScroll, {
            node: v.get('parentNode')
        });
    });
 Y.one('#make').on('click', function(e) {

    YUI().use('dd-drag', 'dd-proxy', function(Y) {
    //Selector of the node to make draggable
    var dd = new Y.DD.Drag({
        container: '#demo',
        node: 'li'
    }).plug(Y.Plugin.DDProxy); //This config option makes the node a Proxy Drag

    var demo =Y.one('#demo ul');
        for (var i = 1; i < 11; i++) {
            //demo.append('<li class="list3">New item #' + i + '</i><br>');
            demo.append('<li class="list3">Item #' + i + '</li>' )
        }
    })
    });




    //Create simple targets for the 2 lists.
    var uls = Y.all('#demo ul');
    uls.each(function(v, k) {
        var tar = new Y.DD.Drop({
            node: v
        });
    });
});

YUI().use('dd-delegate', 'dd-drop-plugin', 'dd-constrain', 'dd-proxy', function(Y) {
    var del = new Y.DD.Delegate({
        container: '#demo',
        nodes: 'li'
    });

    del.on('drag:start', function(e) {
        e.target.get('node').setStyle('opacity', '.5');
    });
    del.on('drag:end', function(e) {
        e.target.get('node').setStyle('opacity', '1');
    });

    del.dd.plug(Y.Plugin.DDConstrained, {
        constrain2node: '#play'
    });

    del.dd.plug(Y.Plugin.DDProxy, {
        moveOnEnd: false,
        cloneNode: true
    });

    var drop = Y.one('#drop').plug(Y.Plugin.Drop);
    drop.drop.on('drop:hit', function(e) {
        drop.set('innerHTML', 'You dropped: <strong>' + e.drag.get('node').get('innerHTML') + '</strong>');
    });


});

enter image description here

当我将蓝色拖到黄色时,我想对左旋拖曳意义。我想在蓝色项目掉落在黄色区域后重新填充。 YUI指南有类似于我想要实现的内容: http://yuilibrary.com/yui/docs/dd/delegate-plugins.html

我不清楚如何将这两个操作一起实现以达到我需要的效果。 任何帮助都会很好。 感谢

的Javascript

YUI().use('dd-delegate', 'dd-drop-plugin', 'dd-constrain', 'dd-proxy', function(Y) {
    var del = new Y.DD.Delegate({
        container: '#demo',
        nodes: 'li'
    });

    del.on('drag:start', function(e) {
        e.target.get('node').setStyle('opacity', '.5');
    });
    del.on('drag:end', function(e) {
        e.target.get('node').setStyle('opacity', '1');
    });

    del.dd.plug(Y.Plugin.DDConstrained, {
        constrain2node: '#play'
    });

    del.dd.plug(Y.Plugin.DDProxy, {
        moveOnEnd: false,
        cloneNode: true
    });

    var drop = Y.one('#drop').plug(Y.Plugin.Drop);
    drop.drop.on('drop:hit', function(e) {
        drop.set('innerHTML', 'You dropped: <strong>' + e.drag.get('node').get('innerHTML') + '</strong>');
    });


});

0 个答案:

没有答案