Skip to content

Instantly share code, notes, and snippets.

@pigeonfresh
Created February 25, 2021 12:54
Show Gist options
  • Save pigeonfresh/7adefc50a563ce0e6a7d4eebebc9cead to your computer and use it in GitHub Desktop.
Save pigeonfresh/7adefc50a563ce0e6a7d4eebebc9cead to your computer and use it in GitHub Desktop.
Parallax Visual Example
<link rel="stylesheet" href="./visual.scss">
<script src="./Visual.ts"></script>
<div class='visual' data-component="visual">
<picture>
<img src='{{src}}' alt='{{alt}}'>
</picture>
</div>
[data-component="visual"] {
overflow: hidden;
position: relative;
@include aspect-ratio(16, 9);
picture {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
}
import { gsap } from 'gsap';
import ScrollTrigger from 'gsap/ScrollTrigger';
import AbstractComponent from '../AbstractComponent';
gsap.registerPlugin(ScrollTrigger);
export default class Visual extends AbstractComponent {
public static readonly displayName: string = 'visual';
private static readonly offset: number = 200;
private picture: HTMLPictureElement | null = this.getElement('picture');
constructor(el: HTMLElement) {
super(el);
this.createParallaxFX();
}
private createParallaxFX = () => {
if (!this.picture) return;
const offset = Visual.offset;
this.picture.style.bottom = `-${offset}px`;
ScrollTrigger.create({
trigger: this.picture,
scrub: 1,
animation: gsap.to(this.picture, {
y: -offset,
})
})
};
public dispose() {
//
super.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment