Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tonpoo/51763cc862d59271f6055060d177e955 to your computer and use it in GitHub Desktop.
Save tonpoo/51763cc862d59271f6055060d177e955 to your computer and use it in GitHub Desktop.
画面の横幅に応じてmeta viewportの値を動的に変更するjs(要jquery)
/**
* 画面の横幅に応じてmeta viewportの値を動的に変更するjs(要jquery)
*
* window.screen.widthの値を見て$("meta[name='viewport']").attr("content", "xxxxxxxxxx")で設定
*/
$(window).on("resize orientationchange", function (e) {
var wsw = window.screen.width;
if (wsw <= 640) {
//デバイス横幅640以下
$("meta[name='viewport']").attr("content", "width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,shrink-to-fit=no");
} else if (wsw < 980) {
//デバイス横幅980より下
$("meta[name='viewport']").attr("content", "width=980px,user-scalable=0,shrink-to-fit=no");
} else {
//それ以外
$("meta[name='viewport']").attr("content", "width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,shrink-to-fit=no");
}
}).trigger("resize");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment