gpt4 book ai didi

javascript - 在滚动上固定/修复 Vanilla Javascript 中的多个元素

转载 作者:行者123 更新时间:2023-11-29 23:22:51 25 4
gpt4 key购买 nike

我是 javascript 的初学者,我正在尝试使用 es6 规范构建一些东西。

我想在向下滚动页面时重新创建 ScrollMagic 的固定效果并固定不同的部分。

所以我有这个简单的 html 标记,其中包含页眉、页脚和 3 个部分:

<header class="forewords">
<h1>Some text</h1>
</header>

<div class="wrapper">
<section class="project" id="item1">this is section 1</section>
<section class="project" id="item2">this is section 2</section>
<section class="project" id="item3">this is section 3</section>
</div>

<footer class="endings">
<h1>some text</h1>
</footer>

我附加了一些样式来模拟现实情况。

这里是javascript逻辑:

获取所有项目:

const projects = Array.from(document.querySelectorAll('.project'));

获取所有项目距顶部的偏移量和所有项目的高度:

let projectsOffsetTop = projects.map(project => project.offsetTop);
let projectsHeight = projects.map(project => project.offsetHeight);

创建一个函数来在有人调整窗口大小时更新值:

function updateProjectsOffsetTop() {
projectsOffsetTop = projects.map(project => project.offsetTop);
projectsHeight = projects.map(project => project.offsetHeight);
};

window.addEventListener('resize', updateProjectsOffsetTop);

如果滚动大于其偏移量,则最终固定元素。

function pinElement() {

if (window.scrollY >= projectsOffsetTop[1]) {
document.body.style.paddingTop = projectsHeight[1] +'px';
projects[1].classList.add('fixed');
} else {
document.body.style.paddingTop = 0;
projects[1].classList.remove('fixed');
}

};

window.addEventListener('scroll', pinElement);

但我无法让它与所有项目元素一起使用。即使有 for 循环。最佳做法是什么?如果可能的话,我想在 Vanilla ES6 中解决这个问题。

在附件中找到完整的 js fiddle。

谢谢

const projects = Array.from(document.querySelectorAll('.project'));
let projectsOffsetTop = projects.map(project => project.offsetTop);
let projectsHeight = projects.map(project => project.offsetHeight);

function updateProjectsOffsetTop() {
projectsOffsetTop = projects.map(project => project.offsetTop);
projectsHeight = projects.map(project => project.offsetHeight);
};

function pinElement() {

if (window.scrollY >= projectsOffsetTop[1]) {
document.body.style.paddingTop = projectsHeight[1] +'px';
projects[1].classList.add('fixed');
} else {
document.body.style.paddingTop = 0;
projects[1].classList.remove('fixed');
}

};

window.addEventListener('resize', updateProjectsOffsetTop);
window.addEventListener('scroll', pinElement);
html {
box-sizing: border-box;

}

*, *::before, *::after {
box-sizing: inherit;
margin: 0;
padding: 0;
}

header, footer {
width: 100%;
padding: 10%;
background-color: grey;
position: relative;
}

.project {
width: 100%;
height: 100vh;
position: relative;
display: flex;
justify-content: center;
align-items: center;
top: 0;
}

#item1 {background-color: yellow;}
#item2 {background-color: blue;}
#item3 {background-color: red;}


.fixed {
position: fixed;
}
<header class="forewords"><h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum soluta ipsam quaerat cupiditate neque, necessitatibus amet nihil perferendis sunt minus! Exercitationem nulla inventore, aut beatae magnam, totam et minus hic.</h1>
</header>

<div class="wrapper">
<section class="project" id="item1">this is section 1</section>
<section class="project" id="item2">this is section 2</section>
<section class="project" id="item3">this is section 3</section>
</div>

<footer class="endings"><h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repudiandae vel, perferendis ullam totam recusandae sed repellendus cum! Molestiae, aut ut sequi eos quidem nam quo est, ad tempora inventore odit.</h1>
</footer>

最佳答案

您提供了令人惊叹的 MCVE 工作,非常感谢您花费如此多的精力和时间来提出一个很好的问题。好消息是您了!你的逻辑是合理的,一切都是有道理的。你真正缺少的是:

  • 重置样式的逻辑的正确位置(body 顶部填充和删除 fixed 类)
  • 获取最接近但大于滚动高度的 .project 元素的索引

您想在 pinElement() 方法中执行的操作如下:

  1. 首先重置/解除一切
  2. 获取大于 scrollY 但最接近它的 projectsOffsetTop 值(这样它将成为我们要固定的元素)
  3. 从中获取该值所属的 .project 元素的索引
  4. 如果索引为-1(即我们没有符合第 2 点条件的元素),返回并停止执行。
  5. 否则,我们将执行您在原始方法中的逻辑,但将 1 替换为我们在第 3 步中确定的索引。

考虑到这一点,这里是您稍微重构的 pinElement() 方法:

function pinElement() {

// Reset all styles
projects.forEach((project) => {
document.body.style.paddingTop = 0;
project.classList.remove('fixed');
});

// Get the index of the project that is closest to top
const valueClosestToScrollY = Math.max.apply(Math, projectsOffsetTop.filter((offsetTop) => offsetTop <= window.scrollY));
const idx = projectsOffsetTop.indexOf(valueClosestToScrollY);

// If index is not found, we don't do anything
if (idx === -1)
return;

// Otherwise, we set the appropriate styles and classes
if (window.scrollY >= projectsOffsetTop[idx]) {
document.body.style.paddingTop = `${projectsHeight[idx]}px`;
projects[idx].classList.add('fixed');
}

};

有趣的提示:您可以使用模板文字来做到这一点:

document.body.style.paddingTop = `${projectsHeight[idx]}px`;

...而不是这个:

document.body.style.paddingTop = ${projectsHeight[idx] + 'px';

这是一个概念验证示例:

const projects = Array.from(document.querySelectorAll('.project'));
let projectsOffsetTop = projects.map(project => project.offsetTop);
let projectsHeight = projects.map(project => project.offsetHeight);

function updateProjectsOffsetTop() {
projectsOffsetTop = projects.map(project => project.offsetTop);
projectsHeight = projects.map(project => project.offsetHeight);
};

function pinElement() {

// Reset all styles
projects.forEach((project) => {
document.body.style.paddingTop = 0;
project.classList.remove('fixed');
});

// Get the index of the project that is closest to top
const valueClosestToScrollY = Math.max.apply(Math, projectsOffsetTop.filter((offsetTop) => offsetTop <= window.scrollY));
const idx = projectsOffsetTop.indexOf(valueClosestToScrollY);

// If index is not found, we don't do anything
if (idx === -1)
return;

// Otherwise, we set the appropriate styles and classes
if (window.scrollY >= projectsOffsetTop[idx]) {
document.body.style.paddingTop = `${projectsHeight[idx]}px`;
projects[idx].classList.add('fixed');
}

};

window.addEventListener('resize', updateProjectsOffsetTop);
window.addEventListener('scroll', pinElement);
html {
box-sizing: border-box;
}

*,
*::before,
*::after {
box-sizing: inherit;
margin: 0;
padding: 0;
}

header,
footer {
width: 100%;
padding: 10%;
background-color: grey;
position: relative;
}

.project {
width: 100%;
height: 100vh;
position: relative;
display: flex;
justify-content: center;
align-items: center;
top: 0;
}

#item1 {
background-color: yellow;
}

#item2 {
background-color: blue;
}

#item3 {
background-color: red;
}

.fixed {
position: fixed;
}
<header class="forewords">
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Harum soluta ipsam quaerat cupiditate neque, necessitatibus amet nihil perferendis sunt minus! Exercitationem nulla inventore, aut beatae magnam, totam et minus hic.</h1>
</header>

<div class="wrapper">
<section class="project" id="item1">this is section 1</section>
<section class="project" id="item2">this is section 2</section>
<section class="project" id="item3">this is section 3</section>
</div>

<footer class="endings">
<h1>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repudiandae vel, perferendis ullam totam recusandae sed repellendus cum! Molestiae, aut ut sequi eos quidem nam quo est, ad tempora inventore odit.</h1>
</footer>


附带说明一下,出于性能原因,您可能需要研究对滚动事件进行节流/去抖动,以免过度调用 pinElement()

关于javascript - 在滚动上固定/修复 Vanilla Javascript 中的多个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50137155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com