2

I created a regular image in HTML. Then, dynamically via JavaScript, I added an image within the initial image. However, I am having an issue where if a user were to zoom in or out, the internal image does not stay in the same place. And it is possible that a user could zoom in my case, anytime, and I really need the internal image not to move. I do not have anything fancy, and I am trying all sorts of ways to get this to work, so if you have a way that uses HTML and CSS, I could add it to my code, because I have tried many different avenues and changed my HTML around multiple times. However, if you would like my code, in depth, I would be glad to supply it with you. Any help is greatly appreciated, or if you simply need more clarification, I can do that as well, thanks in advance.

Here is an example of the code that adds an image to an image, and has the issue that I explained above:

$(document).ready(function() {

  $('#myImgId').click(function(e) {

    var offX = event.clientX;
    var offY = event.clientY;

    margin = 20;

    if (offX > margin) offX -= margin;
    if (offY > margin) offY -= margin;

    var signHereImage = document.createElement("img");

    signHereImage.setAttribute('src', 'imageInserted.jpg');
    signHereImage.setAttribute('class', 'overlays');
    signHereImage.style.left = offX + "px";
    signHereImage.style.top = offY + "px";

    document.body.appendChild(signHereImage);

  });
});
<form id="form1" runat="server">
  <div>
    <img src="page3.jpg" alt="PDF Image" id="myImgId" />
  </div>
</form>

This is for the most part what it is, I know that I am doing something incorrectly, I just do not know what for certain, thanks again in advance.

6
  • 3
    Please add your code, just enough to replicate the issue also some images of the desired output if you can Commented Apr 7, 2017 at 20:12
  • sure, give me a few. Commented Apr 7, 2017 at 20:16
  • Here is a brief example: Commented Apr 7, 2017 at 20:18
  • Use the edit button to include it on your question Commented Apr 7, 2017 at 20:19
  • Thx DaniP, I realized that right after I accidentally almost included it in here. As you can see, I haven't utilized stack o' flow too much in my career, it's my own folly. Commented Apr 7, 2017 at 20:58

2 Answers 2

2

I would use the background-image property to achieve this. You can place the image in a div, give that div a background image, and then position the image relative to the div. Here's a working example:

div {
  position: relative;
  height: 500px;
  background-image: url(https://images.unsplash.com/photo-1413781892741-08a142b23dfe?dpr=2&auto=format&fit=crop&w=1500&h=1000&q=80&cs=tinysrgb&crop=&bg=);
  background-size: cover;
}

img {
  position: absolute;
  margin: auto;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  width: 200px;
}
<div>
  <img alt="img" src="https://images.unsplash.com/photo-1483086431886-3590a88317fe?dpr=2&auto=format&fit=crop&w=1500&h=2247&q=80&cs=tinysrgb&crop=&bg=" />
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks H.T. for the rapid response and it looks like a great answer. I will try it on Monday, as my day is finally ova!! I didn't think that I would get responses so quickly.
Cool! Good luck!
1

Consider what changes when a user zooms in or out on a web page. The viewport size changes, relative to the content on the page.

You are probably styling the internal image based on something related to the viewport. The automatic width of block elements is calculated from the width of the viewport if other constraints are not available. For example, an element with the following CSS properties moves around with the right edge of the viewport:

position: absolute;
right: 20px;

See a demo.

Other elements, perhaps including the external image of yours, are by default laid out line-by-line, starting from the top left. This discrepancy can cause some elements of the page to move with respect to others when zooming or resizing the window.

Review how you instruct the browser to lay out these two images, and make sure that, if they depend on the size of the viewport, they depend on it in the same way.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.