My webpage contains a div
element positioned in the middle of the content, with its height being adjustable through JavaScript code.
I am seeking a way to manage the scrolling behavior when the height of the div changes. Specifically, I want the content to always be pushed downwards and never upwards.
Currently, the content moves either up or down based on the scroll position when the button is triggered.
Below is a basic example:
function toggle(ev) {
const div = document.querySelector("div");
if (div.style.height === "336px") {
div.style.height = "147px";
} else {
div.style.height = "336px";
}
}
body {max-width: 60em; margin: auto;}
<p style="background-color: coral; height: 400px;"></p>
<div style="background-color:grey; height: 147px;"></div>
<button href="#" onclick="toggle()">toggle div size</button>
<p style="background-color: olive; height: 3000px;"></p>
To clarify, I am looking for a solution where the top paragraph remains fixed in its position when the div expands, regardless of the current button location on the window. This means that the bottom paragraph should adjust its position accordingly as well.