【发布时间】:2020-11-02 00:40:59
【问题描述】:
我想在我的页面中有一个水平布局......每当用户滚动页面而不是从上到下时 布局我想要从左到右的布局 ...我的代码在大屏幕上,最后一节之后和小屏幕上都有空白空间 最后一部分不会出现。
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>
【问题讨论】:
标签: javascript html css