以下是转载的博客:https://my.oschina.net/sniperLi/blog/494725

JsPlumb简单样例:

<!DOCTYPE html>
<html>
<head>
    <script src="jquery-1.9.0.js"></script>
    <script src="jquery-ui-1.9.2-min.js"></script>
    <script src="jquery.jsPlumb-1.4.0-all.js"></script>
    <style type="text/css">
        .dragActive { border:2px dotted orange; }
        .dropHover { border:1px dotted red; }
        .item {
            border: 1px solid black;
            background-color: #ddddff;
            width: 100px;
            height: 100px;
            position: absolute;
        }
        #state1 {
            left: 100px;
            top: 100px;
        }
        #state2 {
            left: 250px;
            top: 250px;
        }
        #state3 {
            left: 100px;
            top: 250px;
        }
    </style>
    <script>
        jsPlumb.ready(function() {
            jsPlumb.importDefaults({
                DragOptions : { cursor: 'pointer', zIndex:2000 },
                PaintStyle : { strokeStyle:'#666' },
                EndpointStyle : { width:20, height:16, strokeStyle:'#666' },
                Endpoint : "Rectangle",
                Anchors : ["TopCenter"],
                //控制是否有箭头
                ConnectionOverlays: [
                [ "Arrow", { location: 1 } ],
                [ "Label", {
                    location: 0.1,
                    id: "label",
                    cssClass: "aLabel"
                }]
            ]
            });
            var exampleDropOptions = {
                hoverClass:"dropHover",
                activeClass:"dragActive"
            };
      
      var basicType = {
            connector: "StateMachine",
            paintStyle: { strokeStyle: "red", lineWidth: 4 },
            hoverPaintStyle: { strokeStyle: "blue" },
            overlays: [
                "Arrow"
            ]
        };
        jsPlumb.registerConnectionType("basic", basicType);
      
            var color1 = "#316b31";
            var exampleEndpoint1 = {
              uuid:101,
                endpoint:["Dot", { radius:11 }],//连接点的形状、大小
                paintStyle:{ fillStyle:color1 },//连接点的颜色
                isSource:true,
                scope:"green dot",//点击该颜色的时候,其余该颜色的点都会显示虚线框
                connectorStyle:{ strokeStyle:color1, lineWidth:6 },//点与点之间连线颜色
                connector: ["Bezier", { curviness:63 } ],//线条形状,可弯曲
                maxConnections:1,
                isTarget:true,
                dropOptions : exampleDropOptions
            };
            
            var exampleEndpoint11 = {
              uuid:1011,
                endpoint:["Dot", { radius:11 }],//连接点的形状、大小
                paintStyle:{ fillStyle:color1 },//连接点的颜色
                isSource:true,
                scope:"green dot",//点击该颜色的时候,其余该颜色的点都会显示虚线框
                connectorStyle:{ strokeStyle:color1, lineWidth:6 },//点与点之间连线颜色
                connector: ["Bezier", { curviness:63 } ],//线条形状,可弯曲
                maxConnections:1,
                isTarget:true,
                dropOptions : exampleDropOptions,
                overlays: [
                "Arrow"
            ]
            };
            var color2 = "rgba(229,219,61,0.5)";
            var exampleEndpoint2 = {
              uuid:102,
                endpoint:"Rectangle",
                anchor:"BottomLeft",//连接点的位置,可以被覆盖
                paintStyle:{ fillStyle:color2, opacity:0.5 },
                isSource:true,
                scope:'yellow dot',
                connectorStyle:{ strokeStyle:color2, lineWidth:4 },
                connector : "Straight",//线条形状,直线
                isTarget:true,
                dropOptions : exampleDropOptions,
                beforeDetach:function(conn) {
                    return confirm("Detach connection?");
                },
                onMaxConnections:function(info) {
                    alert("Cannot drop connection " + info.connection.id + " : maxConnections has been reached on Endpoint " + info.endpoint.id);
                }
            };
            
            var exampleEndpoint21 = {
              uuid:1021,
                endpoint:"Rectangle",
                anchor:"BottomLeft",//连接点的位置,可以被覆盖
                paintStyle:{ fillStyle:color2, opacity:0.5 },
                isSource:true,
                scope:'yellow dot',
                connectorStyle:{ strokeStyle:color2, lineWidth:4 },
                connector : "Straight",//线条形状,直线
                isTarget:true,
                dropOptions : exampleDropOptions,
                beforeDetach:function(conn) {
                    return confirm("Detach connection?");
                },
                onMaxConnections:function(info) {
                    alert("Cannot drop connection " + info.connection.id + " : maxConnections has been reached on Endpoint " + info.endpoint.id);
                }
            };
      
      //左上角为起点,0.2表示相对x的偏移量,0.5表示相对y的偏移量
            var anchors = [[0.2, 0.5, 1, 0], [0.8, 1, 0, 1], [0, 0.8, -1, 0], [0.2, 0, 0, -1] ],
                    maxConnectionsCallback = function(info) {
                        alert("Cannot drop connection " + info.connection.id + " : maxConnections has been reached on Endpoint " + info.endpoint.id);
                    };
            var e1 = jsPlumb.addEndpoint("state2", { anchor:"LeftMiddle" }, exampleEndpoint11);
            e1.bind("maxConnections", maxConnectionsCallback);
            jsPlumb.addEndpoint("state1", exampleEndpoint1);
            jsPlumb.addEndpoint("state3", exampleEndpoint2);
            jsPlumb.addEndpoint("state1", {anchor:anchors}, exampleEndpoint21);
            
            //固定连线
            jsPlumb.connect({uuids:[101, 1011]});
            jsPlumb.connect({uuids:[102, 1021]});
            
            //可拖动
            jsPlumb.draggable($('._jsPlumb_endpoint_anchor_'));
        });
    </script>
</head>
<body>
<div id="state1" class="item">state1</div>
<div id="state2" class="item">state2</div>
<div id="state3" class="item">state3</div>
</body>

</html>


用途场景:流程图设计

JsPlumb简单样例可用来实现流程图的绘制

作者:星辰 时间:2017-07-06 浏览 946评论 0 赞 0砸 0 标签: js plugins 前端
评论
还可以再输入500个字

请您注意

·自觉遵守:爱国、守法、自律、真实、文明的原则
·尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法规
·严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的作品
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·您在NoteShare上发表的作品,NoteShare有权在网站内保留、转载、引用或者删除
·参与本评论即表明您已经阅读并接受上述条款