2013-03-18 76 views
0

我有一个表单根据窗口大小做出响应地收缩。但是,我们需要它永久保持较小的移动和其他未知浏览器。基于移动或其他未知浏览器的条件

这是伪代码。 jQuery是罚款,也和我使用v.1.8

if (is_desktop) 
    is_narrow = false; 
else 
    // applies to mobile and unknown browsers 
    is_narrow = true; 

$("#form").toggleClass('narrow', is_narrow); 

大部分的解决方案对未知的浏览器在那里失败。例如,他们只检测浏览器是否是移动的,并且假设它是桌面的。除非被证明是桌面浏览器,否则我需要假设移动设备。

P.S.,我也使用PHP,如果你想提供一个解决方案,而不是。

+2

为什么不坚持使用响应式设计呢? http://stackoverflow.com/questions/6370690/media-queries-how-to-target-desktop-tablet-and-mobile – Tchoupi 2013-03-18 20:04:33

+0

问题是,我们有一个小部件在别人的网站上。而我们需要移动设备上的缩小屏幕来缩小屏幕。 由于窗口的分辨率报告为大于实际的屏幕尺寸,我们必须用其他方法检测它。 – redolent 2013-03-18 20:07:16

+0

使用基于页面尺寸的媒体查询怎么样? – 2013-03-18 20:09:30

回答

-1

您可以使用查询介质在CSS上做一些工作。 关于HTML,您可以插入带有隐藏/可见类属性的两个表单选项,这些属性与用于桌面/移动设备的某些查询媒体相关。

+1

这是一个非常差的答案。包括链接,支持你的答案的例子,并且请,这不需要很长时间来拼写'你'! – Tchoupi 2013-03-18 20:28:32

+0

只是供参考,这里的贡献是相当高的标准,而不符合他们的答案往往被低估或删除。请阅读http://stackoverflow.com/questions/how-to-answer,了解社区在答案的内容和风格方面的用处。 – toxotes 2013-03-18 20:36:31

0

尝试使用Media Queries

/* Smartphones (portrait and landscape) ----------- */ 
@media only screen 
and (min-device-width : 320px) 
and (max-device-width : 480px) { 
/* Styles */ 
} 

/* Smartphones (landscape) ----------- */ 
@media only screen 
and (min-width : 321px) { 
/* Styles */ 
} 

/* Smartphones (portrait) ----------- */ 
@media only screen 
and (max-width : 320px) { 
/* Styles */ 
} 

/* iPads (portrait and landscape) ----------- */ 
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) { 
/* Styles */ 
} 

/* iPads (landscape) ----------- */ 
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : landscape) { 
/* Styles */ 
} 

/* iPads (portrait) ----------- */ 
@media only screen 
and (min-device-width : 768px) 
and (max-device-width : 1024px) 
and (orientation : portrait) { 
/* Styles */ 
} 

/* Desktops and laptops ----------- */ 
@media only screen 
and (min-width : 1224px) { 
/* Styles */ 
} 

/* Large screens ----------- */ 
@media only screen 
and (min-width : 1824px) { 
/* Styles */ 
} 

/* iPhone 4 ----------- */ 
@media 
only screen and (-webkit-min-device-pixel-ratio : 1.5), 
only screen and (min-device-pixel-ratio : 1.5) { 
/* Styles */ 
} 

使用此功能,以确定用户在移动

function is_mobile() { 
     var agents = ['android', 'webos', 'iphone', 'ipad', 'blackberry']; 
     for(i in agents) { 
      if(navigator.userAgent.match('/'+agents[i]+'/i')) { 
       return true; 
      } 
     } 
     return false; 
    } 

if (is_mobile) 
    is_narrow = true; 
else 
    // applies to mobile and unknown browsers 
    is_narrow = false; 

$("#form").toggleClass('narrow', is_narrow); 

编辑:

您可以尝试使用此function

if(jQuery.browser.mobile) 
{ 
    console.log('You are using a mobile device!'); 
} 
else 
{ 
    console.log('You are not using a mobile device!'); 
} 
+0

但是如果我们不确定他们是否在移动设备上呢?我们如何在展示狭义形式方面犯错? – redolent 2013-03-18 20:12:56

+0

查看已更新回答 – 2013-03-18 20:18:16

+0

不幸的是,您已将逻辑从'is_desktop'更改为'is_mobile'。我基本上想检查桌面浏览器,并假设世界其他地方是移动的。 – redolent 2013-03-18 22:24:27