javascript - 水平滚动轮播导航问题
问题描述
我有一个使用 nuxt /vue 和 gsap / scrolltrigger 的水平轮播滑块。一切都按预期工作,但是当我尝试使用导航控制滑块时,它非常笨拙。它不会滑到正确的面板,不确定我在哪里犯了错误,但如果有人有任何想法,我将不胜感激!
https://codepen.io/mDDDD/pen/QWvqVzV
<div class="grand-carousel-container" id="pinInnovationCarousel">
<div class="container__timeline">
<div class="timeline-nav">
<ul class="timeline-nav__list">
<li class="is-active timeline-nav__list-item">Slide One</li>
<li class="timeline-nav__list-item">Slide Two</li>
<li class="timeline-nav__list-item">Slide Three</li>
</ul>
</div>
<div class="timeline-slides">
<div class="slide">
Slide One
</div>
<div class="slide">
Slide Two
</div>
<div class="slide">
Slide Three
</div>
</div>
</div>
</div>
.container__timeline,
.grand-carousel-panel {
overflow: hidden;
width: 100%;
max-width: 1250px;
}
.container__timeline {
position: relative;
}
.timeline-nav {
display: flex;
justify-content: flex-end;
max-width: 100px;
}
.timeline-nav__list {
position: relative;
display: flex;
flex-direction: column;
justify-content: space-between;
width: 100%;
min-height: 175px;
cursor: pointer;
}
.timeline-nav__list-item {
position: relative;
margin-right: 15px;
padding-bottom: 20px;
&:after {
position: absolute;
top: 0;
right: 0;
width: 5px;
height: 55px;
content: '';
border-radius: 5px;
background-color: grey;
}
&.is-active {
position: relative;
color: green;
&::after {
position: absolute;
z-index: 1;
top: 0;
right: 0px;
width: 5px;
height: 55px;
content: '';
border-radius: 5px;
background-color: green;
}
}
}
.slide {
display: flex;
align-items: flex-end;
flex: 1 0 auto;
width: 100vw;
}
.timeline-slides {
display: flex;
justify-content: space-between;
width: 100vw;
}
.slide:nth-child(1){
background-color: lightblue;
}
.slide:nth-child(2){
background-color: black;
color: white;
}
.slide:nth-child(3){
background-color: purple;
color: white;
}
const sections = gsap.utils.toArray('.slide');
const numSections = sections.length - 1;
const snapVal = 1 / numSections;
let lastIndex = 0;
const navList = document.querySelectorAll('.timeline-nav__list-item');
gsap.to(sections, {
xPercent: -100 * numSections,
ease: 'none',
scrollTrigger: {
id: 'innovationPin',
scrub: true,
snap: snapVal,
trigger: '#pinInnovationCarousel',
pin: true,
start: 'top top',
end: () => '+=' + innerWidth * sections.length,
onUpdate: (self) => {
const newIndex = Math.round(self.progress / snapVal);
if (newIndex !== lastIndex) {
navList[lastIndex].classList.remove('is-active');
navList[newIndex].classList.add('is-active');
lastIndex = newIndex;
}
},
},
});
navList.forEach((a, i) => {
a.addEventListener('click', (e) => {
e.preventDefault();
gsap.to(window, {
scrollTo: {
y: i * innerWidth,
},
});
});
});
解决方案
推荐阅读
- r - ggplot 标签用“K”表示数千或“M”表示数百万(维护“逗号”y 轴标签)
- excel - Excel VBA:如何避免代码重复 - 类似属性
- javascript - 如何使 JavaScript Bingo 生成器不重复相同的结果?
- c - C中N个进程之间的管道输出
- python - 我正在尝试将 selenium 用于 python,它返回一个“常见异常”错误
- python - 查找根据滚动高度显示和隐藏的元素 - python-selbium-webdriver
- android - LiveData 的单元测试挂起功能失败,但 Flow 通过
- mongodb - 将 Mongo DB 集合字段从对象更新为对象数组
- javascript - onstorage 事件不会被触发
- python - 如何自动化文本文件的格式以使列表中最长字符串的长度成为字符数。对于该变量