What is the best way to detect retina support on a device using JavaScript?

后端 未结 4 736
时光取名叫无心
时光取名叫无心 2020-11-28 04:58

Right now I am using this function:

function is_retina_device() {
    return window.devicePixelRatio > 1;
}

But its simplicity scares me

4条回答
  •  抹茶落季
    2020-11-28 05:13

    If you want it for images you can use retinajs or this code is a common response to detect it:

    function isRetinaDisplay() {
            if (window.matchMedia) {
                var mq = window.matchMedia("only screen and (min--moz-device-pixel-ratio: 1.3), only screen and (-o-min-device-pixel-ratio: 2.6/2), only screen and (-webkit-min-device-pixel-ratio: 1.3), only screen  and (min-device-pixel-ratio: 1.3), only screen and (min-resolution: 1.3dppx)");
                return (mq && mq.matches || (window.devicePixelRatio > 1)); 
            }
        }
    

提交回复
热议问题