My project is almost ready and one the of the last things I would like do is to animate .quote-container
element's height. Currently, it changes very quickly without any animation effect and unfortunately, this line of CSS didn't work:
.quote-container {
transition: height 1s;
}
However, the same line only applied to the #new-quote
selector, worked:
#new-quote {
transition: height 1s;
}
See the Pen gomeYN by Lukas (@Kestis500) on CodePen.
I've made a video so you can understand what I mean: https://www.youtube.com/watch?v=21V3NbAagnk&feature=youtu.be.
Also, in the 0:08 - 0:09 seconds there is a buggy up and down height change of the .quote-container
element. What's happening here?
I've looked at the animate()
function in the jQuery documentation but I don't understand how to implement it if text in the .quote-container
changes every time you press the #new-quote
button.
It's not an exactly the same question as this one. I've done what was written in that question's answer but it still doesn't work like it should. Plus it's using CSS, not JS. I've saved my codepen: https://codepen.io/Kestis500/pen/gomeYN?editors=0100. Can someone please take a look and say what I've done wrong? Here are my changes:
//CSS
.quote-container, #new-quote { transition: max-height 1s; }
//JS
$("#new-quote").on("click", function() {
$(".quote-container").css({ maxHeight: 0, overflow: "hidden" });
$(".quote-text, .quote-author").fadeTo(1000, 0, function() {
getQuote(function() {
$(".quote-container, #new-quote").css({
maxHeight: $(".quote-container").outerHeight()
});
$(".quote-text, .quote-author").fadeTo(1000, 1);
});
});
});
EDIT 1 I've changed my code so when the page loads, it changes the max-height smoothly resulting in the smooth animation what I needed. Codepen: https://codepen.io/Kestis500/pen/WdpBqv?editors=0010, however, I'm still working on the click event because it's not working the same way as page load.
EDIT 2
Thank you Twisty for your huge help :) I've done some changes to my own preference and now the only thing that needs to be fixed is that animation should start at the same time (#new-quote
and .quote-container
elements), however, I think it's gonna be pretty easy to fix :)
EDIT 3 Here we go! Exactly what I needed and the animation is amazing! :) https://jsfiddle.net/z5hds4Lp/.
EDIT 4 https://jsfiddle.net/z5hds4Lp/2/ - final edit.
Note: Don't use pixels either in the media queries or CSS, use rems for CSS and ems for media queries: https://engageinteractive.co.uk/blog/em-vs-rem-vs-px and https://zellwk.com/blog/media-query-units/.