Reputation: 175
I am trying to create a toggle in order to hide/show the inner footer. I need to calculate the height of this element and then subtract it
The main thing is accomodating for the changing height depending on the browsers width
My current code snippet is very wrong but I'm hoping someone can help me learn JS better
var footerheight = document.querySelector(".footer").offsetHeight;
var innerheight = document.querySelector(".inner").offsetHeight;
var sitefooter = document.querySelector(".footer");
var toggle = document.querySelector(".toggle");
toggle.addEventListener('click', function() {
sitefooter.style.bottom = (footerheight - innerheight);
});
footer {
position: fixed;
bottom:0;
left: 0;
width: 100%;
background: red;
}
footer div:first-of-type {
padding: 15px 50px;
border-bottom: 2px solid #000;
}
.inner {
padding: 50px;
}
<footer class="footer">
<div>
<button class="toggle">Footer toggle</button>
</div>
<div class="inner">
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.
</div>
</footer>
Upvotes: 1
Views: 529
Reputation: 6525
One approach is to store the height of the element onto the element itself via the dataset. Then whenever we update the height we can always revert to the height we saved. To make it a smooth animation you can add a CSS transition.
// Get all elements
const sitefooter = document.querySelector(".footer");
const inner = document.querySelector(".inner");
const toggle = document.querySelector(".toggle");
// Function to set a height to an element
const setElementHeight = (element, amount, unit = "px") => element.style.height = amount + unit;
// Save default heights onto elements
sitefooter.dataset.height = sitefooter.offsetHeight;
inner.dataset.height = inner.offsetHeight;
// Set default height for a smooth animation from the start
// Without this line the first toggle doesn't have an animation since you can't animate from height auto to a fixed value.
setElementHeight(sitefooter, sitefooter.offsetHeight);
// Add click event to toggle button
toggle.addEventListener("click", () => {
// If the footer expanded, collapse it otherwise extend it
sitefooter.offsetHeight == sitefooter.dataset.height
? setElementHeight(sitefooter, sitefooter.dataset.height - inner.dataset.height)
: setElementHeight(sitefooter, sitefooter.dataset.height);
});
footer {
position: fixed;
bottom:0;
left: 0;
width: 100%;
background: red;
/* For an animation effect */
transition: height 0.25s ease-in-out;
}
footer div:first-of-type {
padding: 15px 50px;
border-bottom: 2px solid #000;
}
.inner {
padding: 50px;
}
<footer class="footer">
<div>
<button class="toggle">Footer toggle</button>
</div>
<div class="inner">
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.
</div>
</footer>
Upvotes: 1