按下按钮时创建一个完整的高度和宽度菜单栏

时间:2017-09-28 12:35:40

标签: javascript jquery html css width

我想按下右上角的菜单按钮,从左到右全屏滑动(全高和宽),其他一切都消失了。 这里的问题是,如果向下滚动,然后单击菜单按钮,它不会显示它只显示在顶部。 还有我不太了解的东西,高度和宽度。如果我想要覆盖整个页面的东西或只有我们可以看到的高度和宽度我们如何做这样的事情?为此,我将100%的高度和宽度放在页面的可见部分,如果我们想要覆盖整个页面,该怎么办? 高度和宽度如何工作?



$("#side-button").click(function(){
  if ($("#side-bar").css("display") == "block"){
    $("body").css("overflow","auto");
    $("#side-bar").animate({width: "0%"},400,function(){
        $("#side-bar").css("display","none");
    });
  }
  else{
    $("#side-bar").css("display","block").animate({width: "100%"},400,function(){
      $("body").css("overflow","hidden");
    });
  }
});

html{
  height:100%;
  width:100%;
}
body{
  position: relative;
  height:100%;
  width:100%;
}

#side-bar{
  position: absolute;
  width:0%;
  background: black;
  height: 100%;
  z-index: 100;
  display:none;
}

#side-button{
  display:flex;
  position:fixed;
  right:10px;
  top:10px;
  z-index: 100;
  width:30px;
  height:30px;
  justify-content: space-around;
  flex-direction: column;
}

#side-button div{
  width:100%;
  height:4px;
  background-color: gray;
  display:block;
}

#side-button:hover #first{
  transform: rotate(1turn);
  transition: 1s transform;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="bootstrap/css/bootstrap.css"/>
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="bootstrap.css"/>
</head>
<body>
  <div id="side-bar">
    <div class="page-header text-center header1">
      <h1>THIS IS A HEADER</h1>
    </div>
  </div>
  <div id="side-button" >
    <div id="first"></div>
    <div id="second"></div>
    <div id="third"></div>
  </div>
<div class="container" style="padding-bottom:20px;">
  <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
    <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
    <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
    <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
  </div>
<script src="bootstrap.js"></script>
</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

侧栏有一个绝对位置,仍然是指文件。

因此,您希望将#side-bar放在文档流程之外,方法是fixed 它将在视口“上方”。

我使用顶部,左侧,底部和宽度来为其设置动画。

$("#side-button").click(function(){
  if ($("#side-bar").css("display") == "block"){
    $("body").css("overflow","auto");
    $("#side-bar").animate({left: "-100%"},400,function(){
        $("#side-bar").css("display","none");
    });
  }
  else{
    $("#side-bar").css("display","block").animate({left: "0%"},400,function(){
      $("body").css("overflow","hidden");
    });
  }
});
html{
  height:100%;
  width:100%;
}
body{
  position: relative;
  height:100%;
  width:100%;
}

#side-bar{
  position: fixed;
  top:0;
  bottom: 0;
  left:-100%;
  width: 100%;
  background: black;
  z-index: 100;
  display:none;
}

#side-button{
  display:flex;
  position:fixed;
  right:10px;
  top:10px;
  z-index: 100;
  width:30px;
  height:30px;
  justify-content: space-around;
  flex-direction: column;
}

#side-button div{
  width:100%;
  height:4px;
  background-color: gray;
  display:block;
}

#side-button:hover #first{
  transform: rotate(1turn);
  transition: 1s transform;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="bootstrap/css/bootstrap.css"/>
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="bootstrap.css"/>
</head>
<body>
  <div id="side-bar">
    <div class="page-header text-center header1">
      <h1>THIS IS A HEADER</h1>
    </div>
  </div>
  <div id="side-button" >
    <div id="first"></div>
    <div id="second"></div>
    <div id="third"></div>
  </div>
<div class="container" style="padding-bottom:20px;">
  <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
    <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
    <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
    <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
  </div>
<script src="bootstrap.js"></script>
</body>
</html>

答案 1 :(得分:0)

您可以将位置绝对从侧栏更改为固定,位置将相对于视口并将固定在顶部

&#13;
&#13;
$("#side-button").click(function(){
  if ($("#side-bar").css("display") == "block"){
    $("body").css("overflow","auto");
    $("#side-bar").animate({width: "0%"},400,function(){
        $("#side-bar").css("display","none");
    });
  }
  else{
    $("#side-bar").css("display","block").animate({width: "100%"},400,function(){
      $("body").css("overflow","hidden");
    });
  }
});
&#13;
html{
  height:100%;
  width:100%;
}
body{
  position: relative;
  height:100%;
  width:100%;
}

#side-bar{
  position: fixed;
top: 0;
  width:0%;
  background: black;
  height: 100%;
  z-index: 100;
  display:none;
}

#side-button{
  display:flex;
  position:fixed;
  right:10px;
  top:10px;
  z-index: 100;
  width:30px;
  height:30px;
  justify-content: space-around;
  flex-direction: column;
}

#side-button div{
  width:100%;
  height:4px;
  background-color: gray;
  display:block;
}

#side-button:hover #first{
  transform: rotate(1turn);
  transition: 1s transform;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="bootstrap/css/bootstrap.css"/>
  <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link rel="stylesheet" href="bootstrap.css"/>
</head>
<body>
  <div id="side-bar">
    <div class="page-header text-center header1">
      <h1>THIS IS A HEADER</h1>
    </div>
  </div>
  <div id="side-button" >
    <div id="first"></div>
    <div id="second"></div>
    <div id="third"></div>
  </div>
<div class="container" style="padding-bottom:20px;">
  <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
    <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
    <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
    <div class="jumbotron ">
    <h1>This is a jumbotron !</h1>
    <p>The jumbotron makes a big fat div :p </p>
  </div>
  </div>
<script src="bootstrap.js"></script>
</body>
</html>
&#13;
&#13;
&#13;