LoginSignup
27
30

More than 5 years have passed since last update.

レスポンシブデザインでも便利な Modernizr

Last updated at Posted at 2015-04-24

JavaScript

ブラウザ幅で分岐

Bootstrap参考:
xs: 0, sm: 576px, md: 768px,
lg: 992px, xl: 1200px

const mq = [];
mq.mobile  = Modernizr.mq('(max-width: 575px)');
mq.tablet  = Modernizr.mq('(min-width: 576px) and (max-width: 991px)');
mq.desktop = Modernizr.mq('(min-width: 992px)');

if ( mq.mobile ) { モバイル表示のみ処理させたい処理 }
if ( mq.desktop ) { デスクトップ表示のみ処理させたい処理 }

タッチデバイスなら touchイベント、以外は clickイベント

clickイベントはタッチデバイスでも有効だが、touchイベントの方が圧倒的にレスポンス良いのでそちらを使う

var clickEvent = Modernizr.touch ? 'touchend' : 'click';

$('#hoge').on(clickEvent, function () {
  //処理
});

ブラウザが特定のCSS3プロパティやHTML5の機能に対応していたら処理

例) Transitions (CSS3アニメーション) 対応ブラウザのみ実行

if(Modernizr.csstransitions) {
  //処理
}
// 一例
// audio, applicationcache, backgroundsize, borderimage, borderradius,
// boxshadow, canvas, canvastext, cssanimations, csscolumns,
// cssgradients, cssreflections, csstransforms, csstransforms3d,
// csstransitions, draganddrop, flexbox, fontface, geolocation,
// hashchange, history, hsla, indexeddb, inlinesvg, localstorage,
// multiplebgs, opacity, postmessage, rgba, sessionstorage, smil,
// svg, svgclippaths, textshadow, touch, video, webgl, websqldatabase,
// websockets, webworkers

» Modernizr JS object property 一覧 (公式サイト)


CSS/Sass

後で追加

27
30
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
27
30