简体   繁体   English

基于屏幕尺寸的可变图像宽度/高度

[英]variable image width/height based on screen size

I'm working on a touch panel web page (for my home automation) that is going to run on both the iPad and the iPhone. 我正在开发一个可在iPad和iPhone上运行的触摸屏网页(用于我的家庭自动化)。 What I'm wondering about is the ability to dynamically set the size of my touch icons based on the screen size. 我想知道的是能够根据屏幕大小动态设置触摸图标的大小。

If I'm on the iPad, I need the icons to be 150px by 150px, but if I'm on the iPhone, I need the icons to be 75px by 75px. 如果我在iPad上,我需要150px到150px的图标,但如果我在iPhone上,我需要75px到75px的图标。

Is there a way to dynamically set the image sizes using jQuery? 有没有办法使用jQuery动态设置图像大小?

Look in the user agent string, and then set them accordingly. 查看用户代理字符串,然后相应地设置它们。

I'd do it like... 我会这样做......

JavaScript / jQuery JavaScript / jQuery

if (navigator.userAgent.match(/iPhone/)) {
   $('body').addClass('iphone');
} else if (navigator.userAgent.match(/iPad/)) {
   $('body').addClass('ipad');
}

If you are using jQuery only for this, you could cut down on some bloat by replacing those addClass() lines with... 如果你只是为此使用jQuery,你可以通过用...替换那些addClass()行来减少一些膨胀。

var body = document.getElementsByTagName('body')[0];
body.className = body.className + ' iphone';

...or to make sure it always looks pretty in the source... ......或者确保它在源头看起来很漂亮......

var body = document.getElementsByTagName('body')[0];
var bodyClasses = body.className.split(' ');
bodyClasses.push('iphone');
body.className = bodyClasses.join(' ');

Check it out . 看看吧

CSS CSS

.ipad #nav button {
   width: 150px;
   height: 150px;
}

.iphone #nav button {
   width: 75px;
   height: 75px;
}

This is assuming your buttons will scale by default in the browser - if you have two different images, you could change the background-image CSS property. 这假设您的按钮默认会在浏览器中缩放 - 如果您有两个不同的图像,则可以更改background-image CSS属性。

您可以检查字符串iPadiPhone是否出现在navigator.userAgent ,并相应地设置图像URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM