简体   繁体   English

滚动时的水平布局

[英]horizontal layout when scroll

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).之后calcBodyHeightbody高度为1600像素(400 * 4),这意味着我们需要有1200像素,以滚动(已经提出了第一个400像素)。 But, since body.height is 800px this leaves us with only 800ox to scroll.但是,由于body.height800px,这让我们只有800ox可以滚动。

My suggestion is to calculate a bit different我的建议是计算有点不同

  1. Set the body height as section.length * section.heightbody高度设置为section.length * section.height
  2. Get the window ratio (width / height)获取窗口比例(宽/高)
  3. On scroll, scroll according the ratio滚动时,按比例滚动
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>

Using pure CSS, we can create a container with items within the container.使用纯 CSS,我们可以创建一个包含容器内项目的容器。 Then rotate the container 90 degrees anti-clockwise and rotate the items 90 degrees clockwise to be the right way up.然后将容器逆时针旋转 90 度,将物品顺时针旋转 90 度以正确向上。

 .horizontal{ position:absolute; top:0; left:0; width: 200px; height: 500px; overflow-y: auto; overflow-x: hidden; transform: rotate(-90deg); transform-origin: right top; } .horizontal > div { width: 150px; height: 150px; transform: rotate(90deg); transform-origin: right top; background: green; border: 1px solid black; }
 <div class="horizontal"> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> <div>Item 4</div> <div>Item 5</div> <div>Item 6</div> <div>Item 7</div> <div>Item 8</div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM