简体   繁体   中英

JsPlumb - Endpoints do not refresh position when used on draggable element

I started using jsPlumb and JQuery, I want to connect draggable elements but if I add the draggable behavior before the connection then the connection does not refresh position.

My code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>

        <style type="text/css">
            .window {
                background-color: white;
                border: 3px solid #346789;
                color: black;
                font-family: helvetica;
                font-size: 0.8em;
                height: 12em;
                opacity: 0.8;
                padding: 0.5em;
                position: absolute;
                width: 14em;
                z-index: 20;
            }
        </style>

        <script type="text/javascript" src="jquery.min.js"></script>
        <script type="text/javascript" src="jquery-ui.min.js"></script>
        <script type="text/javascript" src="jquery.jsPlumb-1.3.2-all-min.js"></script>
    </head>
    <body>
    <div>
        <div id="a" class="a window" style="width: 100px;height: 100px;border: solid 1px"></div>
        <div id="b" class="b window" style="width: 100px;height: 100px;border: solid 1px;"></div>
    </div>
    <script type="text/javascript">

        $(document).ready(function() {

            $(".window").draggable();

            var a = $("#a");
            var b = $("#b");
            jsPlumb.connect({
                source:a,
                target:b,
                connector:["Bezier",68],
                endpoints:[
                    ["Dot",{radius:12}],
                    ["Rectangle",{width:20,height:30}]
                ]
            });
        });
    </script>
    </body>
    </html>

i wrote jsPlumb.

the reason it doesn't refresh is that it has no way of knowing something is being dragged. instead of calling $(".window").draggable(), you either need to let jsPlumb do that for you when a connection is made, or through this method:

jsPlumb.draggable($(".window"));

the first option will not initialise the dragging for any window that doesn't have a connection. the second one will.

There are few ways to do so - refer to the jsPlumb documentation

,but generally you can use:

  1. Id of the element
  2. Array of elements
  3. Or selector (for example class selector that was mentioned previously: jsPlumb.draggable($(".window"));

Here is a working example:

<!DOCTYPE html>
<html>
<head>
    <title>JS plumb test</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
        <script type="text/javascript" src="/include/jquery.jsPlumb-1.3.16-all-min.js"></script>

    <style>
        .window { 
            background-color: #EEEEEF;
            border: 1px solid #346789;
            border-radius: 0.5em;
            box-shadow: 2px 2px 19px #AAAAAA;
            color: black;
            height: 5em;
            position: absolute;
            width: 5em;
            cursor: pointer;
        }
    </style>

    <script>

        jsPlumb.ready(function () {

            // three ways to do this - an id, a list of ids, or a selector (note the two different types of selectors shown here...anything that is valid jquery will work of course)

            //jsPlumb.draggable("container0");
            //jsPlumb.draggable(["container0", "container1"]);
            jsPlumb.draggable($(".window"));

            //perform operation only after DOM is loaded
            var e0 = jsPlumb.addEndpoint("container0"),
                e1 = jsPlumb.addEndpoint("container1");



            jsPlumb.connect({ source: e0, target: e1 });


        });


    </script>

</head>
 <body >
     <div class="window" style="left: 20px" id="container0">
     </div>

    <div class="window"  style="left: 200px" id="container1">
    </div>
</body>
</html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM