在鼠标单击时将侧边栏滑入视图

时间:2015-08-05 13:22:10

标签: html css sidebar

我正在尝试创建自己的可切换侧边栏。我已经完成了这个片段(按屏幕中的棕色来切换):

我希望背景具有整个屏幕的宽度,所以当我点击棕色部分时,背景是棕色的,而不是白色。

$(document).ready(function() {
  $("#rest-of-the-page").click(
    function() {
      $("#sidebar").toggleClass("hidden");

    }
  );
});
html,
body {
  height: 100%;
  margin: 0;
}
.container {
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  height: 100%;
}
#sidebar {
  position: absolute;
  height: 100%;
  width: 15%;
  background-color: rgba(27, 26, 26, .8);
  transition: all .2s linear 0s;
}
.hidden {
  margin-left: -30%;
  transition: all .2s linear 0s;
}
#logo-container {
  min-height: 150px;
  height: 30%;
  width: 100%;
  border-bottom: 3px solid #1A1A1A;
}
#logo {
  width: 100%;
  height: 100%;
  background: url(Images/logo.png) no-repeat center center;
  background-size: 75% auto;
}
.menu-options-container {
  height: 50%;
  width: 100%;
}
#menu-options-ul {
  width: 100%;
  list-style-type: none;
  padding: 0;
  margin: 0;
  text-align: center;
  height: 70%;
}
#menu-options-ul li {
  width: 100%;
  border-top: 2px solid #373737;
  border-bottom: 2px solid #1A1A1A;
  height: 3em;
  margin-top: 0;
  color: #999;
  display: table;
}
#menu-options-ul li:hover {
  background: rgba(255, 255, 255, 0.2);
}
#menu-options-ul li p {
  vertical-align: middle;
  display: table-cell;
  width: 100%;
}
#menu-options-ul li p:hover {
  color: #fff;
}
#rest-of-the-page {
  position: absolute;
  top: 0;
  left: 15%;
  width: 85%;
  height: 100%;
  transition: all .2s linear 0s;
  cursor: pointer;
  background-color: antiquewhite
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
  <div id="sidebar">
    <div id="logo-container">
      <div id="logo"></div>
    </div>
    <div class="menu-options-container">
      <ul id="menu-options-ul">
        <li>
          <p>Hola</p>
        </li>
        <li>
          <p>Adios</p>
        </li>
        <li>
          <p>Buenas</p>
        </li>
      </ul>
    </div>
  </div>
  <div id="rest-of-the-page"></div>
</div>

4 个答案:

答案 0 :(得分:1)

只需添加左侧的课程:0将其命名为&#34; fullpage &#34;

#rest-of-page

制作宽度100%
#rest-of-the-page{
    position:absolute;
    top:0;
    width:100%;
    left:15%;
    height:100%;
    transition: all .2s linear 0s;
    cursor:pointer;  
    background-color:antiquewhite
}
#rest-of-the-page.fullpage{
     left:0;
}

更新:另一件事让左边距离:-15%而不是-30%,因为边栏的宽度只有15%

.hidden{ 
    margin-left: -15%;
    transition: all .2s linear 0s; //you don't need this
}

将隐藏的溢出添加到正文

html, body {
    height: 100%;
    margin:0;
    overflow-x:hidden;
}

#rest-of-page

toggleClass全页

https://jsfiddle.net/yggt4s83/2/

答案 1 :(得分:0)

您可以将#rest-of-page元素的背景颜色添加到您的身体:

$(document).ready(function() {
  $("#rest-of-the-page").click(
    function() {
      $("#sidebar").toggleClass("hidden");

    }
  );
});
html,
body {
  height: 100%;
  margin: 0;
  background-color: antiquewhite
}
.container {
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center;
  height: 100%;
}
#sidebar {
  position: absolute;
  height: 100%;
  width: 15%;
  background-color: rgba(27, 26, 26, .8);
  transition: all .2s linear 0s;
}
.hidden {
  margin-left: -30%;
  transition: all .2s linear 0s;
}
#logo-container {
  min-height: 150px;
  height: 30%;
  width: 100%;
  border-bottom: 3px solid #1A1A1A;
}
#logo {
  width: 100%;
  height: 100%;
  background: url(Images/logo.png) no-repeat center center;
  background-size: 75% auto;
}
.menu-options-container {
  height: 50%;
  width: 100%;
}
#menu-options-ul {
  width: 100%;
  list-style-type: none;
  padding: 0;
  margin: 0;
  text-align: center;
  height: 70%;
}
#menu-options-ul li {
  width: 100%;
  border-top: 2px solid #373737;
  border-bottom: 2px solid #1A1A1A;
  height: 3em;
  margin-top: 0;
  color: #999;
  display: table;
}
#menu-options-ul li:hover {
  background: rgba(255, 255, 255, 0.2);
}
#menu-options-ul li p {
  vertical-align: middle;
  display: table-cell;
  width: 100%;
}
#menu-options-ul li p:hover {
  color: #fff;
}
#rest-of-the-page {
  position: absolute;
  top: 0;
  left: 15%;
  width: 85%;
  height: 100%;
  transition: all .2s linear 0s;
  cursor: pointer;
  background-color: antiquewhite
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="container">
  <div id="sidebar">
    <div id="logo-container">
      <div id="logo"></div>
    </div>
    <div class="menu-options-container">
      <ul id="menu-options-ul">
        <li>
          <p>Hola</p>
        </li>
        <li>
          <p>Adios</p>
        </li>
        <li>
          <p>Buenas</p>
        </li>
      </ul>
    </div>
  </div>
  <div id="rest-of-the-page"></div>
</div>

答案 2 :(得分:0)

切换菜单时,为什么不在#Or-of-page上添加或删除课程?这样,如果您有一些内容,您的内容将始终贴在页面的右侧。使用绝对位置等没有棘手的事情。

就像这样,(例如here

HTML:

<div class="container">
    <div id="sidebar">
        <div id="logo-container">
            <div id= "logo"></div>
        </div>
        <div class="menu-options-container">
            <ul id="menu-options-ul">
                <li><p>Hola</p></li>
                <li><p>Adios</p></li>
                <li><p>Buenas</p></li>
            </ul>
        </div>
    </div>
  <div id="rest-of-the-page" class="page--sided"><p>Hello</p></div>
</div>

CSS:

html, body {
    height: 100%;
    margin:0;
}
.container{
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    height: 100%;
}

#sidebar {
    position:absolute;
    height: 100%;
    width: 15%;
    background-color: rgba(27, 26, 26, .8);
    transition: all .2s linear 0s;
}

.hidden{

    margin-left: -30%;
    transition: all .2s linear 0s;
}

#logo-container {
    min-height: 150px;
    height: 30%;
    width: 100%;
    border-bottom: 3px solid #1A1A1A;
}

#logo {
    width: 100%;
    height: 100%;
    background:url(Images/logo.png) no-repeat center center;
    background-size: 75% auto;
}

.menu-options-container {
    height: 50%;
    width: 100%;
}

#menu-options-ul{
    width: 100%;
    list-style-type: none;
    padding:0; 
    margin:0;
    text-align: center;
    height:70%;
}

#menu-options-ul li{

    width: 100%;
    border-top: 2px solid #373737;
    border-bottom: 2px solid #1A1A1A;
    height:3em;
    margin-top:0;
    color: #999;
    display: table;
}

#menu-options-ul li:hover{
    background: rgba(255,255,255,0.2);
}


#menu-options-ul li p{

    vertical-align: middle;
    display: table-cell;
    width: 100%;
}

#menu-options-ul li p:hover{

    color: #fff;
}

#rest-of-the-page{

    height:100%;
    transition: all .2s linear 0s;
    cursor:pointer;  
    background-color:antiquewhite
}

.page--sided{
  position:absolute;
    top:0;
    left:15%;
    width: 85%;
}


.page--full{
  width:100%;
}

JS:

 $(document).ready(function () {
            $("#rest-of-the-page").click(
                function () {
                    $("#sidebar").toggleClass("hidden");

                    if ($("#rest-of-the-page").hasClass("page--sided")){
                      $("#rest-of-the-page").removeClass("page--sided")
                      $("#rest-of-the-page").addClass("page--full");
                    }else{
                      $("#rest-of-the-page").addClass("page--sided")
                      $("#rest-of-the-page").removeClass("page--full");
                    }


                }
            );
        });

答案 3 :(得分:0)

var a = true;
	function show(sidebar){
if(a==true){
		sidebar.style.left = "0px";
		sidebar.style.transition  = "left 0.6s linear";
		a=false;
	}
	else{
		sidebar.style.left = "-200px";
		sidebar.style.transition  = "left 0.6s linear";
		a=true;
	}
	}
body{

	margin: 0px;
	padding:0px;
	font-family: helvetica;
}

#sidebar{
	width: 200px;
	height: 100%;
	background: #151718;
	top:0px;
	left:-200px;
	position: absolute;
}

#sidebar ul{
	margin-top: 10px;
	padding: 0px;
}

#sidebar ul li{
	list-style: none;
}

#sidebar ul li a{
	color:white;
	padding:10px;
	width: 180px;
	margin-bottom: 4px;
	display: block;
	border-bottom: 1px solid black;
    text-decoration:none;
}

#btn{
	margin: 20px;
	padding:20px;
	right: -90px;
	position: absolute;
}

#btn span{
	background: black;
	width: 25px;
	height:5px;
	margin-bottom: 5px;
	display: block;
    border-radius:17px;
}
<div id="sidebar">

<div id="btn" onclick="show(sidebar)">
<span></span>
<span></span>
<span></span>
</div>
<ul>
<li><a href="#">Facebook</a></li>

<li><a href="#">Vk</a></li>

<li><a href="#">google</a></li>

<li><a href="#">ymail</a></li>
</ul>

</div>

** Html左侧边栏使用CSS和Javascript **