0

As recommended by other users, I post a new question in order to ask for help with a scaling issue, instead of trying to complete the already existing one : similar question. I am trying to set up a zoom by scroll on a specific element on a page (an image). At first I used transform : scale(number); But it seems to mess up the overflow preventing to fully scroll the image (a part of the image on the left and the top is hidden). I couldn't find a way to be able to fully scroll the image AND set the transform origin on the mouse position. I am currently trying to find a workaround by putting the image inside a div container and changing the container width/height. It seems to be working pretty good, the only issue is with setting the offset of the div in order to match the mouse position (what setting transform-origin was doing pertty well)...

Here is my code with both methods to zoom in... The mouseWheel js is from some random sites which had a demo on it, I don't think it's the latest version. As you can see, I can zoom in and out, but then I can't fully scroll the objects, I have fixed it a bit by putting the objects inside an other div, but it messes the position up so it does not sound like a solution...

#container1 {
  position: fixed;
  width: 300px;
  height: 200px;
  border: 1px solid #000000;
  left: 0px;
  top: 0px;
  margin-left: 10px;
  margin-top: 10px;
  overflow: auto;
  display: block;
  text-align: center;
}
#object1 {
  position: absolute;
  width: 100px;
  height: 100px;
  display: inline-block;
  background-color: rgba(255, 0, 255, 0.45);
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 25px;
}
#container2 {
  position: fixed;
  width: 300px;
  height: 200px;
  border: 1px solid #000000;
  left: 0px;
  top: 0px;
  margin-left: 330px;
  margin-top: 10px;
  overflow: auto;
  display: block;
  text-align: center;
}
#object2 {
  position: absolute;
  width: 100px;
  height: 100px;
  display: inline-block;
  background-color: rgba(0, 0, 255, 0.45);
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  border-radius: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://owlcarousel.owlgraphic.com/assets/vendors/jquery.mousewheel.min.js"></script>
<div id="container1">

  <div id="object1"></div>

</div>

<div id="container2">

  <div id="object2"></div>

</div>

<script type="text/javascript">
  var scale = 1;
  var object2 = document.getElementById("object2");
  $('#object2').on("mousewheel", function(event) {
    scale += event.deltaY < 1 ? -0.3 : 0.3;
    scale = scale < 1 ? 1 : scale;
    scale = scale > 5 ? 5 : scale;
    object2.style.transform = "scale(" + scale + ")";
    object2.style.transformOrigin = event.offsetX + "px " + event.offsetY + "px";
    event.preventDefault();
  });
  var scale2 = 1;
  var object1 = document.getElementById("object1");
  $('#object1').on("mousewheel", function(event) {
    scale2 += event.deltaY < 1 ? -0.3 : 0.3;
    scale2 = scale2 < 1 ? 1 : scale2;
    scale2 = scale2 > 5 ? 5 : scale2;
    object1.style.width = (100 * scale2) + "px";
    object1.style.height = (100 * scale2) + "px";
    event.preventDefault();
  });
</script>

Here is the same thing with a container div, just so you can see how it messes up the position :

#container1 {
  position: fixed;
  width: 300px;
  height: 200px;
  border: 1px solid #000000;
  left: 0px;
  top: 0px;
  margin-left: 10px;
  margin-top: 10px;
  overflow: auto;
  display: block;
  text-align: center;
}
#objectContainer {
  position: relative;
  width: 100px;
  height: 100px;
  margin: auto;
  bottom: 0;
  right: 0;
}
#object1 {
  position: absolute;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  display: inline-block;
  background-color: rgba(255, 0, 255, 0.45);
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 25px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://owlcarousel.owlgraphic.com/assets/vendors/jquery.mousewheel.min.js"></script>
<div id="container1">
  <div id="objectContainer">
    <div id="object1"></div>
  </div>
</div>
<script type="text/javascript">
  var scale2 = 1;
  var container1 = document.getElementById("objectContainer");
  $('#object1').on("mousewheel", function(event) {
    scale2 += event.deltaY < 1 ? -0.3 : 0.3;
    scale2 = scale2 < 1 ? 1 : scale2;
    scale2 = scale2 > 5 ? 5 : scale2;
    container1.style.width = (100 * scale2) + "px";
    container1.style.height = (100 * scale2) + "px";
    event.preventDefault();
  });
</script>
Community
  • 1
  • 1
Tab
  • 81
  • 1
  • 11
  • There is an example on the "similar question" link, but here is the link to the example Ryan Castle wrote : http://codepen.io/anon/pen/waoEVQ?editors=110 – Tab Aug 20 '15 at 11:59
  • So you need to show us what **you** have done and why it isn't working. – Paulie_D Aug 20 '15 at 12:01
  • It's the same issue, for the same reasons, my code is similar to what Ryan posted. The only difference is with the scroll to zoom, but the real issue is with the transform messing up the scroll, but I will code it and post it. – Tab Aug 20 '15 at 12:05
  • Okay, I added a code snippet with the issue and both ways to zoom in and out (with/height and scale) – Tab Aug 20 '15 at 14:23

0 Answers0