LoginSignup
4
4

More than 5 years have passed since last update.

レスポンシブ対応でカオスになった時

Last updated at Posted at 2016-04-13

javascriptでアクセスしてきた端末をpc/spに振り分けて、bodyにクラスを追加

javascript
if ((navigator.userAgent.indexOf('iPhone') > 0 && navigator.userAgent.indexOf('iPad') == -1) || navigator.userAgent.indexOf('iPod') > 0 || navigator.userAgent.indexOf('Android') > 0) {
    $('body').addClass('env-sp');
} else {
    $('body').addClass('env-pc');
}

たとえばtopページなら・・・

top.scss
/*
共通部分
画像、変数、mixinなど
*/

@import 'pages/top-pc', 'pages/top-sp';

top-pc.scss
/* ======================================
 * pc
 * ====================================== */
.env-pc{

}

top-sp.scss
/* ======================================
 * sp
 * ====================================== */
.env-sp{

}

もしくはpc.cssとsp.cssを用意しておいて、jsでlinkタグを生成する。

var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text.css';
link.href = 'css/pc.css';
document.getElementsByTagName('head')[0].appendChild(link);

domは用意してあるけどspでは表示しない場合

.env-sp{
    .sp-none{
        display: none;
    }

}

参考
端末判定
http://html5-css3.jp/smartphone/pc-iphone-android-php-javascript-htaccess.html

spサイト基本知識

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4