1
1

More than 3 years have passed since last update.

レスポンシブサイトでデバイスサイズごとにJSを切り替える。

Last updated at Posted at 2020-04-19

PCとSPでデザインが違う時に、使用したので忘れずにメモ。

script.js
jQuery(document).ready(function($) {
    if (window.matchMedia( '(min-width: 768px)' ).matches) {
      $(function(){
          /* ウィンドウサイズが768px以上の場合 */
      });
    } else {
      $(function(){
          /* ウィンドウサイズが768px未満の場合 */
      });
    };
});

タブレットの時とかにも使える◎

script.js
if (window.matchMedia( "(max-width: 768px) and (min-width: 480px)" ).matches) {
  /* ウィンドウサイズが 480px以上768px以下の場合 */
}

追記

プラグインを見つけたので、追記。

enquire.js
http://www.nkjmkzk.net/?tag=enquire-js

1
1
1

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
1
1