Mobile web: how to get physical pixel size?

前端 未结 4 1251
暖寄归人
暖寄归人 2020-12-01 16:31

I am creating a web app using jQuery Mobile and PhoneGap. There is a graph and if the user is using a large screen, I would like to display more points on this graph, becaus

4条回答
  •  情深已故
    2020-12-01 17:24

    What you want is to check the device's pixel density - measured in DPI - as @Smamatti already mentioned you control this with CSS media queries.

    Here's an article on how to cope with varying DPIs and screen sizes, using those CSS media queries.

    UPDATE: here's the javascript function (from the above link) that uses a trick to figure out the current device DPI:

    function getPPI(){
     // create an empty element
     var div = document.createElement("div");
     // give it an absolute size of one inch
     div.style.width="1in";
     // append it to the body
     var body = document.getElementsByTagName("body")[0];
     body.appendChild(div);
     // read the computed width
     var ppi = document.defaultView.getComputedStyle(div, null).getPropertyValue('width');
     // remove it again
     body.removeChild(div);
     // and return the value
     return parseFloat(ppi);
    }
    

    Hope this helps!

提交回复
热议问题