如何使用jquery-ui使可拖动的div可调整大小

时间:2018-02-13 12:51:32

标签: javascript jquery html css jquery-ui

我创建了一些小部件,屏幕上的任何位置都应为draggable,用户应为resizable。我设法制作了draggable,但无法制作resizable。这是小提琴:

https://jsfiddle.net/vaxobasilidze/reL880g8/

我做错了什么?我也有其他元素,我以同样的方式制作可拖动和可调整大小,但由于未知原因,这不仅适用于此小部件。



function foo(){
  $('.channel').each(function() {
    $(this).draggable({
      containment: $('.screens'),
      stack: '.channel',
      drop: function (event, ui) {
      
      },
      drag: function(){
        var draggedItemId = $(this).attr('id');
      },
      stop: function(){
        var draggedItemId = $(this).attr('id');
      }
    }).resizable();
  });
}

function makeDraggable(){
  var height = $('.channels').height();
  $('.ram').draggable({
    stack: '.ram',
  }).resizable();
}

makeDraggable();
foo();

html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background: #2c2c2c;
    background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIxMDAlIiB5Mj0iMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzJjMmMyYyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjM3JSIgc3RvcC1jb2xvcj0iIzNkM2QzZCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjY2JSIgc3RvcC1jb2xvcj0iIzM1MzUzNSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjY2JSIgc3RvcC1jb2xvcj0iIzM1MzUzNSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiMxMzEzMTMiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
    background: -moz-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    background: -webkit-gradient(linear, left top, right top, color-stop(0%, #2c2c2c), color-stop(37%, #3d3d3d), color-stop(66%, #353535), color-stop(66%, #353535), color-stop(100%, #131313));
    background: -webkit-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    background: -o-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    background: -ms-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    background: linear-gradient(to right, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2c2c2c', endColorstr='#131313', GradientType=1);
}

body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    color: #b8c0c8;
    background: rgba(0, 0, 0, 0.3);
}

.ram {
  width: 250px;
  height: 140px;
  min-width: 250px;
  min-height: 140px;
  border: none;
  background: none;
  box-shadow: none;
  resize: none;
  padding: 7px;
  border: 1px solid grey;
}

.ram:hover {
  resize: both;
}

#ramUsageContainer {
  width: 100%;
  height: 100%;
  position: relative;
}

#ramUsageContainer svg {
  -webkit-filter: drop-shadow(0 0 3px #647384 );
  filter: drop-shadow(0 0 3px #647384 );
}

.usageHeader {
  width: 100%;
  text-align: center;
  font-size: 24px;
  position: absolute;
  top: 25%;
}

.usagePercent {
  width: 100%;
  text-align: center;
  font-size: 30px;
  position: absolute;
  top: 40%;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head runat="server">
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ResizeSensor.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ElementQueries.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jsPlumb/2.1.8/jsPlumb.js"></script>
<body>
    <div class="channel elementTable widget ui-draggable ui-draggable-handle Usage ram widgetDropped ui-resizable" name="RAM" id="2" style="left: 564px; top: 284px; z-index: 1003;" oncontextmenu="return false;">                                 
      <div id="ramUsageContainer">
        <p class="usageHeader">RAM Usage</p>
        <p class="usagePercent">65.7%</p>
        <svg viewBox="0 0 100 50">
          <path d="M 50,50 m -47,0 a 47,47 0 1 1 94,0" stroke="#353740" stroke-width="6" fill-opacity="0"></path>
          <path d="M 50,50 m -47,0 a 47,47 0 1 1 94,0" stroke="rgb(167,87,0)" stroke-width="6" fill-opacity="0" style="stroke-dasharray: 147.708, 147.708; stroke-dashoffset: 50.6639;"></path>
        </svg>
      </div>
  </div>
</body>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

function foo(){
  $('.channel').each(function() {
    $(this).resizable().draggable({
      containment: $('.screens'),
      stack: '.channel',
      drop: function (event, ui) {
      
      },
      drag: function(){
        var draggedItemId = $(this).attr('id');
      },
      stop: function(){
        var draggedItemId = $(this).attr('id');
      }
    });
  });
}

function makeDraggable(){
  var height = $('.channels').height();
  $('.ram').resizable().draggable({
    stack: '.ram',
  });
}

makeDraggable();
foo();
html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    background: #2c2c2c;
    background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIxMDAlIiB5Mj0iMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzJjMmMyYyIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjM3JSIgc3RvcC1jb2xvcj0iIzNkM2QzZCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjY2JSIgc3RvcC1jb2xvcj0iIzM1MzUzNSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjY2JSIgc3RvcC1jb2xvcj0iIzM1MzUzNSIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiMxMzEzMTMiIHN0b3Atb3BhY2l0eT0iMSIvPgogIDwvbGluZWFyR3JhZGllbnQ+CiAgPHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEiIGhlaWdodD0iMSIgZmlsbD0idXJsKCNncmFkLXVjZ2ctZ2VuZXJhdGVkKSIgLz4KPC9zdmc+);
    background: -moz-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    background: -webkit-gradient(linear, left top, right top, color-stop(0%, #2c2c2c), color-stop(37%, #3d3d3d), color-stop(66%, #353535), color-stop(66%, #353535), color-stop(100%, #131313));
    background: -webkit-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    background: -o-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    background: -ms-linear-gradient(left, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    background: linear-gradient(to right, #2c2c2c 0%, #3d3d3d 37%, #353535 66%, #353535 66%, #131313 100%);
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#2c2c2c', endColorstr='#131313', GradientType=1);
}

body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    color: #b8c0c8;
    background: rgba(0, 0, 0, 0.3);
}

.ram {
  width: 250px;
  height: 140px;
  min-width: 250px;
  min-height: 140px;
  border: none;
  background: none;
  box-shadow: none;
  resize: none;
  padding: 7px;
  border: 1px solid grey;
}

.ram:hover {
  resize: both;
}

#ramUsageContainer {
  width: 100%;
  height: 100%;
  position: relative;
}

#ramUsageContainer svg {
  -webkit-filter: drop-shadow(0 0 3px #647384 );
  filter: drop-shadow(0 0 3px #647384 );
}

.usageHeader {
  width: 100%;
  text-align: center;
  font-size: 24px;
  position: absolute;
  top: 25%;
}

.usagePercent {
  width: 100%;
  text-align: center;
  font-size: 30px;
  position: absolute;
  top: 40%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<head runat="server">
    <title>Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ResizeSensor.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/css-element-queries/0.4.0/ElementQueries.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jsPlumb/2.1.8/jsPlumb.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css"/>

<body>
    <div class="channel elementTable widget ui-draggable ui-draggable-handle Usage ram widgetDropped ui-resizable" name="RAM" id="2" style="left: 564px; top: 284px; z-index: 1003;" oncontextmenu="return false;">
      <div class="ui-resizable-handle ui-resizable-e"></div>
      <div class="ui-resizable-handle ui-resizable-s"></div>                                 
      <div id="ramUsageContainer">
        <p class="usageHeader">RAM Usage</p>
        <p class="usagePercent">65.7%</p>
        <svg viewBox="0 0 100 50">
          <path d="M 50,50 m -47,0 a 47,47 0 1 1 94,0" stroke="#353740" stroke-width="6" fill-opacity="0"></path>
          <path d="M 50,50 m -47,0 a 47,47 0 1 1 94,0" stroke="rgb(167,87,0)" stroke-width="6" fill-opacity="0" style="stroke-dasharray: 147.708, 147.708; stroke-dashoffset: 50.6639;"></path>
        </svg>
      </div>
  </div>
</body>

首先添加resizable,然后像draggable那样调用$(this).resizable().draggable()我在代码中进行了此类更改。你现在可以使用相同的代码。

答案 1 :(得分:1)

我在代码中发现了一个问题。代码是正确的,仅仅是因为未知的原因,加载后保存的图表应用程序在窗口小部件边框上创建新的调整大小传感器。所以我几乎没有把调整大小的传感器放在彼此的顶部,但只有其中一个正在工作,我无法点击。对我来说,完美的解决方案是使用以下代码删除旧的调整大小传感器:

$('.channel .resize-sensor').remove();
$('.channel .ui-resizable-handle').remove();

之后,我只是调用另一个使它们可拖动并可调整大小的函数:

makeDraggable();

答案 2 :(得分:0)

你可以通过使用java-script实现这一点这里是工作演示和在Css唯一重要的风格是位置:绝对,其余由你决定:

dragElement(document.getElementById(("mydiv")));

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    /* if present, the header is where you move the DIV from:*/
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    /* otherwise, move the DIV from anywhere inside the DIV:*/
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
  }

  function elementDrag(e) {
    e = e || window.event;
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    /* stop moving when mouse button is released:*/
    document.onmouseup = null;
    document.onmousemove = null;
  }
}
#mydiv {
    position: absolute;
    z-index: 9;
    background-color: #f1f1f1;
    text-align: center;
    border: 1px solid #d3d3d3;
      width: 300px;
resize: both;
overflow: auto;
}

#mydivheader {
    padding: 10px;
    cursor: move;
    z-index: 10;
    background-color: #2196F3;
    color: #fff;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Draggable DIV Element</h1>

<p>Click and hold the mouse button down while moving the DIV element</p>

<div id="mydiv">
  <div id="mydivheader">Click here to move</div>
  <p>Move</p>
  <p>this</p>
  <p>DIV</p>
</div>

参考来自: - https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_draggable