1

I want to change scale of visualViewport. I want to control zoom pragmatically on certain condition. How can I achieve this?

Edit: The window.visualViewport.scale is totally different from the zoom of the CSS transforms which was previously referred as a solution to this problem and as a reason for locking this question. CSS transforms operate inside the layout viewport while visualViewport scale does not affect the layout per design.

loop
  • 825
  • 6
  • 15
Joker_37
  • 839
  • 2
  • 8
  • 20

1 Answers1

0

This i believe from what I've researched and read will need an API. I have found some code on the subject and a link to where you can understand how this API works. I think if I understand your question, this will be a solution:

let pendingUpdate = false;

function viewportHandler() {
  if (pendingUpdate) return;
  pendingUpdate = true;

  requestAnimationFrame(() => {
    pendingUpdate = false;
    var layoutViewport = document.getElementById('layoutViewport');

    // Since the bar is position: fixed we need to offset it by the
    // visual viewport's offset from the layout viewport origin.
    var offsetLeft = viewport.offsetLeft;
    var offsetTop = viewport.height
                - layoutViewport.getBoundingClientRect().height
                + viewport.offsetTop;

    // You could also do this by setting style.left and style.top if you
    // use width: 100% instead.
    bottomBar.style.transform = 'translate(' +
                                offsetLeft + 'px,' +
                                offsetTop + 'px) ' +
                                'scale(' + 1/viewport.scale + ')'
    }
}

window.visualViewport.addEventListener('scroll', viewportHandler);
window.visualViewport.addEventListener('resize', viewportHandler);

Check it out: https://developer.mozilla.org/en-US/docs/Web/API/Visual_Viewport_API

billy.farroll
  • 1,903
  • 2
  • 15
  • 23