jQuery Animate() with unwanted behavior

Asked

Viewed 30 times

1

Good evening! I am making an application and need to use some slide effects. I used the animate jquery to get the desired result.

It was even good, but while the animation occurs the height of the div increases/ decreases so that all the text inside fits, follows the example:

$('#btnprincipal').click(function (){
  $('#divTexto').animate({ width: 'toggle' }, 500);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btnprincipal">Aperte aqui</button>

<div id="divTexto" style="background-color:#00ff00">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ultricies erat massa, sed posuere risus pharetra sed. Integer in pretium tortor. Sed vestibulum orci ut odio lacinia fermentum. Ut nec nulla sed ligula finibus volutpat vel nec sem. Nulla aliquam mauris vel tortor sodales, id finibus est varius. Duis suscipit ultrices dolor, vitae molestie ex fringilla vitae. Suspendisse et sapien eu orci egestas volutpat quis vitae mauris. Curabitur nisl risus, maximus ac tristique sed, consequat quis risus. Nunc vel venenatis mi, eu faucibus arcu. Nunc quis faucibus sem. Vestibulum dignissim commodo lectus, sit amet iaculis metus lacinia ac. Integer placerat faucibus metus et consequat. Aliquam sed volutpat justo, vel pharetra nunc.</div>

What can I do to prevent this behavior?

  • if the height do div this should not happen, for example height: 200px

  • I deleted my answer, now that I see the accepted answer I understand the purpose of the question :)

  • Yes, but I couldn’t set height because the text was generated according to what the user typed into what I did. The accepted answer gave the solution to the problem

1 answer

3


So that the overflow function as expected, it is necessary that the div has the set height. However, you cannot set a static height because it varies according to the content (text) and eventually according to the size of the viewport.

My suggestion is to set just the height while the animation is going on and remove it as soon as the animation is over. For this we can use the callbacks start and done.

Something like the following code:

$('#btnprincipal').click(function (){
  const divTexto = $('#divTexto');
  
  divTexto.animate(
    { width: 'toggle' },
    {
      duration: 500,
      start: () => {
        divTexto.css('height', divTexto.height());
      },
      done: () => {
        divTexto.css('height', 'auto');
      },
    },
  );
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btnprincipal">Aperte aqui</button>

<div id="divTexto" style="background-color:#00ff00">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam ultricies erat massa, sed posuere risus pharetra sed. Integer in pretium tortor. Sed vestibulum orci ut odio lacinia fermentum. Ut nec nulla sed ligula finibus volutpat vel nec sem. Nulla aliquam mauris vel tortor sodales, id finibus est varius. Duis suscipit ultrices dolor, vitae molestie ex fringilla vitae. Suspendisse et sapien eu orci egestas volutpat quis vitae mauris. Curabitur nisl risus, maximus ac tristique sed, consequat quis risus. Nunc vel venenatis mi, eu faucibus arcu. Nunc quis faucibus sem. Vestibulum dignissim commodo lectus, sit amet iaculis metus lacinia ac. Integer placerat faucibus metus et consequat. Aliquam sed volutpat justo, vel pharetra nunc.</div>

  • Thank you so much! I didn’t know how to solve the problem of having my variable height, it solves my problem

Browser other questions tagged

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