jsPlumb Demo源码分析

拖拉到中间的div层可以拖动,拖动不能超过中间div的边框:

jsPlumb.draggable(id);
$("#" + id).draggable({ containment: "parent" });
 jsPlumb.draggable(jsPlumb.getSelector(".window"), { containment:".demo"});   

使用jquery里的draggable为其标记。并指定可以拖拽的范围(局限于我们的content容器)。如果想限制元素拖拽的范围,只需要设置它的containment属性。

   $(".jq-draggable-incontainer").draggable({
                containment: $( "#content" ).length ? "#content" : "document"
                });


html 代码:

 

<!doctype html>
<html>
    <head>
        
        <title>jsPlumb 1.5.2 demo - jQuery</title>
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />  
  <!-- <link rel="stylesheet" href="../demo-all.css"> -->
  <link rel="stylesheet" href="demo.css">
    </head>
    <body>
   <div id="main">
   <div id="render"></div> 
            <div class="component window" id="window1"><strong>Window 1</strong></div>
            <div class="component window" id="window2"><strong>Window 2</strong></div>
            <div class="component window" id="window3"><strong>Window 3</strong></div>
            <div class="component window" id="window4"><strong>Window 4</strong></div>
            <div class="component window" id="window5"><strong>Window 5</strong></div>
            <div class="component window" id="window6"><strong>Window 6</strong></div>
            <div class="component window" id="window7"><strong>Window 7</strong></div>            
        </div>
 </body>
 <script type='text/javascript' src='C:/Users/qiaoqiao.li/Desktop/msm/jquery-1.9.0-min.js'></script>
 <script type='text/javascript' src='C:/Users/qiaoqiao.li/Desktop/msm/jquery-ui-1.9.2-min.js'></script>
 <script type='text/javascript' src='C:/Users/qiaoqiao.li/Desktop/msm/jquery.ui.touch-punch.min.js'></script>
 <script type='text/javascript' src='C:/Users/qiaoqiao.li/Desktop/msm/jquery.jsPlumb-1.5.2.js'></script>
  
 <!--  demo code -->
 <script src="demo.js"></script>
</html>

/

css样式:

 

#window1 {  top:5em;left:4em;}
#window2 { top:17em; left:49em;}
#window3 { top:31em;left:4em; }
#window4 { top:9em; left:28em;}
#window5 {top:45em;left:33em;}
#window6 {top:30em;left:64em;}
#window7 {top:52em;left:6em;}
#window8 {left:66em;top:32em;}
body {
    padding:0;
    margin:0;
    background-color:white;    
    font-family:'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;    
    background-color:#eaedef;
   
}
@font-face {
  font-family: 'Open Sans';
  font-style: normal;
  font-weight: 400;
  src:local('Open Sans'), 
   local('OpenSans'),
   url("OpenSans-Regular.ttf") format('truetype'),
   url("OpenSans.woff") format('woff');   
}

 #main {  
  margin-top:44px;
  position: relative;
  font-size: 80%;    
}

#render {   
 background-color:#fff;     
}
.window{ 
    
    text-align:center;
    z-index:24;
 cursor:pointer;
 box-shadow: 2px 2px 19px #aaa;
   -o-box-shadow: 2px 2px 19px #aaa;
   -webkit-box-shadow: 2px 2px 19px #aaa;
   -moz-box-shadow: 2px 2px 19px #aaa;
    
}
.component { 
  border:1px solid #346789; 
  border-radius:0.5em;        
  opacity:0.8; 
  filter:alpha(opacity=80);
  background-color:white;
  color:black;
  padding:0.5em;   
  font-size:0.8em;
}

.component:hover {
    border:1px solid #123456;
    box-shadow: 2px 2px 19px #444;
   -o-box-shadow: 2px 2px 19px #444;
   -webkit-box-shadow: 2px 2px 19px #444;
   -moz-box-shadow: 2px 2px 19px #fff;
    opacity:0.9;
    filter:alpha(opacity=90);
}

.window {
    background-color:white;
    border:1px solid #346789;
    box-shadow: 2px 2px 19px #e0e0e0;
    -o-box-shadow: 2px 2px 19px #e0e0e0;
    -webkit-box-shadow: 2px 2px 19px #e0e0e0;
    -moz-box-shadow: 2px 2px 19px #e0e0e0;
    -moz-border-radius:0.5em;
    border-radius:0.5em;        
    width:5em; height:5em;        
    position:absolute;    
    color:black;
    padding:0.5em;
    width:8em; 
    height:8em;
    line-height: 8em; 
    font-size:0.8em;
    -webkit-transition: -webkit-box-shadow 0.15s ease-in;
    -moz-transition: -moz-box-shadow 0.15s ease-in;
    -o-transition: -o-box-shadow 0.15s ease-in;
    transition: box-shadow 0.15s ease-in;
}

.window:hover {
    border:1px solid #123456;
    box-shadow: 2px 2px 19px #444;
   -o-box-shadow: 2px 2px 19px #444;
   -webkit-box-shadow: 2px 2px 19px #444;
   -moz-box-shadow: 2px 2px 19px #fff;
    opacity:0.9;
    filter:alpha(opacity=90);
}

.window a {
    font-family:helvetica;
}
._jsPlumb_drag_select * {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: -moz-none;
    -ms-user-select: none;
    user-select: none
}
/** Z-INDEX **/

._jsPlumb_connector { z-index:18; }
._jsPlumb_endpoint { z-index:19; }
._jsPlumb_overlay { z-index:20; }
._jsPlumb_connector._jsPlumb_hover {
    z-index:21 !important;
}
._jsPlumb_endpoint._jsPlumb_hover {
    z-index:22 !important;
}
._jsPlumb_overlay._jsPlumb_hover {
    z-index:23 !important;
}

js代码:

 

jsPlumb.bind("ready", function() {

 window.jsPlumbDemo = {
   
  init : function() {   
   
   jsPlumb.importDefaults({
    DragOptions : { cursor: "pointer", zIndex:2000 },
    HoverClass:"connector-hover"
   });
 
   var connectorStrokeColor = "rgba(50, 50, 200, 1)",
    connectorHighlightStrokeColor = "rgba(180, 180, 200, 1)",
    hoverPaintStyle = { strokeStyle:"#7ec3d9" };   // hover paint style is merged on normal style, so you 
                                                           // don't necessarily need to specify a lineWidth   

   // 
   // connect window1 to window2 with a 13 px wide olive colored Bezier, from the BottomCenter of 
   // window1 to 3/4 of the way along the top edge of window2.  give the connection a 1px black outline,
   // and allow the endpoint styles to derive their color and outline from the connection.
   // label it "Connection One" with a label at 0.7 of the length of the connection, and put an arrow that has a 50px
   // wide tail at a point 0.2 of the length of the connection.  we use 'cssClass' and 'endpointClass' to assign
   // our own css classes, and the Label overlay has three css classes specified for it too.  we also give this
   // connection a 'hoverPaintStyle', which defines the appearance when the mouse is hovering over it. 
   //
   var connection1 = jsPlumb.connect({
    //开始点
    source:"window1", 
    //目的点
       target:"window2", 
    //连接器采用Bezier曲线,还有直线和流程图连线
    connector:["Bezier", { curviness:70 }],
    //样式
       cssClass:"c1",
    //端点类型,点对点(Dot Endpoint ),矩形端点(Rectangle Endpoint).图片端点(Image Endpoint)
    //blank端点类型为空
       endpoint:"Blank",
       endpointClass:"c1Endpoint", 
    //anchors锚(动态锚,静态锚),[x,y,dx,dy];x,y的区间为[0,1]意思为锚的位置;dx,dy的区间为[-1,1]意思为曲线的方向
       anchors:["BottomCenter", [ 0.75, 0, 0, -1 ]], 
    //连接线的样式
       paintStyle:{ 
     //连接线的宽度,int值
     lineWidth:6,
     //连接器的颜色
     strokeStyle:"#a7b04b",
     //连接器或端点的轮廓宽度
     outlineWidth:1,
     //连接器或端点的颜色
     outlineColor:"#666"
    },
    //fillStyle:定义Endpoint的颜色
    endpointStyle:{ fillStyle:"#a7b04b" },
    //连接线悬浮样式
       hoverPaintStyle:hoverPaintStyle,  
    //覆盖物类型,四个值Arrow可配置的箭头(可折回),Label点的连接器上可配置的标签,PlainArrow一个三角形的箭头,不可折回,Diamond钻石
       overlays : [
     //连接器上配置的label 
     ["Label", {                     
      cssClass:"l1 component label",
      //显示的label
      label : "Connection One", 
      //连接器的位置
      location:0.7,
      id:"label",
      //点击事件
      events:{
       "click":function(label, evt) {
        alert("clicked on label for connection " + label.component.id);
       }
      }
     }],
     //连接器上配置的箭头
     ["Arrow", {
      cssClass:"l1arrow",
      location:0.5, width:20,length:20,
      //点击事件
      events:{
       "click":function(arrow, evt) {
        alert("clicked on arrow for connection " + arrow.component.id);
       }
      }
     }]
    ]
   });            
    
        
         var w23Stroke = "rgb(189,11,11)"; 
         var connection3 = jsPlumb.connect({
    source:"window2", 
    target:"window3", 
    //连接线的样式
    paintStyle:{ 
    //连接线的宽度,int值
     lineWidth:8,
     //连接器的颜色
     strokeStyle:w23Stroke,
     outlineColor:"#666",
     outlineWidth:1 
    },
    detachable:false,
    //连接线悬浮样式
    hoverPaintStyle:hoverPaintStyle, 
    //锚
    anchors:[ [ 0.3 , 1, 0, 1 ], "TopCenter" ], 
    //端点类型
    endpoint:"Rectangle", 
    endpointStyles:[
     //gradient渐变,Linear线条渐变和radial半径渐变两种
     { gradient : { stops:[[0, w23Stroke], [1, "#558822"]] }},
     { gradient : {stops:[[0, w23Stroke], [1, "#882255"]] }}
    ] 
   });     
    
   var connection2 = jsPlumb.connect({
    source:'window3', target:'window4', 
    //连接线的样式
    paintStyle:{ 
       lineWidth:10,
       //连接器颜色
       strokeStyle:connectorStrokeColor,
       outlineColor:"#666",
       outlineWidth:1
    },
    //鼠标悬浮连接器样式
    hoverPaintStyle:hoverPaintStyle, 
    //锚
    anchor:"AutoDefault",
    detachable:false,
    //端点样式
    endpointStyle:{ 
        gradient : { 
         stops:[[0, connectorStrokeColor], [1, connectorHighlightStrokeColor]],
         offset:17.5, 
         innerRadius:15 
        }, 
        //端点半径
        radius:35
    }, 
    //覆盖物label显示时间
    label : function(connection) { 
     var d = new Date();
     var fmt = function(n, m) { m = m || 10;  return (n < m ? new Array(("" + m).length - (""+n).length + 1).join("0") : "") + n; }; 
     return (fmt(d.getHours()) + ":" + fmt(d.getMinutes()) + ":" + fmt(d.getSeconds())+ "." + fmt(d.getMilliseconds(), 100)); 
    },
    labelStyle:{
     cssClass:"component label"
    }
     });
 

            //
            //this connects window5 with window6 using a Flowchart connector that is painted green,
            //with large Dot endpoints that are placed in the center of each element.  there is a
            //label at the default location of 0.5, and the connection is marked as not being
            //detachable.
   //
         var conn4Color = "rgba(46,164,26,0.8)";
         var connection4 = jsPlumb.connect({  
    source:'window5', 
    target:'window6', 
    //连接器采用流程图连线,cornerRadius流程图线在折线处为圆角
    connector:[ "Flowchart", { cornerRadius:10 } ],
    //锚,连接器在两个window连接的位置
    anchors:["Center", "Center"],  
    //连接线样式
    paintStyle:{ 
     lineWidth:9, 
     strokeStyle:conn4Color, 
     outlineColor:"#666",
     outlineWidth:2,
     joinstyle:"round"
    },
    //鼠标悬浮连接线样式
    hoverPaintStyle:hoverPaintStyle,
    detachable:false,
    endpointsOnTop:false, 
    //端点样式
    endpointStyle:{ radius:65, fillStyle:conn4Color },
    labelStyle : { cssClass:"component label" },
    label : "big\nendpoints"
      });
 
         var connection5 = jsPlumb.connect({
    source:"window4", 
    target:"window5", 
    //锚点位置
    anchors:["BottomRight", "TopLeft"], 
    //连接线样式
    paintStyle:{ 
     lineWidth

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值