window.innerWidth in Chrome's device mode

后端 未结 4 2034
野的像风
野的像风 2021-01-08 00:29

When in google chrome\'s device mode, what does window.innerWidth return? Is it the viewport of the device (plus any scroll bars)?

I\'m getting different values for

4条回答
  •  迷失自我
    2021-01-08 00:46

    We're currently having success with something like:

        const widths = [window.innerWidth];
    
        if (window.screen?.width) {
          widths.push(window.screen?.width);
        }
    
        const width = Math.min(...widths);
    

    The conditional check is there because I'm not sure how widespread the screen width API is. You may need to adjust this not to use certain newer JS features depending on what devices you are targeting/your build process.

    This could potentially go a bit weird if you have a window that is wider than the screen, but for us that isn't a problem.

    This gives us a width that matches the one at the top of the Responsive screen tool, even when contents overflow horizontally. This is important for us because we needed the UI to change in order to prevent that overflow, but the overflow was interfering with the width number we used to trigger the adjustment.

    I'm not sure if this is important, but we are also using:

    
    

提交回复
热议问题