从左到上移动侧栏

时间:2020-01-28 07:08:40

标签: javascript jquery html css css-transitions

我有这个补充工具栏,它最初位于台式机和大屏幕的左侧到右侧。在平板电脑和较小的屏幕中,此侧边栏是否有可能从底部移到顶部? 例如,当我单击桌面或更大屏幕上的侧边栏时,它从左到右导航,我想要的是,当我单击平板电脑或更小侧边栏中的侧边栏时,它应该从顶部扩展到底部 这就是我到目前为止所拥有的。

function openNav() {
  document.getElementById("mySidebar").style.width = "40%";
  document.getElementById("main").style.marginLeft = "40%";
}

function closeNav() {
  document.getElementById("mySidebar").style.width = "0";
  document.getElementById("main").style.marginLeft = "0";
}
.sidebar {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 999999;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}

.sidebar a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidebar a:hover {
  color: #f1f1f1;
}

.sidebar .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

.openbtn {
  font-size: 20px;
  cursor: pointer;
  color: black;
  padding: 10px 15px;
  border: none;
  background: transparent;
  outline: none;
}

#main {
  transition: margin-left .5s;
  background-color: #fff;
  border: 1px solid black;
  height: 100vh;
  width: 48px;
  @media (max-width: 768px) {
    width: 100%;
    text-align: center;
    height: 48px;
  }
}
<div id="mySidebar" class="sidebar">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
</div>

<div id="main">
  <button class="openbtn" onclick="openNav()">☰</button>
</div>

2 个答案:

答案 0 :(得分:1)

尝试寻求解决方案,如果您需要更多说明,请告诉我

var x = window.matchMedia("(max-width: 768px)");

function openNav() {
  if (!x.matches) {
    document.getElementById("mySidebar").style.height = "60%";
    document.getElementById("mySidebar").style.width = "100%";
    document.getElementById("main").style.marginTop = "50%";
  } else {
    document.getElementById("mySidebar").style.width = "40%";
    document.getElementById("main").style.marginLeft = "40%";
  }
}

function closeNav() {
  if (!x.matches) {
    document.getElementById("mySidebar").style.height = "0";
    document.getElementById("mySidebar").style.width = "0";
    document.getElementById("main").style.marginTop = "0";
  } else {
    document.getElementById("mySidebar").style.width = "0";
    document.getElementById("main").style.marginLeft = "0";
  }
}
.sidebar {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 999999;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: all 0.5s;
  padding-top: 60px;
  @media (max-width: 768px) {
   height: 0;
   width: 0;
  }

}

.sidebar a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
  transition: 0.3s;
}

.sidebar a:hover {
  color: #f1f1f1;
}

.sidebar .closebtn {
  position: absolute;
  font-size: 36px;
  right: 0;
  top: 5px;
  margin-left: 40px;
  
  @media (max-width: 768px) {
    bottom: 0;
    left: 25px;
    margin-top: 40px;
  }

}

.openbtn {
  font-size: 20px;
  cursor: pointer;
  color: black;
  padding: 10px 15px;
  border: none;
  background: transparent;
  outline: none;
}

#main {
  transition: margin-top .5s;
  background-color: #fff;
  border: 1px solid black;
  height: 100vh;
  width: 48px;
  @media (max-width: 768px) {
    height: 48px;
    width: 100vw;
  }
}
<div id="mySidebar" class="sidebar">
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Clients</a>
  <a href="#">Contact</a>
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
</div>

<div id="main">
  <button class="openbtn" onclick="openNav()">☰</button>
</div>

答案 1 :(得分:0)

您可以使用PdfSignatureAppearance查询来确定屏幕尺寸,然后相应地设置其样式。 Here是W3schools的一个简单解释,您可以从顶部开始,而不是从左侧过渡。

相关问题