How to programmatically find the device-width in Phonegap/JQuery mobile

前端 未结 2 1958
终归单人心
终归单人心 2020-12-12 23:03

I need to find the device-width of the mobile device. Although we can specify the content



        
2条回答
  •  孤街浪徒
    2020-12-12 23:28

    The viewport dimensions can be gathered via the window object:

    var viewport = {
        width  : $(window).width(),
        height : $(window).height()
    };
    
    //can access dimensions like this:
    //viewport.height
    

    Though you won't always get perfect results, different devices behave differently and this gives the viewport dimensions, not the screen dimensions.

    Alternatively you could check the width of a data-role="page" element to find the device-width (since it's set to 100% of the device-width):

    var deviceWidth = 0;
    $(window).bind('resize', function () {
        deviceWidth = $('[data-role="page"]').first().width();
    }).trigger('resize');​​​
    

提交回复
热议问题