-
-
Save r-abe01/2b1ff82e540466e55cc5 to your computer and use it in GitHub Desktop.
Media Queries(メディアクエリ)とJavaScriptの連携について ref: http://qiita.com/r_abe01/items/a3a2e94b5162d949037b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- ページ内のどこかに空タグで良いので配置しておく --> | |
<div class="jsc-media-queries media-queries"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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