Changing the browser zoom level

后端 未结 8 1610
野的像风
野的像风 2020-11-22 07:00

I have need to create 2 buttons on my site that would change the browser zoom level (+) (-). I\'m requesting browser zoom and not css zoom because of image size and layout i

相关标签:
8条回答
  • 2020-11-22 07:29

    I would say not possible in most browsers, at least not without some additional plugins. And in any case I would try to avoid relying on the browser's zoom as the implementations vary (some browsers only zoom the fonts, others zoom the images, too etc). Unless you don't care much about user experience.

    If you need a more reliable zoom, then consider zooming the page fonts and images with JavaScript and CSS, or possibly on the server side. The image and layout scaling issues could be addressed this way. Of course, this requires a bit more work.

    0 讨论(0)
  • 2020-11-22 07:30

    as the the accepted answer mentioned, you can enlarge the fontSize css attribute of the element in DOM one by one, the following code for your reference.

     <script>
        var factor = 1.2;
        var all = document.getElementsByTagName("*");
        for (var i=0, max=all.length; i < max; i++) {
            var style = window.getComputedStyle(all[i]);
            var fontSize = style.getPropertyValue('font-size');
    
            if(fontSize){
                all[i].style.fontSize=(parseFloat(fontSize)*factor)+"px";
            }
            if(all[i].nodeName === "IMG"){
                var width=style.getPropertyValue('width');
                var height=style.getPropertyValue('height');
                all[i].style.height = (parseFloat(height)*factor)+"px";
                all[i].style.width = (parseFloat(width)*factor)+"px";
            }
        }
    </script>
    
    0 讨论(0)
提交回复
热议问题