HTML CSS / JS底部导航栏向上滑动

时间:2019-03-21 16:09:47

标签: javascript html css navbar

我已经进行了一些研究,但似乎无法完全找到所需的内容。我的目标是在我的JS应用程序的底部具有一个导航栏,当用户单击某个按钮时,它将启动一个动画,其中导航栏将从应用程序的底部移动到应用程序的顶部。我做了一些修改以说明我的意思:

default position

after user presses "profile" button, for example

不知道哪个JS库可以帮助我解决这个问题,或者是否有代码示例。这里的关键是我不希望它在单击的任何按钮(仅某些按钮)上移动。例如,如果用户从上面的示例中单击“库”,则希望它停留在底部。

也许有人知道我能做到这一点吗?

编辑:所以我这样做的原因是因为这是一个电子应用程序,我希望某些内容位于本地,而某些内容位于远程。这就是为什么当用户按下“库”时,我希望导航栏保持静止。但是,如果用户按下“配置文件”,它将移至顶部,“内容”窗口的作用类似于Web浏览器,因为它将在远程Web服务器上加载页面。希望对您有所帮助。并感谢您提供所有信息!

编辑2:有点题外话,但是我得到这个奇怪的填充,我不确定从哪里来:

weird space

编辑3:

此处包含HTML和CSS:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title></title>
 <script type="text/javascript" src="renderer.js"></script>
 <script 
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
 </script>
 <link rel="stylesheet" href="styles.css">
<body>
<script>
  function toggleNavLocation() {
    //alert('clciiikkkked');
    $('nav').toggleClass('top');
  }
</script>
<nav>
  <div id="logo_container"><img id="logo" 
 src="./assets/images/poscon_nav.jpg" width="100px" height="55px" /></div>
  <div id="navitems">
  <ul>
    <li id="dashboard">DASHBOARD</li>
    <li id="pilotclient">PILOT CLIENT</li>
    <li id="livemap">LIVE MAP</li>
    <li id="community">COMMUNITY</li>
    <li id="profile" onclick="toggleNavLocation()">PROFILE</li>
    <li id="training">TRAINING</li>
    <li id="support">SUPPORT</li>
    <li id="library">LIBRARY</li>
  </ul>
  </div>
</nav>

<div id="right_content">
  <div id="user_pane">
    <span id="username"></span>
  </div>
</div>

<div id="center_content">

</div>

<div id="left_content">

</div>

 </body>
</html>

CSS:

 body {
  font-family: "Arial", Serif;
  background-color: rgb(27, 27, 27);
  overflow-x: hidden;
  overflow-y: hidden;
 }

 nav {
  position: absolute;
  bottom: 0;
  left: 0;
  width:850;
  height:75px;
  background: rgb(27, 27, 80);
  -webkit-transition: all 1s ease;
  -moz-transition: all 1s ease;
  -o-transition: all 1s ease;
  -ms-transition: all 1s ease;
  transition: all 1s ease;
 }

 nav.top {
  bottom:calc(100% - 50px);
  -webkit-transition: all 1s ease;
  -moz-transition: all 1s ease;
  -o-transition: all 1s ease;
  -ms-transition: all 1s ease;
  transition: all 1s ease;
 }

 #dashboard, #pilotclient, #livemap, #community, #profile, #training, 
 #support, #library, #search_img {
  font-size: 11px;
  color: rgb(81, 81, 81);
  padding-top: 22px;
  display: inline-block;
  width: 75px;
  height:75px;
  background:rgb(27, 27, 27);
  text-align:center;
 }

#dashboard:hover, #pilotclient:hover, #livemap:hover, #community:hover, 
#profile:hover, #training:hover, #support:hover, #library:hover {
  background: #252525;
}

#logo_container {
 display: inline-block;
 position: absolute;
 left: 10px;
 bottom: 2px;
}

#navitems {
 display: inline-block;
 margin-left: 150px;
 height: 100px;
}

#right_content {
 width: 250px;
 height: 575px;
 position: absolute;
 top: 0px;
 right: 0px;
 background-color: red;
}

#center_content {
 width: 500px;
 height: 575px;
 position:absolute;
 top: 0px;
 right: 250px;
 background-color: blue;
}

#left_content {
 width: 250px;
 height: 575px;
 position:absolute;
 top: 0px;
 left: 0px;
 background-color: green;
}

#user_pane {
 width: 250px;
 height: 250px;
 position: absolute;
 top: 0px;
 right: 0px;
 background-color: green;
}

#username {
 width: 200px;
 height: 25px;
 position: absolute;
 top: 175px;
 left: 20px;
 text-align: center;
 background-color: white;
}

3 个答案:

答案 0 :(得分:0)

这似乎很简单,但是我们需要知道您使用了哪个属性将导航栏定位在底部?最好的解决方案是在flexbox行为中创建两个具有不同属性的css属性,然后在单击配置文件时使用JavaScript更改对应于属性的导航ID。

答案 1 :(得分:0)

您可以使用导航键为导航栏设置动画,使其在2个垂直位置之间滑动:-

@keyframes animatebottom {
from {
    bottom: -300px;
    opacity: 0
}
to {
    bottom: 0;
    opacity: 1
}
}

修改“底部”属性以适合您的页面高度和其他要求。

答案 2 :(得分:0)

您可以使用一些JS将类添加到导航栏中以制作动画,并且可以在单击具有特定ID的按钮时添加此类。

下面是演示此内容的代码段:

$('#profile').on('click', function(){
  toggleNavLocation();
});

function toggleNavLocation() {
$('nav').toggleClass('top');
}
nav {
  width:100vw;
  height:50px;
  background:#000;
  bottom: 0;
  color:#fff;
  -webkit-transition: all 1s ease;
  -moz-transition: all 1s ease;
  -o-transition: all 1s ease;
  -ms-transition: all 1s ease;
  transition: all 1s ease;
  position: absolute;
}

nav.top {
  bottom:calc(100% - 50px);
  -webkit-transition: all 1s ease;
  -moz-transition: all 1s ease;
  -o-transition: all 1s ease;
  -ms-transition: all 1s ease;
  transition: all 1s ease;
}

li {
display:inline-block;
width:100px;
height:25px;
background:rgba(255,255,255,0.2);
text-align:center;
line-height:25px;
cursor:pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body>
<nav>
<ul>
<li id="profile">Profile</li>
<li id="library">Library</li>
</ul>
</nav>
</body>
</html>

如果您不希望它进行动画处理,而只是跳到顶部或底部,则可以删除所有这些行:

-webkit-transition: all 1s ease;
  -moz-transition: all 1s ease;
  -o-transition: all 1s ease;
  -ms-transition: all 1s ease;
  transition: all 1s ease;

以下是演示此内容的代码段:

$('#profile').on('click', function(){
  toggleNavLocation();
});

function toggleNavLocation() {
$('nav').toggleClass('top');
}
nav {
  width:100vw;
  height:50px;
  background:#000;
  bottom: 0;
  color:#fff;
  position: absolute;
}

nav.top {
  bottom:calc(100% - 50px);
}

li {
display:inline-block;
width:100px;
height:25px;
background:rgba(255,255,255,0.2);
text-align:center;
line-height:25px;
cursor:pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
<body>
<nav>
<ul>
<li id="profile">Profile</li>
<li id="library">Library</li>
</ul>
</nav>
</body>
</html>

这些示例为我们提供了一些JQuery,但是,如果您要使用纯JS,您应该可以将其移植到Vanilla,而在我提供的代码中稍加考虑。

相关问题