Animation with Requestanimationframe - Javascript

Asked

Viewed 104 times

0

Based on the Onedrive website, where there is a drop-down menu on the left side that when clicked on the div Onedrive it loads the menu down; and if clicked again it returned in the first state, that is the collected menu.

Imagem do OneDrive

I’m trying to do this animation, only using Javascript only. After much research, I saw that it is not recommended to use some timer to make these kinds of animations. The Requestanimationframe is used for that. Follows the code where I’ve already managed to do most of what I want. I think what’s happening is that I can’t stop the animation at the right time, and therefore I can’t properly manipulate the cancelRequestAnimationFrame.

In the code, what I’m trying to do is when I click the square it goes down. When I click again it goes up. And just keep making these two moves, so that I can then make a menu based on this.

Thank you.

  • You messed up too much: http://jsfiddle.net/smpfjwg8/. And you can do this with CSS as well.

1 answer

4


I see no need to use both javascript when it is possible to use styles for these tasks.

var acao = function(e) {
  if (!e.target.nextElementSibling.style.height)
    e.target.nextElementSibling.style.height = '0px';
  else
    e.target.nextElementSibling.style.height = '';
};
var sub = document.querySelectorAll('[role=sub]');
sub[0].addEventListener('click', acao);
sub[1].addEventListener('click', acao);
main {
  width: 300px;
  box-shadow: 0px 0px 30px;
  margin: 50px auto;
}
[role=sub] {
  cursor: pointer;
  padding: 5px;
  border-bottom: 1px solid silver;
}
[role=sub]:hover {
  background: black;
  color: white;
}
div {
  transition: 0.5s;
}
[role=item] {
  line-height: 30px;
  margin-left: 15px;
  height: 150px;
  overflow: hidden;
}
<main>
  <div role="sub">OneDrive</div>
  <div role="item">
    <div>DADOS #01</div>
    <div>DADOS #02</div>
    <div>DADOS #03</div>
    <div>DADOS #04</div>
    <div>DADOS #05</div>
  </div>
  <div role="sub">Grupos</div>
  <div role="item">
    <div>GRUPO #01</div>
    <div>GRUPO #02</div>
    <div>GRUPO #03</div>
    <div>GRUPO #04</div>
    <div>GRUPO #05</div>
  </div>
</main>

  • I found your solution much interesting indeed. It helped me perfectly (decreases my JS). Now, please ask me some questions: 1 - In Javascript, what is the logic in lines 2, 3, 4 and 5? (When height is 0, does it do anything? Otherwise ... Anyway, I was left with doubt); 2 - When adding addeventlistener(action) in both Ivs I thought that by clicking a div, automatically the other would also receive the same effect as the one above. That is, the effects would happen simultaneously. Only I can click the top one and only the top one moves; the same with the bottom one. Obg

  • 1

    @Wesleyredfield the idea of these initial lines is to make the css property transition: 0.5s; work, I mean, Divs already have defined sizes so changing this value to zero, in the case of the example, makes the animation happen. Already the doubt concerning the addEventListener, does not execute all at the same time on account that the function depends only on the origin, does not have a specific link, that part e.target is the component itself that received the action, only for him who will act.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.