使用调整大小调整div的大小并使其可拖动

时间:2017-04-25 06:29:14

标签: javascript jquery html css jquery-ui

我希望div可拖动,当光标位于resize图标上时,div应该可以调整大小。如果我使用draggable方法和调整大小只有draggable工作,它没有可拖动的方法工作,但我希望它是可移动的,并在光标处于调整大小图标时调整大小。任何人都可以帮助我吗?



$("div").draggable();

div {
  margin: 30px;
  width: 200px;
  height: 200px;
  padding: 0.3%;
  z-index: 1;
  overflow: hidden;
  display: inline-block;
  resize: both;
}

img {
  z-index: 0;
  width: 99%;
  height: 99%;
  -webkit-user-select: none;
}

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div>
  <img draggable="false" src="http://www.crash.lshtm.ac.uk/Images/image44p.jpg"></img>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

<html>
<head>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/cupertino/jquery-ui.css" />
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<head>

<body>
<div id="rnd" style="display:inline-block">
<img id="img_rnd" style="border:1px solid red" src="http://www.crash.lshtm.ac.uk/Images/image44p.jpg" />
</div>
<script>
$('#img_rnd').resizable();
$('#rnd').draggable({
    appendTo: 'body',
    start: function(event, ui) {
        isDraggingMedia = true;
    },
    stop: function(event, ui) {
        isDraggingMedia = false;
        // blah
    }
});

</script>

</body>
</html>

这肯定会对你有所帮助。 :)

答案 1 :(得分:0)

你好我做了工作演示。

可能是jquery-ui css无法正常工作。检查此代码

<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Resizable - Default functionality</title>

  <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

  <style>
    #resizable { width: 150px; height: 150px; padding: 0.5em; }
    #resizable h3 { text-align: center; margin: 0; }
  </style>

  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

  <script>
    $( function() {
      /*aspectRatio option is maintain div aspect ratio so image will display properly*/
      $( "#resizable" ).resizable({'aspectRatio' :true});
      $( "#resizable" ).draggable();
    } );
  </script>
  <style>
      body {
        font-family: Arial, Helvetica, sans-serif;
      }
      table {
        font-size: 1em;
      }
      .ui-draggable, .ui-droppable {
        background-position: top;
      }
  </style>
</head>
<body>



<div id="resizable" class="ui-widget-content">
  <img id="resizable1" src="http://www.crash.lshtm.ac.uk/Images/image44p.jpg" style="width: 100%;height: auto"></img>
</div>


</body>
</html>