How to keep a
constant in size as the user zooms in and out on the page?

后端 未结 6 1729
北荒
北荒 2020-12-11 19:30

Is there an html / css / javascipt way to maintain a

at a constant size in the face of the user\'s zooming the page in and out? That is, using control-plus to incre
相关标签:
6条回答
  • 2020-12-11 19:46

    I am not sure what you mean, just use css:

    div#id {
        width: 100px; /*or some other #*/
        height: 100px; /*or some other #*/
    }
    

    html:

    <div id="id">some content</div>
    
    0 讨论(0)
  • 2020-12-11 19:48

    You should just be ablemto set a width and height in css using a px measurement

    Eg

    div
    {
    width:100px; height:200px;
    }
    
    0 讨论(0)
  • 2020-12-11 19:51

    I read in another post a solution that I didn't test yet...

    Maintain div size (relative to screen) despite browser zoom level

    that's the used javascript:

    //This floating div function will cause a div to float in the upper right corner of the screen at all times. However, it's not smooth, it will jump to the proper location once the scrolling on the iPhone is done. (On my Mac, it's pretty smooth in Safari.) 
    
    function flaotingDiv(){
    
        //How much the screen has been zoomed.
        var zoomLevel = ((screen.width)/(window.innerWidth));
        //By what factor we must scale the div for it to look the same.
        var inverseZoom = ((window.innerWidth)/(screen.width));
        //The div whose size we want to remain constant.
        var h = document.getElementById("fontSizeDiv");
    
        //This ensures that the div stays at the top of the screen at all times. For some reason, the top value is affected by the zoom level of the Div. So we need to multiple the top value by the zoom level for it to adjust to the zoom. 
        h.style.top = (((window.pageYOffset) + 5) * zoomLevel).toString() + "px";
    
        //This ensures that the window stays on the right side of the screen at all times. Once again, we multiply by the zoom level so that the div's padding scales up.
        h.style.paddingLeft = ((((window.pageXOffset) + 5) * zoomLevel).toString()) + "px";
    
        //Finally, we shrink the div on a scale of inverseZoom.
        h.style.zoom = inverseZoom;
    
    }
    
    //We want the div to readjust every time there is a scroll event:
    window.onscroll = flaotingDiv;
    
    0 讨论(0)
  • 2020-12-11 19:57

    To make the div size invariant of zooming (But not contents inside it) do the following :
    Inside your css for that div :

    min-width: 100%;
    max-width: 100%;

    This will freeze the width, you can do the same for height too.

    0 讨论(0)
  • 2020-12-11 20:00

    .box {
        background: red;
        width: 5vw;
        height: 10vh;
        position: absolute;
        top: 10vh;
        left: 5vw;
    }
    <div class="box"></div>

    0 讨论(0)
  • 2020-12-11 20:10

    There is no good way (read: reliable) to do this. Sorry.

    What you're asking for basically boils down to detecting the zoom level of the browser, and there's a great answer here (confirming just how difficult this is):

    • How to detect page zoom level in all modern browsers?

    As stated in that answer, there is a "kinda" cross-browser crazy way involving the use of Flash, but there are downsides:

    • It uses Flash.
    • It's not reliable if the user loads your page already zoomed in.
    • It uses Flash. Yes, this is so bad that I said it twice. Think of all those iPhones/iPads.

    Anyway, it's here:
    http://blog.sebastian-martens.de/2009/12/how-to-detect-the-browser-zoom-level-change-browser-zoo/

    0 讨论(0)
提交回复
热议问题