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 );

そこにある解決策のほとんどは、未知のブラウザでは失敗します。たとえば、ブラウザがモバイルであるかどうかのみを検出し、それ以外の場合はデスクトップであると見なします。デスクトップブラウザであることが証明されない限り、私はモバイルを想定する必要があります。

PS、代わりにソリューションを提供したい場合は、PHPも使用しています。

4

2 に答える 2

1

メディアクエリを使用してみてください

/* 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 );

編集:

この関数を使用してみることができます:

if(jQuery.browser.mobile)
{
   console.log('You are using a mobile device!');
}
else
{
   console.log('You are not using a mobile device!');
}
于 2013-03-18T20:08:59.610 に答える
-1

CSSで少し作業するだけでクエリメディアを使用できます。HTMLについては、デスクトップ/モバイルのクエリメディアに関連する非表示/表示クラス属性を持つ2つのフォームオプションを挿入できます。

于 2013-03-18T20:05:30.660 に答える