Skip to content

Instantly share code, notes, and snippets.

@r-abe01
Created February 15, 2016 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-abe01/2b1ff82e540466e55cc5 to your computer and use it in GitHub Desktop.
Save r-abe01/2b1ff82e540466e55cc5 to your computer and use it in GitHub Desktop.
Media Queries(メディアクエリ)とJavaScriptの連携について ref: http://qiita.com/r_abe01/items/a3a2e94b5162d949037b
/* PC用 */
.media-queries {
display: none;
font-family: 'pc';
}
/* TAB用 */
@media screen and (max-width: 768px) {
.media-queries {
font-family: 'tab';
}
}
/* SP用 */
@media screen and (max-width: 480px) {
.media-queries {
font-family: 'sp';
}
}
<!-- ページ内のどこかに空タグで良いので配置しておく -->
<div class="jsc-media-queries media-queries"></div>
var $win = $(window),
$mediaQueries = $('.jsc-media-queries');
$win.on('load resize', function() {
// モダンブラウザでは「pc」という文字列が取得できるが、
// IEでは「"pc"」と「"」が付与されて取得されてしまうため「"」を除去しつつ取得する。
var layout = $mediaQueries.css('font-family').replace(/"/g, '');
if (layout === 'pc') {
// PCの処理
} else if (layout === 'tab') {
// TABの処理
} else {
// SPの処理
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment