Reputation: 1763
How can I add a smooth transition to the change in height when a new paragraph is appended inside a div? Example: https://jsbin.com/wahasikaki/edit?html,css,js,output
I know this may be possible with jQuery, but is it possible to do it using pure CSS?
Upvotes: 1
Views: 273
Reputation: 4946
It is not with pure CSS, but then you already add your paragraph with javascript. the problem is that in order to animate height in CSS you need the values. You will need javascript to extract these height values. What you can do is measure the height of your panel with scrollHeight before you add the paragraph, and next after you add the paragraph. You then change the height of your panel CSS.
Use an extra div inner-panel
with a position relative to wrap the paragraphs. this div must have a position property of relative.
I have also added some CSS
overflow:hidden;
function addParagraph() {
document.getElementById("panel").style.height = document.getElementById("inner-panel").scrollHeight + "px";
var newParagraph = document.createElement("p");
newParagraph.innerHTML = "League salmagundi Jack Tar wherry scuppers Gold Road bring a spring upon her cable grog blossom lanyard Yellow League salmagundi Jack Tar wherry scuppers Gold Road bring a spring upon her cable grog blossom lanyard Yellow ";
document.getElementById("inner-panel").appendChild(newParagraph);
console.log(document.getElementById("panel").scrollHeight);
document.getElementById("panel").style.height = document.getElementById("inner-panel").scrollHeight + "px";
}
function removeParagraph() {
var popParagraph = document.getElementById("inner-panel").lastChild;
popParagraph.parentNode.removeChild(popParagraph);
document.getElementById("panel").style.height = document.getElementById("inner-panel").scrollHeight + "px";
}
#panel {
position: absolute;
bottom: 0;
left: 0;
right: 0;
background: #000;
color: #fff;
font-family: Arial, sans-serif;
font-size: 0.875em;
transition: all 1s;
max-height: 95%;
overflow: hidden;
}
#inner-panel {
position: relative;
padding: 0 1em;
overflow: hidden;
}
p {
background: #333;
padding: 1em;
line-height: 150%;
border-radius: 4px;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<button onclick="addParagraph();">Add Paragraph</button>
<button onclick="removeParagraph();">Remove Paragraph</button>
<div id="panel">
<div id="inner-panel">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>
</div>
</body>
</html>
Upvotes: 3