【发布时间】:2017-07-01 08:52:56
【问题描述】:
我尝试制作带有视差的文本但失败了。代码看似无害,似乎没有做错什么,但滚动的外观和感觉却大错特错。
标记是这样的:
<!-- content above -->
<section class="section-parallax">
<div class="container text-center">
<div class="flex-row-columns">
<div class="flex-row">
<h2 class="heading flex-8">
<span class="heading-sub">Some Header</span>
<span class="heading-bottom">getting also long<sup>
</h2>
</div>
</div>
</div>
</section>
<!-- content below -->
这样的样式:
.container {
box-sizing: border-box;
margin-left: auto;
margin-right: auto;
padding-left: 15px;
padding-right: 15px;
width: 1400px;
}
.text-center {
text-align: center;
}
.section-parallax {
background: black;
overflow: hidden;
&,
& .container,
& .flex-row {
min-height: 545px;
}
& .container {
transform: translate3d(0, -100%, 0);
}
}
.flex-row-columns {
display: flex;
flex-direction: column;
margin-top: 0;
margin-bottom: 0;
margin-left: 0;
margin-right: 0;
}
.flex-row {
align-items: center;
display: flex;
justify-content: center;
margin-left: -15px;
margin-right: -15px;
}
.flex-8 {
flex-basis: 66.66666666666667%;
padding-left: 15px;
padding-right: 15px;
}
.heading {
color: white;
font-size: 54px;
letter-spacing: .66px;
line-height: 1.273em;
}
.heading-sub {
display: block;
margin-bottom: 20px;
}
最后我用的JS是这样的:
class ParallaxSection {
constructor() {
this.el = document.querySelector('.section-parallax');
this.els = {
container: this.el.querySelector('.container')
};
this.calcBounds();
window.addEventListener('scroll', this.onScroll.bind(this));
window.addEventListener('resize', () => {
this.calcBounds();
this.onScroll();
});
}
calcBounds() {
if (this.tween) {
this.tween.kill();
this.els.container.removeAttribute('style');
}
const rect = this.el.getBoundingClientRect();
const scrollY = ParallaxSection.getScroll();
this.start = (rect.top + scrollY) - (window.innerHeight * 0.75);
this.end = this.start + this.el.offsetHeight + window.innerHeight;
this.tween = TweenLite.fromTo(this.els.container, 1, {
css: {
force3D: true,
y: -this.el.offsetHeight
}
}, {
paused: true,
css: {
force3D: true,
y: this.end - this.start - this.el.offsetHeight
},
ease: Linear.easeNone
});
}
onScroll() {
const scroll = ParallaxSection.getScroll();
if (scroll >= this.start && scroll <= this.end) {
const diff = this.end - this.start;
const offset = scroll - this.start;
const perc = offset / diff;
this.tween.progress(perc);
}
}
static getScroll() {
return window.pageYOffset || document.documentElement.scrollTop;
}
}
const p = new ParallaxSection();
现在,奇怪的是,在尝试发现问题时,我将其放入 Pen 中,这样我就可以尝试查看它失败的地方,并且在 Pen 中似乎还不错。这导致我删除了页面上的所有元素并准确地复制了笔,结果发现,由于某种未知的原因,效果在 codepen 上是完美的并且失败了。
我已经下载了 codepen 生成的整个 HTML,它也有同样的体验。
Pen 在这里可见。
这有什么问题?
【问题讨论】:
-
你真的需要 GSAP 吗?您可以通过固定容器来复制这种效果。
-
您不需要为此使用 CSSPlugin,您可以直接为 'y' 属性设置动画。不过,我不会认为这足以让它变得“笨拙”。如果 codepen/local 之间的情况有所不同,则可能是您的容器之一
<div>上没有transform: translateZ(0)而 codepen 有?代码在我看来是合理的。 -
对您来说是一直都不好,还是只是在您到达视差部分时?
-
效果在视差部分似乎是错误的。无论如何,滚动时的 FPS 似乎一直是 60。在此之后 Codepen 现在似乎也出错了:twitter.com/CodePen/status/845351286816096256 using translate3d 所以它可以更流畅。
-
@AntonioLaguna 当你说你已经下载时,你的意思是你使用了导出功能吗?导出后我仍然无法复制问题,可能是您的计算机在直接访问文件时速度很慢?您是否尝试在本地服务器上运行它以查看它是否仍然出现?
标签: javascript performance parallax gsap