如何创建响应式侧面菜单布局

时间:2016-02-24 22:22:58

标签: html css responsive-design sidebar

如果你没有看到全屏gif作为背景,这里是网站的链接(http://pavilioncreative.com/)点击刷新。

无论屏幕的大小如何,我都试图将红色框放在中间位置。问题是侧边菜单的位置是固定的,因此主要内容div在侧面菜单下在屏幕上完全伸展100%。

我想我可能会把这一切都搞错了?

    <html lang="en">

    <head>

    <title>FirstPage</title>

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="css/reset.css" >
    <link rel="stylesheet" href="css/text.css" >
    <link rel="stylesheet" href="css/style.css">
    <link href='https://fonts.googleapis.com/css?family=Lora:400,700' rel='stylesheet' type='text/css'>


    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>

    </head>

    <body>

<div class="wraper">

        <ul>

    <li class="menu" >

            <div class="menu_tab_wrap">

     <div class="menu_tab">

         <div class="menu_off_wrap">

         <div class="menu_off">

             <div class="barOne"></div>
             <div class="spacer"></div>
             <div class="barTwo"></div>
             <div class="spacer"></div>
             <div class="barThree"></div>

         </div>



        </div>

     </div> 

     </div>

     <div class="menu_tab_wrap_on">

     <div class="menu_tab_on">

         <div class="menu_on_wrap">

         <div class="menu_on">

             <div class="cross"></div>


         </div>



        </div>

     </div> 

     </div>

     <div class="logo_wrap">

            <div class="logo">

                <img src="img/logo.svg">

            </div>

         </div>


            </li>

              <section id="menu_out">

         <div class="menu_inner_wrap">

         <div class="menu_list">

         <ul class="menu_ul">

             <li class="menu_li"> <a href="#">Home</a> </li>
             <span class="in_lable">Back to the home page</span>
             <li class="menu_li"> <a href="#">About</a> </li>
             <span class="in_lable">Find out more about me</span>
             <li class="menu_li"> <a href="#">Portfolio</a> </li>
             <span class="in_lable">Take a look at my past work</span>
             <li class="menu_li"> <a href="#">Contact Me</a> </li>
             <span class="in_lable">Get in contact with me</span>

        </ul>

         </div>



             </div>

     </section>

    <li class="content">
        <div class="content_wrap">

            <h1>test</h1>
            </div>
            </li>


    </ul>



        </div>


        <script type="text/javascript">



        $(document).ready(function () {

    var hoverIn = false; //You need this counter to detect whether animate occurs.
$(".menu_tab").hover(function() {
    if (hoverIn)return; //if the hover is activated, it stops the function

  //I also took the liberty to help you add stop to prevent multiple hover. Feel free to implement that else where
  $(".barOne").stop(true, true).animate({
    "bottom": "+=5px"
  }, "fast");
  $(".barThree").stop(true, true).animate({
    "top": "+=5px"
  }, "fast");
  hoverIn = true;
}, function() {
    if (!hoverIn)return; //if the hover is deactivated, it stops this function
  $(".barOne").stop(true, true).animate({
    "bottom": "-=5px"
  }, 300);
  $(".barThree").stop(true, true).animate({
    "top": "-=5px"
  }, 300);
  hoverIn = false;
});

            });











       $(document).ready(function () {

        if($(window).width() > 700) {
             $(".menu_tab").click(function(){
             $("#menu_out").animate({"width": "30em"}, "slow");
             $(".menu_tab_wrap_on").stop().fadeIn();
             $(".menu_list").stop().delay( 400 ).fadeIn('slow');

            });

        }else{

             $(".menu_tab").click(function(){
             $("#menu_out").animate({"width": "100%"}, "slow");
             $(".menu_tab_wrap_on").stop().fadeIn();
             $(".menu_list").stop().delay( 400 ).fadeIn('slow');


            });

        }

                });


       $(document).ready(function () {

             $(".menu_tab_wrap_on").click(function(){
             $(".menu_tab_wrap_on").stop().hide('fast');
             $("#menu_out").animate({"width": "0em"}, "slow");
             $(".menu_list").stop().hide();
            });

                });











   </script>

    </body>


</html>




html{

    -webkit-text-size-adjust: 100%; /* Prevent font scaling in landscape */

}

.wraper{

    width: 100%;
    margin: auto;
    padding: 0;

}


.wraper ul{

    width: 100%;
    margin: 0 auto;
    padding: 0;

}

.wraper ul li{

    padding: 0;
    margin: 0 auto;
    display: inline-block;

}

.wraper ul .menu{

    width: 7%;
    min-width: 7em;
    max-width: 7em;
    background: black;
    height: 100%;
    position: fixed;
    z-index: 20;
    float: left;


}


.wraper ul .content{

    background-color: blue;
    width: 93%;
    height: 70em;
    float: right;

}

.wraper ul .content .content_wrap{

    width: 80%;
    margin: auto;
    text-align: center;
    border: red solid 1px;


}





























.menu .menu_tab_wrap{

    position: relative;

}

.menu .menu_tab{

    width: 100%;
    background-color: #232323;
    height: 6em;
    display: table;
    position: absolute;
    width: 100%;
    cursor: pointer;
}



.menu .menu_tab .menu_off_wrap {
    width: 100%;
    height: 100%;
    display: table-cell;
    vertical-align: middle;



}

.menu .menu_tab .menu_off_wrap .menu_off{

   margin-left: auto;
    margin-right: auto; 
    position: relative;



}





.menu .menu_tab .menu_off_wrap .menu_off .barOne,.barTwo,.barThree{

    width: 3em;
  padding: 2px;
  background-color: white;
    margin: auto;
    position: relative;


}

.menu .menu_tab .menu_off_wrap .menu_off .spacer{

    width: 5em;
    height: 5px;


}



.menu .menu_tab_wrap_on{

    position: relative;
    display: none;





}

.menu .menu_tab_wrap_on .menu_tab_on{

    width: 100%;
    background-color: white;
    height: 6em;
    display: table;
    position: absolute;
    width: 100%;
    cursor: pointer;
}



.menu .menu_tab_wrap_on .menu_tab_on .menu_on_wrap {
    width: 100%;
    height: 100%;
    display: table-cell;
    vertical-align: middle;



}

.menu .menu_tab_on .menu_on_wrap .menu_on{

   margin-left: auto;
    margin-right: auto; 
    position: relative;
    text-align: center;



}





.menu .menu_tab_on .menu_on_wrap .menu_on .cross{


  width: 3em;
  height: 3em;

  color: #232323;
  margin: auto;
  position: relative;

}




.cross:before, .cross:after {
  position: absolute;

  content: ' ';
  height: 3em;
  width: 4px;
  background-color: #333;
}
.cross:before {
  transform: rotate(45deg);
}
.cross:after {
  transform: rotate(-45deg);
}



.menu .logo_wrap{

    width: 100%;
    text-align: center;


}

.menu .logo_wrap .logo{

    width: 7em;
   margin: auto;
    position:absolute;
   bottom:0;
    padding-bottom: 2em;

}

.menu .logo_wrap .logo img{

    width: 70%;
    height: auto;

}
















#menu_out{

    background-color: #232323;
    height: 100%;
    width: 0em;
    position: fixed;
    z-index: -1;

    overflow-y:scroll;
    overflow-x:hidden;
    border-right: 0.5em black solid;
    z-index: 10;


}


#menu_out .menu_inner_wrap{

    position: relative;



}


#menu_out .menu_list{

    width: 70%;
    height: auto;
    margin: auto;
    padding-top: 5em;
    padding-bottom: 0em;
    display: none;
    position: relative;


}


#menu_out .menu_list .menu_ul{

    padding: 0;
    margin: 0 auto;
    width: 100%;
    padding-left: 4em;
    padding-bottom: 2em;



}

#menu_out .menu_list .menu_ul .menu_li{

    display: block;
    padding: 0;
    margin: 0 auto;


}

#menu_out .menu_list .menu_ul .menu_li a{

    font-size: 40px;
    color: white;
    text-decoration: none;
    font-family: 'Lora', serif;
    font-weight: 700;
    opacity: 0.8;


}


#menu_out .menu_list .menu_ul .menu_li a:hover{

    opacity: 1;

}


#menu_out .menu_list .menu_ul .in_lable{

    font-size: 15px;
    color: #80E577;
    font-weight: 100;
    font-family: 'Open Sans', sans-serif;
    position: relative;
    bottom: 10px;;


}


#menu_out .menu_inner_wrap .footer{

    width: 100%;

    margin: auto;

    position:absolute;
   bottom:0;
    height: 10em;

}












































@media only screen and (max-width: 800px) {

    .wraper ul .content{

    background-color: blue;
    width: 90%;
    height: 70em;
    float: right;

}


}




@media only screen and (max-width: 700px) {

  .wraper ul .content{

    background-color: blue;
    width: 100%;
    height: 70em;
    float: none;

}

    .wraper ul .menu{

    width: 100%;
    min-width: none;
    max-width: none;
    height: 5em;



}


    .menu .menu_tab{


    height: 100%;
    display: table;
    position: absolute;
    width: 6em;
    cursor: pointer;
        right: 0;
}



    .menu .logo_wrap{

    width: 100%;
    text-align: center;



}

.menu .logo_wrap .logo{

    width: 7em;
   margin: auto;
    position:absolute;
   bottom:0;

    padding-top: 1em;
        padding-bottom: 1em;

}


    .menu .menu_tab_wrap_on .menu_tab_on{

    width: 6em;

    height: 100%;

    right: 0;
    }



    #menu_out .menu_list{

    width: 100%;
    height: auto;
    margin: auto;
    padding-top: 8em;
    text-align: center;



}

    #menu_out .menu_list .menu_ul{

    padding: 0;
    margin: 0 auto;
    width: 100%;
    padding-left: 0em;
        text-align: center;

}


    #menu_out .menu_list .menu_ul .menu_li a{

    font-size: 35px;
    color: white;
    text-decoration: none;
    font-family: 'Lora', serif;
    font-weight: 700;

}


#menu_out .menu_list .menu_ul .menu_li a .in_lable{

    font-size: 10px;
    color: #80E577;
    font-family: 'Open Sans', sans-serif;
    font-weight: 100;

}


    #menu_out{

       width: 0; 
        border-right: none;

    }



}

1 个答案:

答案 0 :(得分:0)

看看你的CSS,我看到了:

#content_wrap .content {
    max-width: 1050px;
    width: 80%;
    margin: auto;
    height: 700em;
     border: red solid 1px;
    margin-right: 7%;
    margin-left: 13%;
}

如果你删除左边距和右边距,它能完成你所追求的吗?

示例:

#content_wrap .content {
    max-width: 1050px;
    width: 80%;
    margin: auto;
    height: 700em;
     border: red solid 1px;
}

(当您移除margin-leftmargin-right时,margin: auto会将自动应用于上边距,右边,下边和左边。)

更新2/25/2016:

按照上述步骤操作后,您就可以完成任务了。但是,如果您希望内容在左侧扩展菜单时更改其位置(或重新居中),则需要更多CSS和一些JavaScript来添加和删除类。 注意:上述CSS更改对于此工作是必要的

#content_wrap{
   //This should be the exact same with as the menu when it's not expanded out
    padding-left: 7em;
    //Set this to the same amount of time it takes for the menu to expand
    //This will animate the effect using CSS
    transition: 0.5s; 
}

#content_wrap.menu_showing {
   //This should be the exact same with as the menu when it's expanded out
    padding-left: 30em
}

现在,只需点击“展开菜单”图标,即可使用JavaScript切换该课程。这是一个使用jQuery的例子:

$(".menu_tab").click(function(){
    $("#content_wrap").toggleClass("menu_showing");
});