JSを読み込みます
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="https://codingmania.net/demo/form/0014/js/main.js"></script>
[main.js]JavaScript
$(function(){
//メニュー
//SET
if($(".Header__btn").css("display") == "block") {
windowWidth = $(window).width();
$(".Globalnav").css({"margin-left":windowWidth, "width":windowWidth,"display":"block"});
}
//risize
var setwidth = $(window).width();
var timer = false;
$(window).resize(function() {
if (timer !== false) {
clearTimeout(timer);
}
timer = setTimeout(function() {
var ww = $(window).width();
windowWidth = $(window).width();
if(windowWidth == setwidth){
}else if(windowWidth<640){
$(".Header__btn p").addClass("close").removeClass("open");
$(".Header__btn p img").attr("src","common/img/icon_menu.png");
$(".Globalnav").css({"margin-left":windowWidth, "width":windowWidth,"display":"block"});
}else{
$(".Globalnav").css({"margin":"0 auto", "width":950});
};
}, 50);
});
//orientationchange
$(function(){
var timer = false;
//$(window).on("orientationchange", function() {
$(window).on("orientationchange", function() {
if(timer !== false){
clearTimeout(timer);
}
timer = setTimeout(function() {
//window.onorientationchange = function(){
windowWidth = $(window).width();
//alert(windowWidth);
if(windowWidth<640){
$(".Header__btn p").addClass("close").removeClass("open");
$(".Header__btn p img").attr("src","common/img/icon_menu.png");
$(".Globalnav").css({"margin-left":windowWidth, "width":windowWidth,"display":"block"});
}else{
$(".Globalnav").css({"margin":"0 auto", "width":950});
}
}, 1);
});
});
//click
$(".Header__btn p").click(function() {
if($(this).hasClass("open")) {
$(this).addClass("close").removeClass("open");
//var windowWidth = $(window).width();
$(".Header__btn p img").attr("src","common/img/icon_menu.png");
$(".Globalnav").animate({"marginLeft":windowWidth+"px"},500);
//alert(windowWidth);
} else {
$(this).addClass("open").removeClass("close");
$(".Header__btn p img").attr("src","common/img/icon_menu-close.png");
$(".Globalnav").animate({"marginLeft":"0px"},500);
}
});
});
HTMl
<header class="Header" role="banner">
<div class="Header__btn">
<p><a href="javascript:void(0)"><span>メニューを開く</span></a></p>
<!-- /.Header__btn --></div>
<nav class="Globalnav" role="navigation" aria-label="メインメニュー">
<ul>
<li class="current"><a href="#">TOPページ</a></li>
<li><a href="#">メニューその1</a></li>
<li><a href="#">メニューその2</a></li>
<li><a href="#">メニューその3</a></li>
<li><a href="#">メニューその4</a></li>
<li><a href="#">メニューその5</a></li>
</ul>
<!-- /.Globalnav --></nav>
<!-- /.Header --></header>
CSS
/* Header
-----------------------------------------*/
@media screen and (max-width: 640px) {
.Header{
position: relative;
height: 50px;
}
}
.Header__btn{
display: none;
}
@media screen and (max-width: 640px) {
.Header__btn{
position: absolute;
top:0px;
right: 0px;
display: block;
background-color: #489fdf;
}
.Header__btn p a{
display: block;
width: 50px;
height: 50px;
}
.Header__btn p a:after {
position: absolute;
display: block;
content: " \f0c9";
font-family: FontAwesome;
width: 50px;
height: 50px;
top: 14px;
left:0;
right: 0;
font-size: 22px;
color: #FFF;
text-align: center;
}
.Header__btn p.open a:after {
content: " \f00d";
}
.Header__btn span{
display: block;
width: 1px;
height: 1px;
overflow: hidden;
}
}
/* Globalnav
-----------------------------------------*/
@media screen and (max-width: 640px) {
body {
overflow: hidden;
}
}
.Globalnav {
margin: 0 auto;
width: 950px;
}
@media screen and (max-width: 640px) {
.Globalnav {
width: auto;
}
}
@media screen and (max-width: 640px) {
.Globalnav {
display: none;
position: absolute;
top: 50px;
left: 0;
margin-left: 320px;
width: 320px;
z-index: 9999;
background-color: #489fdf;
}
}
.Globalnav ul {
display: table;
width: 100%;
}
@media screen and (max-width: 640px) {
.Globalnav ul {
display:block;
width: 100%;
box-sizing: border-box;
padding: 15px 10px;
}
}
.Globalnav ul li {
display: table-cell;
}
@media screen and (max-width: 640px) {
.Globalnav ul li {
display: block;
margin: 0;
border-bottom: solid 1px #FFF;
}
.Globalnav ul li:last-child{
border-bottom: none;
}
}
.Globalnav ul li a {
display: block;
text-align: center;
padding: 20px 0;
color: #000;
font-size: 16px;
font-weight: bold;
text-decoration: none;
border-bottom: solid 3px #FFF;
}
@media screen and (max-width: 640px) {
.Globalnav ul li a {
position: relative;
padding: 15px 0;
font-size: 14px;
border: none;
color: #FFF;
text-align: left;
}
}
@media screen and (max-width: 640px) {
.Globalnav ul li a:after {
position: absolute;
content: " \f105";
font-family: FontAwesome;
width: 20px;
height: 7px;
top: 50%;
margin-top: -10px;
right: 0;
}
}
.Globalnav ul li a:hover, .Globalnav ul li a:focus, .Globalnav ul li.current a {
border-bottom: solid 3px #489fdf;
color: #489fdf;
}
@media screen and (max-width: 640px) {
.Globalnav ul li a:hover, .Globalnav ul li a:focus, .Globalnav ul li.current a {
border-bottom: none;
color: #FFF;
}
}