Media Query to differentiate Google Pixel 1/2 vs Google Pixel XL vs Google Pixel 2 XL

╄→尐↘猪︶ㄣ 提交于 2019-12-24 05:43:13

问题


Im writing a cordova app and need to isolate these google phones to adjust styling

given this:

Im struggling to differentiate between any of the Google Pixel Phones.

@media only screen and (min-width: 411px) and (max-width: 731px) {
    .myDiv{ background-color: blue; }
}

this triggers on all Pixels - both in emulators and physical devices

@media only screen and (min-width: 411px) and (-webkit-device-pixel-ratio: 3) {
    .myDiv{ background-color: blue; }
}

this does not trigger at all with any pixel phone - dosnt matter if 3 or 4 pixel ratio

@media screen and (device-width: 411px) and (device-height: 731px) and (orientation: portrait) and (-webkit-device-pixel-ratio: 2.6){
   .myDiv{ background-color: blue;
}

this also does not trigger on any pixel phone

im struggling to find even ONE media query that works for isolating the Google Pixel 2 XL - nothing seems to have been posted - but the phone has been out for a while?

Has anyone had any luck with this?


回答1:


You can get a devices width, height, and pixel aspect ratio using Javascript.

Output Device Information Function

function OutputDeviceInformation() {
  let deviceWidth = window.screen.width;
  let deviceHeight = window.screen.height;
  let devicePixelAspectRation = window.devicePixelRatio;

  console.log('Width: ' + deviceWidth);
  console.log('Height: ' + deviceHeight);
  console.log('Pixel Aspect Ratio: ' + devicePixelAspectRatio);
}

Google Pixel 2 XL Output:

Width: 412px

Height: 823px

Pixel Aspect Ratio: 3.5

Media Query to target Google Pixel 2 XL

/* Portrait */
@media screen 
  and (device-width: 412px) 
  and (device-height: 823px) 
  and (-webkit-device-pixel-ratio: 3.5) 
  and (orientation: portrait) {

}

/* Landscape */
@media screen 
  and (device-width: 412px) 
  and (device-height: 823px) 
  and (-webkit-device-pixel-ratio: 3.5) 
  and (orientation: landscape) {

}

/* Target Portrait and Landscape */
@media screen 
  and (device-width: 412px) 
  and (device-height: 823px) 
  and (-webkit-device-pixel-ratio: 3.5) 
  and (orientation: landscape) {

}



回答2:


Try this for Google Pixel XL :

/*Google Pixel XL */ '@media screen and (device-width: 411px) and (device-height: 823px){

}'



来源:https://stackoverflow.com/questions/49078882/media-query-to-differentiate-google-pixel-1-2-vs-google-pixel-xl-vs-google-pixel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!