Animate change in size of div when content changes
up vote
1
down vote
favorite
First: I know that there are a lot of different questions similar to this one. I've looked at a lot of them and tried a lot of things.
Here's what I'm trying to do: Using JavaScript, I'm changing the content of a div on my page. When it changes, the size changes instantly. I'd like the change to be animated.
It seems the general consensus is that 'auto' can't be animated, so here's what I have so far:
function rightClick() {
fgContent = document.querySelector('#fgContent');
rtButton = document.querySelector('#rightButton');
fgDiv = document.querySelector('#fgDiv');
spacing = fgDiv.clientHeight - fgContent.clientHeight;
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgDiv.maxHeight = fgDiv.clientHeight;
fgDiv.minHeight = fgDiv.clientHeight;
fgContent.innerHTML = nextDiv.innerHTML;
fgDiv.maxHeight = nextDiv.clientHeight + spacing;
console.log('Max height')
fgContent.minHeight = 0;
rtButton.textContent = nextButton;
};
.foreground {
position: relative;
z-index: 1;
width: 33%;
margin-top: 10%;
margin-left: 10%;
padding: 10px;
color: black;
background: whitesmoke;
height: auto;
opacity: .85;
border-radius: 10px;
-moz-transition: height 1s ease;
-webkit-transition: height 1s ease;
-o-transition: height 1s ease;
transition: height 1s ease;
}
<div class="foreground" id="fgDiv">
<div id="fgContent">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
</div>
<p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
</div>
<div class="hidden" id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
I think that's all the moving parts. I'm guessing that it's more a question of me having overlooked something, but for the life of me, I don't see it.
javascript css css-animations
add a comment |
up vote
1
down vote
favorite
First: I know that there are a lot of different questions similar to this one. I've looked at a lot of them and tried a lot of things.
Here's what I'm trying to do: Using JavaScript, I'm changing the content of a div on my page. When it changes, the size changes instantly. I'd like the change to be animated.
It seems the general consensus is that 'auto' can't be animated, so here's what I have so far:
function rightClick() {
fgContent = document.querySelector('#fgContent');
rtButton = document.querySelector('#rightButton');
fgDiv = document.querySelector('#fgDiv');
spacing = fgDiv.clientHeight - fgContent.clientHeight;
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgDiv.maxHeight = fgDiv.clientHeight;
fgDiv.minHeight = fgDiv.clientHeight;
fgContent.innerHTML = nextDiv.innerHTML;
fgDiv.maxHeight = nextDiv.clientHeight + spacing;
console.log('Max height')
fgContent.minHeight = 0;
rtButton.textContent = nextButton;
};
.foreground {
position: relative;
z-index: 1;
width: 33%;
margin-top: 10%;
margin-left: 10%;
padding: 10px;
color: black;
background: whitesmoke;
height: auto;
opacity: .85;
border-radius: 10px;
-moz-transition: height 1s ease;
-webkit-transition: height 1s ease;
-o-transition: height 1s ease;
transition: height 1s ease;
}
<div class="foreground" id="fgDiv">
<div id="fgContent">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
</div>
<p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
</div>
<div class="hidden" id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
I think that's all the moving parts. I'm guessing that it's more a question of me having overlooked something, but for the life of me, I don't see it.
javascript css css-animations
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
First: I know that there are a lot of different questions similar to this one. I've looked at a lot of them and tried a lot of things.
Here's what I'm trying to do: Using JavaScript, I'm changing the content of a div on my page. When it changes, the size changes instantly. I'd like the change to be animated.
It seems the general consensus is that 'auto' can't be animated, so here's what I have so far:
function rightClick() {
fgContent = document.querySelector('#fgContent');
rtButton = document.querySelector('#rightButton');
fgDiv = document.querySelector('#fgDiv');
spacing = fgDiv.clientHeight - fgContent.clientHeight;
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgDiv.maxHeight = fgDiv.clientHeight;
fgDiv.minHeight = fgDiv.clientHeight;
fgContent.innerHTML = nextDiv.innerHTML;
fgDiv.maxHeight = nextDiv.clientHeight + spacing;
console.log('Max height')
fgContent.minHeight = 0;
rtButton.textContent = nextButton;
};
.foreground {
position: relative;
z-index: 1;
width: 33%;
margin-top: 10%;
margin-left: 10%;
padding: 10px;
color: black;
background: whitesmoke;
height: auto;
opacity: .85;
border-radius: 10px;
-moz-transition: height 1s ease;
-webkit-transition: height 1s ease;
-o-transition: height 1s ease;
transition: height 1s ease;
}
<div class="foreground" id="fgDiv">
<div id="fgContent">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
</div>
<p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
</div>
<div class="hidden" id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
I think that's all the moving parts. I'm guessing that it's more a question of me having overlooked something, but for the life of me, I don't see it.
javascript css css-animations
First: I know that there are a lot of different questions similar to this one. I've looked at a lot of them and tried a lot of things.
Here's what I'm trying to do: Using JavaScript, I'm changing the content of a div on my page. When it changes, the size changes instantly. I'd like the change to be animated.
It seems the general consensus is that 'auto' can't be animated, so here's what I have so far:
function rightClick() {
fgContent = document.querySelector('#fgContent');
rtButton = document.querySelector('#rightButton');
fgDiv = document.querySelector('#fgDiv');
spacing = fgDiv.clientHeight - fgContent.clientHeight;
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgDiv.maxHeight = fgDiv.clientHeight;
fgDiv.minHeight = fgDiv.clientHeight;
fgContent.innerHTML = nextDiv.innerHTML;
fgDiv.maxHeight = nextDiv.clientHeight + spacing;
console.log('Max height')
fgContent.minHeight = 0;
rtButton.textContent = nextButton;
};
.foreground {
position: relative;
z-index: 1;
width: 33%;
margin-top: 10%;
margin-left: 10%;
padding: 10px;
color: black;
background: whitesmoke;
height: auto;
opacity: .85;
border-radius: 10px;
-moz-transition: height 1s ease;
-webkit-transition: height 1s ease;
-o-transition: height 1s ease;
transition: height 1s ease;
}
<div class="foreground" id="fgDiv">
<div id="fgContent">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
</div>
<p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
</div>
<div class="hidden" id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
I think that's all the moving parts. I'm guessing that it's more a question of me having overlooked something, but for the life of me, I don't see it.
function rightClick() {
fgContent = document.querySelector('#fgContent');
rtButton = document.querySelector('#rightButton');
fgDiv = document.querySelector('#fgDiv');
spacing = fgDiv.clientHeight - fgContent.clientHeight;
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgDiv.maxHeight = fgDiv.clientHeight;
fgDiv.minHeight = fgDiv.clientHeight;
fgContent.innerHTML = nextDiv.innerHTML;
fgDiv.maxHeight = nextDiv.clientHeight + spacing;
console.log('Max height')
fgContent.minHeight = 0;
rtButton.textContent = nextButton;
};
.foreground {
position: relative;
z-index: 1;
width: 33%;
margin-top: 10%;
margin-left: 10%;
padding: 10px;
color: black;
background: whitesmoke;
height: auto;
opacity: .85;
border-radius: 10px;
-moz-transition: height 1s ease;
-webkit-transition: height 1s ease;
-o-transition: height 1s ease;
transition: height 1s ease;
}
<div class="foreground" id="fgDiv">
<div id="fgContent">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
</div>
<p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
</div>
<div class="hidden" id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
function rightClick() {
fgContent = document.querySelector('#fgContent');
rtButton = document.querySelector('#rightButton');
fgDiv = document.querySelector('#fgDiv');
spacing = fgDiv.clientHeight - fgContent.clientHeight;
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgDiv.maxHeight = fgDiv.clientHeight;
fgDiv.minHeight = fgDiv.clientHeight;
fgContent.innerHTML = nextDiv.innerHTML;
fgDiv.maxHeight = nextDiv.clientHeight + spacing;
console.log('Max height')
fgContent.minHeight = 0;
rtButton.textContent = nextButton;
};
.foreground {
position: relative;
z-index: 1;
width: 33%;
margin-top: 10%;
margin-left: 10%;
padding: 10px;
color: black;
background: whitesmoke;
height: auto;
opacity: .85;
border-radius: 10px;
-moz-transition: height 1s ease;
-webkit-transition: height 1s ease;
-o-transition: height 1s ease;
transition: height 1s ease;
}
<div class="foreground" id="fgDiv">
<div id="fgContent">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
</div>
<p><div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
</div>
<div class="hidden" id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
javascript css css-animations
javascript css css-animations
edited Nov 17 at 21:22
Scott Marcus
37.9k51936
37.9k51936
asked Nov 17 at 21:14
toby.in.dresden
529
529
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Use CSS transitions
, (as you have done) and JS .scrollHeight
.
Longer explanation:
- Set the dimensions on page load, because Transitions can't animate something from
auto
to##px
. - Set the
scrollHeight
dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)
Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.
var fgDiv = document.querySelector('#fgDiv');
var fgContent = document.querySelector('#fgContent');
var rtButton = document.querySelector('#rightButton');
updateDimesions(fgContent, fgContent); // First update of the dimensions
function rightClick() {
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgContent.innerHTML = nextDiv.innerHTML;
rtButton.textContent = nextButton;
updateDimesions(fgContent, nextDiv); // Second update of the dimensions
};
function updateDimesions(el, replace) {
el.style.width = replace.scrollWidth + 'px';
el.style.height = replace.scrollHeight + 'px';
}
.foreground {
width: 33%;
background: whitesmoke;
}
.foreground-inner {
overflow: hidden;
/* Hide the content initially */
-moz-transition: all 1s ease;
-webkit-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
}
.hidden {
height: 0;
overflow: hidden;
}
.hidden > div {
overflow: hidden;
/* This accounts for margin in ".scrollHeight" */
padding: 0;
/* Padding messes with .scrollHeight */
}
<div class="foreground" id="fgDiv">
<div id="fgContent" class="foreground-inner">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
<p>
<div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
<div class="hidden">
<div id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
</div>
</div>
</div>
Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
– toby.in.dresden
Nov 17 at 22:22
@toby.in.dresden Gotcha. Updated.
– Chris Happy
Nov 17 at 22:51
I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
– toby.in.dresden
Nov 18 at 22:47
@toby.in.dresden Your hidden divs can havedisplay: none
, otherwise, JS will think it hasheight: 0
. In my solution, I hid them by wrapping the content with a div that hasheight: 0; overflow: hidden;
.
– Chris Happy
Nov 19 at 22:57
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Use CSS transitions
, (as you have done) and JS .scrollHeight
.
Longer explanation:
- Set the dimensions on page load, because Transitions can't animate something from
auto
to##px
. - Set the
scrollHeight
dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)
Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.
var fgDiv = document.querySelector('#fgDiv');
var fgContent = document.querySelector('#fgContent');
var rtButton = document.querySelector('#rightButton');
updateDimesions(fgContent, fgContent); // First update of the dimensions
function rightClick() {
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgContent.innerHTML = nextDiv.innerHTML;
rtButton.textContent = nextButton;
updateDimesions(fgContent, nextDiv); // Second update of the dimensions
};
function updateDimesions(el, replace) {
el.style.width = replace.scrollWidth + 'px';
el.style.height = replace.scrollHeight + 'px';
}
.foreground {
width: 33%;
background: whitesmoke;
}
.foreground-inner {
overflow: hidden;
/* Hide the content initially */
-moz-transition: all 1s ease;
-webkit-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
}
.hidden {
height: 0;
overflow: hidden;
}
.hidden > div {
overflow: hidden;
/* This accounts for margin in ".scrollHeight" */
padding: 0;
/* Padding messes with .scrollHeight */
}
<div class="foreground" id="fgDiv">
<div id="fgContent" class="foreground-inner">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
<p>
<div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
<div class="hidden">
<div id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
</div>
</div>
</div>
Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
– toby.in.dresden
Nov 17 at 22:22
@toby.in.dresden Gotcha. Updated.
– Chris Happy
Nov 17 at 22:51
I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
– toby.in.dresden
Nov 18 at 22:47
@toby.in.dresden Your hidden divs can havedisplay: none
, otherwise, JS will think it hasheight: 0
. In my solution, I hid them by wrapping the content with a div that hasheight: 0; overflow: hidden;
.
– Chris Happy
Nov 19 at 22:57
add a comment |
up vote
0
down vote
Use CSS transitions
, (as you have done) and JS .scrollHeight
.
Longer explanation:
- Set the dimensions on page load, because Transitions can't animate something from
auto
to##px
. - Set the
scrollHeight
dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)
Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.
var fgDiv = document.querySelector('#fgDiv');
var fgContent = document.querySelector('#fgContent');
var rtButton = document.querySelector('#rightButton');
updateDimesions(fgContent, fgContent); // First update of the dimensions
function rightClick() {
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgContent.innerHTML = nextDiv.innerHTML;
rtButton.textContent = nextButton;
updateDimesions(fgContent, nextDiv); // Second update of the dimensions
};
function updateDimesions(el, replace) {
el.style.width = replace.scrollWidth + 'px';
el.style.height = replace.scrollHeight + 'px';
}
.foreground {
width: 33%;
background: whitesmoke;
}
.foreground-inner {
overflow: hidden;
/* Hide the content initially */
-moz-transition: all 1s ease;
-webkit-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
}
.hidden {
height: 0;
overflow: hidden;
}
.hidden > div {
overflow: hidden;
/* This accounts for margin in ".scrollHeight" */
padding: 0;
/* Padding messes with .scrollHeight */
}
<div class="foreground" id="fgDiv">
<div id="fgContent" class="foreground-inner">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
<p>
<div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
<div class="hidden">
<div id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
</div>
</div>
</div>
Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
– toby.in.dresden
Nov 17 at 22:22
@toby.in.dresden Gotcha. Updated.
– Chris Happy
Nov 17 at 22:51
I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
– toby.in.dresden
Nov 18 at 22:47
@toby.in.dresden Your hidden divs can havedisplay: none
, otherwise, JS will think it hasheight: 0
. In my solution, I hid them by wrapping the content with a div that hasheight: 0; overflow: hidden;
.
– Chris Happy
Nov 19 at 22:57
add a comment |
up vote
0
down vote
up vote
0
down vote
Use CSS transitions
, (as you have done) and JS .scrollHeight
.
Longer explanation:
- Set the dimensions on page load, because Transitions can't animate something from
auto
to##px
. - Set the
scrollHeight
dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)
Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.
var fgDiv = document.querySelector('#fgDiv');
var fgContent = document.querySelector('#fgContent');
var rtButton = document.querySelector('#rightButton');
updateDimesions(fgContent, fgContent); // First update of the dimensions
function rightClick() {
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgContent.innerHTML = nextDiv.innerHTML;
rtButton.textContent = nextButton;
updateDimesions(fgContent, nextDiv); // Second update of the dimensions
};
function updateDimesions(el, replace) {
el.style.width = replace.scrollWidth + 'px';
el.style.height = replace.scrollHeight + 'px';
}
.foreground {
width: 33%;
background: whitesmoke;
}
.foreground-inner {
overflow: hidden;
/* Hide the content initially */
-moz-transition: all 1s ease;
-webkit-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
}
.hidden {
height: 0;
overflow: hidden;
}
.hidden > div {
overflow: hidden;
/* This accounts for margin in ".scrollHeight" */
padding: 0;
/* Padding messes with .scrollHeight */
}
<div class="foreground" id="fgDiv">
<div id="fgContent" class="foreground-inner">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
<p>
<div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
<div class="hidden">
<div id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
</div>
</div>
</div>
Use CSS transitions
, (as you have done) and JS .scrollHeight
.
Longer explanation:
- Set the dimensions on page load, because Transitions can't animate something from
auto
to##px
. - Set the
scrollHeight
dimensions using the element you're going to replace it with. (Note: For accurate dimensions, the element must have overflow: hidden` to account for margin and no padding.)
Finally, to hide the elements, you could put inside the wrapper and hide it with a parent.
var fgDiv = document.querySelector('#fgDiv');
var fgContent = document.querySelector('#fgContent');
var rtButton = document.querySelector('#rightButton');
updateDimesions(fgContent, fgContent); // First update of the dimensions
function rightClick() {
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgContent.innerHTML = nextDiv.innerHTML;
rtButton.textContent = nextButton;
updateDimesions(fgContent, nextDiv); // Second update of the dimensions
};
function updateDimesions(el, replace) {
el.style.width = replace.scrollWidth + 'px';
el.style.height = replace.scrollHeight + 'px';
}
.foreground {
width: 33%;
background: whitesmoke;
}
.foreground-inner {
overflow: hidden;
/* Hide the content initially */
-moz-transition: all 1s ease;
-webkit-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
}
.hidden {
height: 0;
overflow: hidden;
}
.hidden > div {
overflow: hidden;
/* This accounts for margin in ".scrollHeight" */
padding: 0;
/* Padding messes with .scrollHeight */
}
<div class="foreground" id="fgDiv">
<div id="fgContent" class="foreground-inner">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
<p>
<div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
<div class="hidden">
<div id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
</div>
</div>
</div>
var fgDiv = document.querySelector('#fgDiv');
var fgContent = document.querySelector('#fgContent');
var rtButton = document.querySelector('#rightButton');
updateDimesions(fgContent, fgContent); // First update of the dimensions
function rightClick() {
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgContent.innerHTML = nextDiv.innerHTML;
rtButton.textContent = nextButton;
updateDimesions(fgContent, nextDiv); // Second update of the dimensions
};
function updateDimesions(el, replace) {
el.style.width = replace.scrollWidth + 'px';
el.style.height = replace.scrollHeight + 'px';
}
.foreground {
width: 33%;
background: whitesmoke;
}
.foreground-inner {
overflow: hidden;
/* Hide the content initially */
-moz-transition: all 1s ease;
-webkit-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
}
.hidden {
height: 0;
overflow: hidden;
}
.hidden > div {
overflow: hidden;
/* This accounts for margin in ".scrollHeight" */
padding: 0;
/* Padding messes with .scrollHeight */
}
<div class="foreground" id="fgDiv">
<div id="fgContent" class="foreground-inner">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
<p>
<div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
<div class="hidden">
<div id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
</div>
</div>
</div>
var fgDiv = document.querySelector('#fgDiv');
var fgContent = document.querySelector('#fgContent');
var rtButton = document.querySelector('#rightButton');
updateDimesions(fgContent, fgContent); // First update of the dimensions
function rightClick() {
if (rtButton.textContent === "What's a Grimmage?") {
nextButton = "How's it work?";
nextDiv = document.querySelector('#whatIsIt');
}
// I removed a handful of similar if statements.
fgContent.innerHTML = nextDiv.innerHTML;
rtButton.textContent = nextButton;
updateDimesions(fgContent, nextDiv); // Second update of the dimensions
};
function updateDimesions(el, replace) {
el.style.width = replace.scrollWidth + 'px';
el.style.height = replace.scrollHeight + 'px';
}
.foreground {
width: 33%;
background: whitesmoke;
}
.foreground-inner {
overflow: hidden;
/* Hide the content initially */
-moz-transition: all 1s ease;
-webkit-transition: all 1s ease;
-o-transition: all 1s ease;
transition: all 1s ease;
}
.hidden {
height: 0;
overflow: hidden;
}
.hidden > div {
overflow: hidden;
/* This accounts for margin in ".scrollHeight" */
padding: 0;
/* Padding messes with .scrollHeight */
}
<div class="foreground" id="fgDiv">
<div id="fgContent" class="foreground-inner">
<h1 id="foregroundTitle" class="headline">Make a Grimmage!</h1>
<p class="text">Balh.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
<p>
<div class="btn-group" style="position: relative; margin: auto;"><a href="#" class="btn btn-primary">Login / Sign up</a><button id="rightButton" onclick="rightClick()" class="btn btn-info">What's a Grimmage?</button></div>
</p>
<div class="hidden">
<div id="whatIsIt">
<h1 class="headline">What is a Grimmage?</h1>
<p class="text">More blah.</p>
<p class="text">So much blah.</p>
</div>
</div>
</div>
</div>
edited Nov 17 at 22:50
answered Nov 17 at 21:43
Chris Happy
4,3881833
4,3881833
Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
– toby.in.dresden
Nov 17 at 22:22
@toby.in.dresden Gotcha. Updated.
– Chris Happy
Nov 17 at 22:51
I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
– toby.in.dresden
Nov 18 at 22:47
@toby.in.dresden Your hidden divs can havedisplay: none
, otherwise, JS will think it hasheight: 0
. In my solution, I hid them by wrapping the content with a div that hasheight: 0; overflow: hidden;
.
– Chris Happy
Nov 19 at 22:57
add a comment |
Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
– toby.in.dresden
Nov 17 at 22:22
@toby.in.dresden Gotcha. Updated.
– Chris Happy
Nov 17 at 22:51
I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
– toby.in.dresden
Nov 18 at 22:47
@toby.in.dresden Your hidden divs can havedisplay: none
, otherwise, JS will think it hasheight: 0
. In my solution, I hid them by wrapping the content with a div that hasheight: 0; overflow: hidden;
.
– Chris Happy
Nov 19 at 22:57
Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
– toby.in.dresden
Nov 17 at 22:22
Thanks very much! It's already much better, but it only animates when getting larger. When the content is smaller, it simply remains large. (Also, weirdly, the 'var' bit didn't work. It kept throwing an error in the console to the extent that it couldn't read the height of 'null'.)
– toby.in.dresden
Nov 17 at 22:22
@toby.in.dresden Gotcha. Updated.
– Chris Happy
Nov 17 at 22:51
@toby.in.dresden Gotcha. Updated.
– Chris Happy
Nov 17 at 22:51
I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
– toby.in.dresden
Nov 18 at 22:47
I tried to copy this as slavishly as I could, but it seems that the replacement Divs have a height of 0. The div animates down to just the row of buttons.
– toby.in.dresden
Nov 18 at 22:47
@toby.in.dresden Your hidden divs can have
display: none
, otherwise, JS will think it has height: 0
. In my solution, I hid them by wrapping the content with a div that has height: 0; overflow: hidden;
.– Chris Happy
Nov 19 at 22:57
@toby.in.dresden Your hidden divs can have
display: none
, otherwise, JS will think it has height: 0
. In my solution, I hid them by wrapping the content with a div that has height: 0; overflow: hidden;
.– Chris Happy
Nov 19 at 22:57
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355649%2fanimate-change-in-size-of-div-when-content-changes%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown