I am currently developing a gallery feature where I want to display portrait-oriented images to mobile phone users and landscape-oriented images to other devices like tablets and laptops.
Here is what I have so far:
var maxW = window.screen.availWidth * window.devicePixelRatio;
if (maxW <= 767){
galleryImages = portraitImages;
}else{
galleryImages = landscapeImages;
};
var portraitImages = [ 'https://static.photocdn.pt/images/articles/2016-7/landscape-comp/iStock_000058793850_Medium.jpg',
'https://keyassets.timeincuk.net/inspirewp/live/wp-content/uploads/sites/12/2016/05/AP_Portrait_Landscapes_Stu_Meech-5.jpg',
'https://keyassets.timeincuk.net/inspirewp/live/wp-content/uploads/sites/12/2016/05/AP_Portrait_Landscapes_Stu_Meech-16.jpg',
'https://static.photocdn.pt/images/articles/2016-7/landscape-comp/iStock_000062009934_Medium.jpg']
var landscapeImages = ['https://images.unsplash.com/photo-1506744038136-46273834b3fb?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjM3Njd9&w=1000&q=80',
'https://images.unsplash.com/photo-1506260408121-e353d10b87c7?ixlib=rb-1.2.1&w=1000&q=80',
'https://www.tom-archer.com/wp-content/uploads/2017/03/landscape-photography-tom-archer-4.jpg',
'https://s.yimg.com/uu/api/res/1.2/DdytqdFTgtQuxVrHLDdmjQ--~B/aD03MTY7dz0xMDgwO3NtPTE7YXBwaWQ9eXRhY2h5b24-/https://media-mbst-pub-ue1.s3.amazonaws.com/creatr-uploaded-images/2019-11/7b5b5330-112b-11ea-a77f-7c019be7ecae',
'https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/hbx030117buzz14-1550595844.jpg']
Now, assuming the smallest resolution for tablets is 768x1024. When I input 768x1024 in the Dev Tools resolution and run
alert("MaxW: " + maxW + ",\nMaxH : " + maxH);
, I receive MaxW: 1536, MaxH: 2048 due to the device-pixel ratio.
My question is, how can I accurately calculate the maximum screen size for a mobile phone, taking into account the device-pixel ratio?
I cannot simply use "show picture gallery if max-device width is 2048" because some computer monitors have lower resolutions.
I hope I have explained my query clearly. Please let me know if you need any more information or clarification.
Thank you for your assistance.