分类目录

链接

2022 年 10 月
 12
3456789
10111213141516
17181920212223
24252627282930
31  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > 前端 > 正文
js/jquery实现连线拖拽
前端 暂无评论 阅读(61)

先看效果图:

code:

  1. <!DOCTYPE html>
  2. <html>
  3. <body>
  4. <style type="text/css">
  5.     body, svg{width: 100vh;
  6.         height: 100vh;}
  7.         svg{
  8.             position: relative;
  9.         }
  10. </style>
  11. <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
  12. <h1>My first SVG</h1>
  13.  
  14. <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
  15.    <circle  cx="100" cy="100" r="20" stroke="black" stroke-width="1" fill="rgb(200,200,200)"   />
  16.  
  17.      <rect width="40" height="40" x="300"  y="100"
  18.            style="fill:rgb(255,255,255,0.8);stroke-width:1;stroke:rgb(0,0,0,0.5)" >
  19.     </rect> 
  20.      <line x1="0" y1="0" x2="0" y2="0" style="stroke:rgb(0,0,0);stroke-width:1"/>
  21. </svg> 
  22.  
  23. <script type="text/javascript">
  24.     $(function(){
  25.         
  26.         
  27.  
  28.         function draw(){
  29.             let circle = $('circle')[0];
  30.             let rect = $('rect')[0];
  31.             let line = $('line')[0];
  32.             
  33.             console.log(circle.cx.baseVal.value)
  34.             console.log(circle.cy.baseVal.value)
  35.             line.setAttribute("x1", circle.cx.baseVal.value);
  36.             line.setAttribute("y1", circle.cy.baseVal.value);
  37.  
  38.             line.setAttribute("x2", rect.x.baseVal.value+rect.width.baseVal.value/2);
  39.             line.setAttribute("y2", rect.y.baseVal.value+rect.height.baseVal.value/2);
  40.         }
  41.  
  42.          function dragable(el) { 
  43.             el.mousedown(function(e1) { 
  44.                 $(document).on("mousemove", function(e2){
  45.                     console.log(e2);
  46.                     el[0].setAttribute("x", e2.offsetX-el[0].width.baseVal.value/2);
  47.                     el[0].setAttribute("y", e2.offsetY-el[0].width.baseVal.value/2);
  48.  
  49.                     draw();
  50.                 });
  51.             });
  52.  
  53.             $(document).mouseup(function(e){ 
  54.                   $(document).off("mousemove");
  55.             });
  56.         }
  57.  
  58.         //init
  59.         draw();
  60.         dragable($('rect'));
  61.     });
  62.     
  63.  
  64.     
  65.     
  66.      
  67. </script>
  68.  
  69. </body>
  70. </html>

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:js/jquery实现连线拖拽 | Bruce's Blog

发表评论

留言无头像?