1

I am working on the fiddle in which I want to constantly move/resize image(which is itself resizable/draggable image) over the video when the browser is resize.

The snippets of HTML/CSS/JS code which I have used is:

HTML:

<div id="wrapper" style="display:inline-block">
    <img id="image" src="http://www.google.com.br/images/srpr/logo3w.png" />
</div>

CSS:

.overlay {
  position:absolute;
  width:100%;
  height:100%;
  background:red;
  opacity:.5;
  display:none;
}

JS:

$(function() {
$('#wrapper').draggable();
$('#image').resizable({
start: function( event, ui ) {
   $('#overlay').show();
  },

stop: function( event, ui ) {
   $('#overlay').hide();
  }
  }
);
});


Problem Statement:

I am wondering what changes I should make in the JS code above so that whenever I resize the browser, the draggable/resizable image should also constantly move.

For example: Let us suppose I place the google image over the nose of a guy in full screen and if I resize the browser window, the google image doesn't seem to stay over the nose as shown in the fiddle https://jsfiddle.net/obn4yph0/embedded/result

john
  • 11,311
  • 40
  • 131
  • 251
  • You might find one of these helpful: [dragging items based on percentage to containment element](https://stackoverflow.com/questions/11115824/dragging-items-based-on-percentage-to-containment-element) or [jQuery Draggable, convert position to percentage](https://stackoverflow.com/questions/37910467/jquery-draggable-convert-position-to-percentage) or [Responsive jQuery UI with resizable / draggable elements](https://stackoverflow.com/questions/20727936/responsive-jquery-ui-with-resizable-draggable-elements). – showdev Oct 18 '18 at 18:54
  • @showdev These are helpful but I am not sure how I can implement in the real code. Just checking if you get my question. – john Oct 18 '18 at 19:14
  • @showdev Are you around ? – john Oct 18 '18 at 19:34
  • The idea is to position the element using percentage values relative to its container, rather than pixel values. – showdev Oct 18 '18 at 20:04
  • @showdev hi, I am wondering if you can explain in the fiddle so that I am able to visualize. Thanks. – john Oct 18 '18 at 20:10

1 Answers1

1

One idea is to position by percentage values relative to the container, rather than pixel values.

That way the positioning will be responsive, meaning that the position will be the same relative to the container regardless of the size of the container.

The calculations to convert pixels to percentages are based on this answer by peteykun.
Calculations are performed upon the stop events for both resizing and dragging.

And here's a JSFiddle (since the YouTube embed doesn't work here).

function convert_to_percentage($el) {

  var $parent = $el.parent();

  $el.css({
    left: parseInt($el.position().left) / $parent.width() * 100 + "%",
    top: parseInt($el.position().top) / $parent.outerHeight() * 100 + "%",
    width: $el.width() / $parent.width() * 100 + "%",
    height: $el.height() / $parent.outerHeight() * 100 + "%"
  });

}

$(function() {

  var $wrapper = $('#wrapper');
  var $overlay = $('#overlay');

  convert_to_percentage($wrapper);

  $wrapper
    .draggable({
      stop: function(event, ui) {
        convert_to_percentage($wrapper);
      }
    })
    .resizable({
      start: function(event, ui) {
        $overlay.show();
      },
      stop: function(event, ui) {
        $overlay.hide();
        convert_to_percentage($wrapper);
      }
    });
});
#wrapper {
  z-index: 100;
  position: absolute;
}

#wrapper img {
  width: 100%;
  height: 100%;
}

.embed-responsive {
  position: relative;
}

.overlay {
  position: absolute;
  width: 100%;
  height: 100%;
  background: red;
  opacity: .5;
  display: none;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

<div class="embed-responsive embed-responsive-16by9">
  <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/zpOULjyy-n8?rel=0" allowfullscreen></iframe>
  <div class="overlay" id="overlay" />
</div>

<div id="wrapper" style="display:inline-block">
  <img id="image" src="http://www.google.com.br/images/srpr/logo3w.png" />
</div>
showdev
  • 28,454
  • 37
  • 55
  • 73
  • Thanks, it did work. That's the solution, I am looking for. I have created an overlay over video. It resize when event starts and hide when event stops. Although I have decided to go with overlay concept, parent div class of overlay doesn't seem to have closing tag. I am asking your opinion where the closing tag of div should be. Let me know your thoughts because `
    ` or `
    ` (either one of them) doesn't have a closing div tag.
    – john Oct 19 '18 at 01:05
  • Are you around ? – john Oct 19 '18 at 14:12
  • Just checking if you are still there. – john Oct 19 '18 at 16:14
  • @john The `.overlay` div is self-closing, hence the `/>` at the end (versus `>`). Since it has no textNode/children and gets it's dimensions from CSS this is fine. – coreyward Oct 19 '18 at 16:26
  • @coreyward Thanks for letting me know. Do you know js ? The Js code being used here is converting everything in %. http://jsfiddle.net/butnsv18/ Let me know when you see it in the function `convert_to_percentage($el)` – john Oct 19 '18 at 16:28
  • 1
    @john Sorry, I had gone home last night. I based the HTML off of your JSFiddle. My apologies, I'm not sure what you're asking. – showdev Oct 19 '18 at 16:44
  • @showdev No worries I think coreyward already answered my question. You can have a look. In your answer, you convert `left, top, height and width in percentage instead of pixels` if I am not wrong. When I checked my base code, I do have a code which is doing the same but in pixel. I am wondering if there is any we can integrate that. Here is code for that http://jsfiddle.net/km0bz8d2/ (The fiddle is not doing anything atm, I have just pasted my code there so that you can have a look). – john Oct 19 '18 at 16:51
  • Very cool. Is it working? Is it possible to make a working fiddle to demonstrate? I'm not sure how some of the variables are defined, like `area`, `coords`, `boundaries`,`scaleX`, `scaleY`. – showdev Oct 19 '18 at 16:58
  • I tried to integrate but it didn’t work. I am wondering if you can give me a demo to do that. – john Oct 19 '18 at 17:13
  • Can I see what you've got? – showdev Oct 19 '18 at 17:15
  • Sure, I will send it to you. Give me 5 mins – john Oct 19 '18 at 18:26