-1

I have an asset that has to be statically sized based on viewport height. The problem is that for some mobile browsers when the navigation bar hides, the asset changes size, because the height of the window changes. I tried window.innerHeight, visualViewport.height, clientHeight etc. but none of them seem to represent 100svh. I have a fix for now that creates a 100svh element and measures it, but it seems hacky and I dont want this element in my DOM. I am using React and with a hook i get the size whenever the screen resizes. Any ideas how to get 100svh in Javascript without creating a dummy element in the DOM?

5
  • 1
    "how to get 100svh in Javascript" - what for? Why can't you use 100svh directly, why would you need to "measure" it using JS in the first place? Please provide a proper minimal reproducible example of the issue. Commented yesterday
  • The asset itself is not 100svh, I am using threejs and I need fixed values which i calculate based on window height. Commented yesterday
  • 1
    getComputedStyle in combination with a CSS variable does not appear to work (only gets the assigned string value, even if wrapped in calc()), so I think you'll need an element in the live DOM. If you do not want to create one, you could set a property that accepts a length value on an existing element perhaps - like left: 100svh; or something on an element you know will have position: static, so that left won't actually do anything - but you can still read the actual value using getComputedStyle. Commented yesterday
  • This question lacks details. As it is, it is expecting us to understand your exact requirement, build a test code and try to reproduce your issue and provide a solution. This takes considerable effort on our part and all that effort may be in vain if we misunderstand your requirements. Hence you will not get answers. Therefore, you will need to edit your question and add details about what the exact requirement is and what code you have written and how exactly did that fail. Commented yesterday
  • 1
    And you're sure there's no way you can design your interface to be responsive? We just have to take your word for it, but it sounds weird. Have you also thought about what would happen if the user rotates the device from a portrait to a landscape orientation or visa-versa? You'll have to lock it and hope that it works. More problems because of one bad decision: "My asset has to be statically sized.". Does it really have to? Convince us. Apart from that, it is an interesting question. Commented yesterday

2 Answers 2

1

You can't get the true 100svh value directly from JavaScript. The small viewport height is a CSS-only concept that browsers calculate internally.

Since your asset is in React, just apply 100svh in CSS directly:

.asset{
    height: 100svh;
}

Browsers handle the mobile nav hiding automatically. You don't need JavaScript for this. Only use JS if you absolutely need the height value for calculations.

New contributor
Shirley is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

1

what about using visualViewport.height && take frist value by chek it`s no scrole happen the logic will using frist On page load, check visualViewport.heigh sceond Make sure no scroll has happened visualViewport.pageTop === 0 then Store the first value as the real 100svh in the last Ignore any further changes caused by scroll or navbar hide/show. code

let initialSVH = null;

function captureInitialSVH() {
  if (initialSVH === null && visualViewport.pageTop === 0) {
    initialSVH = visualViewport.height;
    console.log("Captured 100svh:", initialSVH);
  }
}

visualViewport.addEventListener("resize", captureInitialSVH);

// Optional: run once immediately
captureInitialSVH();

or use dummy element

createElement('div').style.height = '100svh'
  

this a best soultion

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.