Bhanu Tharun
Bhanu Tharun

Reputation: 134

Resizing a div with resize and make it draggable

I want the div draggable and when the cursor is on the resize icon that div should be resizable . If i use draggable method and resize only draggable is working, it worked without draggable method, but i want it to be movable as well as get resized when cursor is at resize icon . Can anyone help me in this?

$("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>

Upvotes: 2

Views: 3091

Answers (2)

Naitik Kundalia
Naitik Kundalia

Reputation: 309

Hello i made the working demo.

May be jquery-ui css is not working properly. Check this code

<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>

Upvotes: 0

Unknown_Coder
Unknown_Coder

Reputation: 774

<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>

This will definitely help you. :)

Upvotes: 1

Related Questions