Changing the browser zoom level

后端 未结 8 1609
野的像风
野的像风 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:11

    You can use the CSS3 zoom function, but I have not tested it yet with jQuery. Will try now and let you know. UPDATE: tested it, works but it's fun

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

    Possible in IE and chrome although it does not work in firefox:

    <script>
       function toggleZoomScreen() {
           document.body.style.zoom = "80%";
       } 
    </script>
    
    <img src="example.jpg" alt="example" onclick="toggleZoomScreen()">
    
    0 讨论(0)
  • 2020-11-22 07:16

    <html>
      <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <script>
            var currFFZoom = 1;
            var currIEZoom = 100;
    
            function plus(){
                //alert('sad');
                    var step = 0.02;
                    currFFZoom += step;
                    $('body').css('MozTransform','scale(' + currFFZoom + ')');
                    var stepie = 2;
                    currIEZoom += stepie;
                    $('body').css('zoom', ' ' + currIEZoom + '%');
    
            };
            function minus(){
                //alert('sad');
                    var step = 0.02;
                    currFFZoom -= step;
                    $('body').css('MozTransform','scale(' + currFFZoom + ')');
                    var stepie = 2;
                    currIEZoom -= stepie;
                    $('body').css('zoom', ' ' + currIEZoom + '%');
            };
        </script>
        </head>
    <body>
    <!--zoom controls-->
                            <a id="minusBtn" onclick="minus()">------</a>
                            <a id="plusBtn" onclick="plus()">++++++</a>
      </body>
    </html>

    in Firefox will not change the zoom only change scale!!!

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

    Try if this works for you. This works on FF, IE8+ and chrome. The else part applies for non-firefox browsers. Though this gives you a zoom effect, it does not actually modify the zoom value at browser level.

        var currFFZoom = 1;
        var currIEZoom = 100;
    
        $('#plusBtn').on('click',function(){
            if ($.browser.mozilla){
                var step = 0.02;
                currFFZoom += step; 
                $('body').css('MozTransform','scale(' + currFFZoom + ')');
            } else {
                var step = 2;
                currIEZoom += step;
                $('body').css('zoom', ' ' + currIEZoom + '%');
            }
        });
    
        $('#minusBtn').on('click',function(){
            if ($.browser.mozilla){
                var step = 0.02;
                currFFZoom -= step;                 
                $('body').css('MozTransform','scale(' + currFFZoom + ')');
    
            } else {
                var step = 2;
                currIEZoom -= step;
                $('body').css('zoom', ' ' + currIEZoom + '%');
            }
        });
    
    0 讨论(0)
  • 2020-11-22 07:24

    I could't find a way to change the actual browser zoom level, but you can get pretty close with CSS transform: scale(). Here is my solution based on JavaScript and jQuery:

    <!-- Trigger -->
    <ul id="zoom_triggers">
        <li><a id="zoom_in">zoom in</a></li>
        <li><a id="zoom_out">zoom out</a></li>
        <li><a id="zoom_reset">reset zoom</a></li>
    </ul>
    
    <script>
        jQuery(document).ready(function($)
        {
            // Set initial zoom level
            var zoom_level=100;
    
            // Click events
            $('#zoom_in').click(function() { zoom_page(10, $(this)) });
            $('#zoom_out').click(function() { zoom_page(-10, $(this)) });
            $('#zoom_reset').click(function() { zoom_page(0, $(this)) });
    
            // Zoom function
            function zoom_page(step, trigger)
            {
                // Zoom just to steps in or out
                if(zoom_level>=120 && step>0 || zoom_level<=80 && step<0) return;
    
                // Set / reset zoom
                if(step==0) zoom_level=100;
                else zoom_level=zoom_level+step;
    
                // Set page zoom via CSS
                $('body').css({
                    transform: 'scale('+(zoom_level/100)+')', // set zoom
                    transformOrigin: '50% 0' // set transform scale base
                });
    
                // Adjust page to zoom width
                if(zoom_level>100) $('body').css({ width: (zoom_level*1.2)+'%' });
                else $('body').css({ width: '100%' });
    
                // Activate / deaktivate trigger (use CSS to make them look different)
                if(zoom_level>=120 || zoom_level<=80) trigger.addClass('disabled');
                else trigger.parents('ul').find('.disabled').removeClass('disabled');
                if(zoom_level!=100) $('#zoom_reset').removeClass('disabled');
                else $('#zoom_reset').addClass('disabled');
            }
        });
    </script>
    
    0 讨论(0)
  • 2020-11-22 07:28

    Not possible in IE, as the UI Zoom button in the status bar is not scriptable. YMMV for other browsers.

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