I want to have a horizontal layout in my page ... whenever user scrolls the page instead of top-to-bottom layout I want to have left-to-right layout ... with my code on big screens there is empty space after last section and on small screens last section won't appear.
calcBodyHeight();
function calcBodyHeight() {
const sections = document.querySelectorAll('.section');
let height = null;
sections.forEach(section => {
height += section.offsetWidth;
})
document.body.style.height = `${height}px`;
}
const container = document.querySelector('.container');
window.addEventListener('scroll', e => {
const scroll = window.scrollY;
container.style.left = `-${scroll}px`;
});
html,
body {
height: 100%;
}
body {
overflow-x: hidden;
overflow-y: auto;
}
.container {
width: fit-content;
height: 100vh;
display: flex;
position: fixed;
left: 0;
top: 0;
}
.container .section {
width: 100vw;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.container .section:nth-child(1) {
background-color: cornflowerblue;
}
.container .section:nth-child(2) {
background-color: coral;
}
.container .section:nth-child(3) {
background-color: lightgreen;
}
.container .section:nth-child(4) {
background-color: teal;
}
<div class="container">
<div class="section"></div>
<div class="section"></div>
<div class="section"></div>
<div class="section"></div>
</div>
The problem is that the initial height is missing from the calculation.
Let's take an example:
body.height: 800
body.width: 400
After calcBodyHeight
, body
height is 1600px (400 * 4) which means we need to have 1200px to scroll (the first 400px already presented). But, since body.height
is 800px this leaves us with only 800ox to scroll.
My suggestion is to calculate a bit different
body
height as section.length * section.height
calcBodyHeight();
function calcBodyHeight() {
const height = [...document.querySelectorAll('.section')]
.reduce((prev, curr) => prev + curr.offsetHeight, 0);
document.body.style.height = `${height}px`;
}
const container = document.querySelector('.container');
const ratio = window.innerWidth / window.innerHeight;
window.addEventListener('scroll', e => {
const scroll = window.scrollY * ratio;
container.style.left = `-${scroll}px`;
});
Demo
calcBodyHeight();
function calcBodyHeight() {
const height = [...document.querySelectorAll('.section')]
.reduce((prev, curr) => prev + curr.offsetHeight, 0);
document.body.style.height = `${height}px`;
}
const container = document.querySelector('.container');
const ratio = window.innerWidth / window.innerHeight;
window.addEventListener('scroll', e => {
const scroll = window.scrollY * ratio;
container.style.left = `-${scroll}px`;
});
html,
body {
height: 100%;
}
body {
overflow-x: hidden;
overflow-y: auto;
}
.container {
width: fit-content;
height: 100vh;
display: flex;
position: fixed;
left: 0;
top: 0;
}
.container .section {
width: 100vw;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.container .section:nth-child(1) {
background-color: cornflowerblue;
}
.container .section:nth-child(2) {
background-color: coral;
}
.container .section:nth-child(3) {
background-color: lightgreen;
}
.container .section:nth-child(4) {
background-color: teal;
}
<div class="container">
<div class="section"></div>
<div class="section"></div>
<div class="section"></div>
<div class="section"></div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With