Live Detect Browser Size - jQuery / JavaScript

前端 未结 6 1332
梦毁少年i
梦毁少年i 2020-12-02 10:46

Is there a jQuery plugin or a way using straight JavaScript to detect browser size.

I\'d prefer it is the results were \'live\', so if the width or height changes, s

相关标签:
6条回答
  • 2020-12-02 11:17

    This should return the visible area:

    document.body.offsetWidth
    document.body.offsetHeight
    

    I guess this is always equal to the browser size?

    0 讨论(0)
  • 2020-12-02 11:21

    You can try adding even listener on re-size like

    window.addEventListener('resize',CheckBrowserSize,false);
    function CheckBrowserSize() 
    {
        var ResX= document.body.offsetHeight;
        var ResY= document.body.offsetWidth;
    }
    
    0 讨论(0)
  • 2020-12-02 11:22

    Do you mean something like this window.innerHeight; window.innerWidth $(window).height(); $(window).width()

    0 讨论(0)
  • 2020-12-02 11:27

    you can use

    function onresize (){
       var h = $(window).height(), w= $(window).width();
       $('#resultboxid').html('height= ' + h + ' width: ' w);
    }
     $(window).resize(onresize ); 
    
     onresize ();// first time;
    

    html:

    <span id=resultboxid></span>
    
    0 讨论(0)
  • 2020-12-02 11:31

    JavaScript

    function jsUpdateSize(){
        // Get the dimensions of the viewport
        var width = window.innerWidth ||
                    document.documentElement.clientWidth ||
                    document.body.clientWidth;
        var height = window.innerHeight ||
                     document.documentElement.clientHeight ||
                     document.body.clientHeight;
    
        document.getElementById('jsWidth').innerHTML = width;  // Display the width
        document.getElementById('jsHeight').innerHTML = height;// Display the height
    };
    window.onload = jsUpdateSize;       // When the page first loads
    window.onresize = jsUpdateSize;     // When the browser changes size
    

    jQuery

    function jqUpdateSize(){
        // Get the dimensions of the viewport
        var width = $(window).width();
        var height = $(window).height();
    
        $('#jqWidth').html(width);      // Display the width
        $('#jqHeight').html(height);    // Display the height
    };
    $(document).ready(jqUpdateSize);    // When the page first loads
    $(window).resize(jqUpdateSize);     // When the browser changes size
    

    jsfiddle demo

    Edit: Updated the JavaScript code to support IE8 and earlier.

    0 讨论(0)
  • 2020-12-02 11:40

    use width and height variable anywhere you want... when ever browser size change it will change variable value too..

    $(window).resize(function() {
        width = $(this).width());
        height = $(this).height());
    });
    
    0 讨论(0)
提交回复
热议问题