如何使用幻灯片动画移动div?

时间:2018-08-07 00:56:57

标签: javascript jquery html css

我正在使用html,css和javascript使边栏更加直观。 我想用慢动画(如幻灯片)移动html代码中的侧边栏。 现在,我只使用纯JavaScript。但是,如果可行,可以使用jquery解决此问题。 但是现在,它像演示一样移动。现在移动不平稳。 我该怎么解决?

// Function for the button that open the sidebar.
  function openRightMenu() {
      document.getElementById("sidebar").style.display = "block";
      document.getElementById("sidebar").style.left = "76%";
  }
  //Function for the button for close the sidebar.
  function closeRightMenu() {
      document.getElementById("sidebar").style.display = "none";
  }
body {
  margin: 0;
  padding: 0;
  overflow-x: hidden;
  height:100%;
  width:100%;
  position: absolute;
}
.map-group {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 100%;
}

#sidebar {
  position: relative;
  display: none;
  right: 0;
  top: 0;
  bottom: 0;
  width: 24%;
  height: 700px;
  overflow: auto;
  margin-left: 0%;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
  background-color: white;
  opacity: 1.0;
  z-index: 1;
}

#img1, #img2, #img3 {
  max-width: 100%;
  margin-left: 1px;
}

.w3-teal {
  position: absolute;
  display: inline-block;
  width: 30px;
  height: 30px;
  left: 97%;
}

.closebtn {
  font-size: 36px;
  display: inline-block;
  margin-left: 19px;
}

search-bar {
  position: absolute;
  left: 27%;
  top: 2%;
  width: 50%;
}

.info {
  padding: 6px 8px;
  font: 14px/16px Arial, Helvetica, sans-serif;
  background: white;
  background: rgba(255, 255, 255, 0.8);
  box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
  border-radius: 5px;
}
.info h4 {
  margin: 0 0 5px;
  color: #777;
}
.legend {
  line-height: 18px;
  color: #555;
}
.legend i {
  width: 18px;
  height: 18px;
  float: left;
  margin-right: 8px;
  opacity: 0.7;
}

.fa {
  position: relative;
  display: table-cell;
  width: 60px;
  height: 36px;
  text-align: center;
  vertical-align: middle;
  font-size: 20px;
}

.fa-2x {
  font-size: 2em;
}  

#arrow {
  margin-top: 2px;
}

.col-xs-2{
  padding-left: 0;
  padding-right: 0;
}
.col-xs-10{
  padding-left: 5px;
  padding-right: 5px;
}

#logo_img{
  z-index: 0;
  position: absolute;
  top:2%;
  left:10%;
  width : 15%;
  height: 10%;

}

.btn.btn-info{
  position: relative;
  left: 110%;
}

#powerby{
  position: absolute;
  bottom: 0%;
  left: 74%;
  width: 10%;
  z-index: 0;
}

.white {
  color : white;
}

.stats{
  position: absolute;
  bottom: 0;
  pointer-events: none;
  line-height: 1;
  width: 100%;
}

time-series {
  z-index: 1000;
  pointer-events: auto;
  position: relative;
  display: inline-block;
  left: 4%;
}

#count-div{
  color: #000000;
  font-weight:500;
  margin-top: 20px;
  margin-bottom: 40px;
  padding-left: 10px;
  float: left;
}

#count{
  font-size: 32px;
  margin-bottom: 0;
  text-align: right;
}

#count-text{
  font-size: 13px;
}

.normalize p{
  text-align: center;
}

.search-keyword-btn{
  background-color: crimson;
  color: white;
  font-weight: bold;
  border-radius: 10px;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
}

.search-keyword-btn:active, .search-keyword-btn:focus, .search-keyword-btn:hover{
  background-color: transparent;
  color: black;
}

.search-keyword-btn-group{
  margin-left: 36%;
}
/*For the popup box of tweets in points map*/
.tweet {
  padding: 8px 8px 4px 8px;
  font-size: 12px; }

.tweet-top {
  display: -ms-flexbox;
  display: flex; }

.tweet-user-photo {
  -ms-flex-negative: 0;
  flex-shrink: 0;
  background-size: 32px 32px;
  background-repeat: no-repeat;
  width: 32px;
  height: 32px;
  overflow: hidden;
  background: #e2e2e2; }

.tweet-user-photo img {
  display: none; }

.tweet-body {
  padding-left: 8px;
  line-height: 1.2;
  padding-bottom: 6px; }

.tweet-body .name {
  font-weight: bold;
  color: #3b94d9;
  cursor: pointer; }

.tweet-body .name:hover {
  color: #9266CC; }

.tweet-body .time {
  color: #a7a7a7;
  font-size: 11px; }

.tweet-body .user-info {
  margin-bottom: 2px; }

.tweet-media img {
  width: 100%; }

.tweet-link {
  text-decoration: none;
  color: #3b94d9; }

.tweet-link:hover {
  text-decoration: underline; }

/* button for hiding and showing the timebar */
.slide-up-down {
  position: absolute;
  display: inline-block;
  width: 53px;
  height: 35px;
  bottom : 110px;
  margin-left: 5px;
}
<div class="w3-teal">
    <button type="button" class="btn btn-default btn-sm" onclick="openRightMenu()">
      <span id="hamburger" class="glyphicon glyphicon-menu-hamburger" aria-hidden="true"></span>
    </button>
  </div>

<div id='sidebar' class="open" ng-init="click = -1" ng-class="{toggled: click === 1}" ng-controller="SidebarCtrl">
    <div class = "col-xs-2">
      <a href="javascript:void(0)" class="closebtn" onclick="closeRightMenu()">&times;</a>
      <ul class="nav nav-tabs tabs-left">
        <li>1</li>
        <li>2</li>
        <li>3</li>
      </ul>
    </div>
  </div>

2 个答案:

答案 0 :(得分:0)

这是使用CSS动画的非常简单的演示。请注意,这不是生产代码,只是滑动动画的一个肮脏的例子。

jsfiddle: https://jsfiddle.net/CriticalError/4qru8mb6/

HTML

<div id="action-menu">
Click to Open Menu
</div>
<div id="sub-menu">
  <a href="#">My Link 1</a>
  <br />
  <a href="#">My Link 2</a>
  <br />
  <a href="#">My Link 3</a>
</div>

CSS

body {
  padding: 0;
  margin: 0;
}
#action-menu {
  position: relative;
  width: 100%;
  padding: 10px 4px 10px 4px;
  background-color: silver;
  cursor: pointer;
}
#sub-menu {
  position: relative;
  padding-left: 10px;
  overflow: hidden;
  height: 0px;
  background-color: red;
}
.slideOpen {
  animation: slideOpen 1s forwards;
}
.slideShut{
  animation: slideShut 1s forwards;
}
@keyframes slideOpen {
  0% { height: 0px; }
  100% { height: 100px; }
}
@keyframes slideShut {
  0% { height: 100px; }
  100% { height: 0px; }
}

JavaScript

var actionMenu = document.getElementById( "action-menu" );
var isOpen = false;

actionMenu.addEventListener( "click", function () {

    if ( isOpen ) {

    document.getElementById( "sub-menu" ).className = "slideShut";
    isOpen = false;

  }
  else {

    document.getElementById( "sub-menu" ).className = "slideOpen";
    isOpen = true;

  }

})

答案 1 :(得分:0)

检查我的示例笔

Simple Navigation Animation 我在导航容器上切换了一个触发CSS转换的类。相当容易使用

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions

您还可以使用@keyframes,这也需要进行类切换

https://developer.mozilla.org/en-US/docs/Web/CSS/@keyframes