我创建了一些小部件,这些小部件应该在屏幕上的任意位置为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>

最佳答案

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()我对代码进行了此类更改。您现在可以使用相同的代码。

10-07 19:38