Dynamic height in 'product cart'

Asked

Viewed 105 times

1

I have a div which serves as a shopping cart, it is fixed at the top of the screen where only the subtotal of all products chosen and by clicking on the div, she makes a slide down effect and shows all the products.

I did the effect of slide animated in css, and I leave the div fixed at the top with Top negative, according to the size of the div, for example, top: -370px.

The problem is that the size of the div is dynamic, increases as products are added, so even with the top negative, div does not 'hide' all products. I’ve tried changing the top property as the add product buttons are clicked, which works, but I don’t know how to change the top of the animation that makes go down and up the div.

Does anyone know if there’s a way to change this property or if there’s a better way to do it?

To make it easier to understand: https://jsfiddle.net/84rczeds/2/

  • You can simplify your stroller a lot.

  • So I want to leave the animations only in css, but I think I ended up complicating too much even haha

2 answers

1

Wouldn’t it be better if you rethought your logic to make it easier?
So the code is doing too much unnecessary calculation, see an example:

Create a div to encompass the products and the subtotal and add the css to stay at the top, the structure html will stay like this:

<div class="engloba-tudo">

   <div class="produtos">
     // Sua lista de produtos
   </div>
   <div class="subtotal ">
     // Subtotal dos produtos
   </div>

</div>

Now just leave your div products with display:none; and how I saw that you are using Jquery can use the function slideToggle() that already saves enough code to show the products:

$(".subtotal").click(function() {
   $(".produtos").slideToggle(300);
})

Example in Jsfiddle.
But it’s just an idea, because as I’ve also made a cart with animation I know how annoying it is to maintain or change large code, besides having greater chance of crashes;

  • It got simpler anyway, I’m trying to use more animation with css, but I think I ended up complicating things too haha

0


I don’t quite understand what you mean, but I think that’s what you want.

var subtotal = $('.subtotal').height();

$('.subtotal-menu').click(function(event) {
				$('.subtotal').addClass('slideDown').removeClass('slideUp');
				$('.subtotal-menu').hide();
				$('.close-subtotal').show();
});

$('.close-subtotal').click(function(event) {
				$('.subtotal').addClass('slideUp').removeClass('slideDown');
				$('.subtotal-menu').show();
				$('.close-subtotal').hide();
});

$('button').click(function() {
			$('.produtos').append('<p>Produto X</p><hr>');
      subtotal = $('.subtotal').height();
      $('.subtotal').css('top', '-'+subtotal+'px')
      alert(subtotal)
});
.subtotal {
  position: relative;
  width: 200px;
  top: -170px;
  margin: 0 auto;
}

span {
  position: absolute;
  top: 0;
}

@keyframes slideDown {
  
  to {
    top: 0;
  }
}

.slideDown {
    animation-name: slideDown;
    animation-duration: .5s;
    animation-fill-mode: both;
}

@keyframes slideUp {
  from {
    top: 0;
  }
  
}

.slideUp {
    animation-name: slideUp;
    animation-duration: .5s;
    nimation-fill-mode: both;
}
<script
  src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
  integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g="
  crossorigin="anonymous"></script>

<div class="subtotal">
  <div class="produtos">
    <p>Produto 1</p>
    <hr>
    <p>Produto 2</p>
    <hr>
    <p>Produto 3</p>
  </div>
    <hr>
    <p>Subtotal: x</p>
  
 
</div>
 <span class="subtotal-menu" style="cursor: pointer;">O</span>
 <span class="close-subtotal" style="display: none; cursor: pointer;">X</span>
 
 <button>add Produto</button>

  • Yes, that’s more or less what I really want. As you add products, the div keeps showing only the part where you have the subtotal. You got any moves on the animation property, too? Because by clicking the button and adding products, the div stays where it has to be, but when it does the animation to div back it continues with the top of before.

  • You do not need the 'from' and 'to' of the Keyframes, if you take the animation will work perfectly, updated in my reply, see if it is what you need.

  • It was exactly the way I wanted it! I hadn’t really touched that I didn’t need the 'from' and 'to' to do the animation. Thank you very much!

  • @Felipeevangelista Don’t forget to leave +1 hugs

Browser other questions tagged

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