Jump to content
Search Community

ScrollTrigger - I am Creating Horizontall scroll animation with scrolltrigger but some issue with starting and ending

Fallen test
Moderator Tag

Recommended Posts

Hii i am doing scroll trigger animation in react 

import React, { useEffect, useRef } from "react";
import gsap from "gsap";
import ScrollTrigger from 'gsap/ScrollTrigger';


const webDesign = "https://assets-global.website-files.com/63f8ca18cae25871c18fd72d/64062922a09751a1098ce054_web-design-agency.png";

function Cardsitem1() {
    return (
        <div className="card-1 one cards">
            <div className="left-container">
                <h3>Web Design</h3>
                <p>We design websites that look amazing, convey the right brand message, and are highly oriented for conversion.</p>
                <a href="#">Learn more</a>
            </div>
            <div className="right image-container">
                <img src={webDesign} alt="Web Design" />
            </div>
        </div>
    );
}
function Cardsitem2() {
    return (
        <div className="card-1  two cards">
            <div className="left-container">
                <h3>Web Development</h3>
                <p>We design websites that look amazing, convey the right brand message, and are highly oriented for conversion.</p>
                <a href="#">Learn more</a>
            </div>
            <div className="right image-container">
                <img src={webDesign} alt="Web Design" />
            </div>
        </div>
    );
}
function Cardsitem3() {
    return (
        <div className="card-1  three cards">
            <div className="left-container">
                <h3>Web Design</h3>
                <p>We design websites that look amazing, convey the right brand message, and are highly oriented for conversion.</p>
                <a href="#">Learn more</a>
            </div>
            <div className="right image-container">
                <img src={webDesign} alt="Web Design" />
            </div>
        </div>
    );
}
function Cardsitem4() {
    return (
        <div className="card-1  four cards">
            <div className="left-container">
                <h3>Web Design</h3>
                <p>We design websites that look amazing, convey the right brand message, and are highly oriented for conversion.</p>
                <a href="#">Learn more</a>
            </div>
            <div className="right image-container">
                <img src={webDesign} alt="Web Design" />
            </div>
        </div>
    );
}
function Cardsitem5() {
    return (
        <div className="card-1 five cards">
            <div className="left-container">
                <h3>Web Design</h3>
                <p>We design websites that look amazing, convey the right brand message, and are highly oriented for conversion.</p>
                <a href="#">Learn more</a>
            </div>
            <div className="right image-container">
                <img src={webDesign} alt="Web Design" />
            </div>
        </div>
    );
}
function Cardsitem6() {
    return (
        <div className="card-1 six cards" >
            <div className="left-container">
                <h3>Web Design</h3>
                <p>We design websites that look amazing, convey the right brand message, and are highly oriented for conversion.</p>
                <a href="#">Learn more</a>
            </div>
            <div className="right image-container">
                <img src={webDesign} alt="Web Design" />
            </div>
        </div>
    );
}

const H3style = {
    color : "white"
}

function HeadingContent() {
    return(
        <div className="heading-content">
        <div className="left-content">
        <span style={H3style}>
            our services
        </span>
        <h3  style={H3style}> High-impact design &<br /> development services

</h3>
        </div>
        <div className="right-content">
        <a href="#" 
        className="button-primary white w-button">Get in touch <span className="line-rounded-icons button-icon">→</span></a>

        </div>
    </div>
    )
}


function Horizontalscroll  () {
    const containerRef = useRef();
  
    useEffect(() => {
      gsap.registerPlugin(ScrollTrigger);
      const cards = gsap.utils.toArray(".cards");
      gsap.to(cards, {
        xPercent: -100 * (cards.length - 1),
        ease: "none",
        scrollTrigger: {
          trigger: ".scroll-container",
          pin: true,
          scrub: 1,
          snap: 1 / (cards.length - 1),
          end: () => "+=" + containerRef.current.offsetWidth,
        },
      });
    }, []);

    return (
        <section  ref={containerRef} className="scroll-container">
            <div>
                <HeadingContent /> </div>
            <div className="cards-contaiener"> 
            <Cardsitem1 />
            <Cardsitem2 />
            <Cardsitem3 />
            <Cardsitem4 />
            <Cardsitem5 />
            <Cardsitem6 />
            </div>

        </section>
    );
}

export default Horizontalscroll;
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}


/* src/App.css */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;700&family=Lato:wght@400;700&display=swap');

h2, h3 {
  font-family: 'Poppins', sans-serif;
  font-size: 2.302rem;
  font-weight: 600;
  padding: 0;
  margin: 0;
  letter-spacing: .4px;
}
.left-content span h3 {
  color: #fff;
  font-family: 'Poppins', sans-serif;
}

code {
  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
    monospace;
}
.scroll-container{
  background: #2388FF;
  width: 100%;
  height: 100vh;
  padding: 110px 0px;
  overflow: hidden;
}
.cards-contaiener{
  grid-column-gap: 28px;
    flex-direction: row;
    display: flex;
    padding-left: 100px;
  
}
.card-1{
  flex-direction: row;
  flex: none;
  width: 70%;
  height: 50vh;
  
  display: flex;
  justify-content: space-around;
  align-items: center;
  background: #fff;
  overflow: hidden;
    transform: translate(0);
    border-radius: 32px;
}
.image-container img{
  width: 100%;
  height: 100%;
  background-size: cover;
}
.left-container, .right{
  width: 50%;
  height: auto;
}
.left-container{
  flex-direction: column;
    justify-content: center;
    align-self: stretch;
    padding: 60px 54px;
    display: flex;
}
.left-container h3{
  color: #19213d;
    margin-bottom: 6px;
    
}
.left-container p{
  color: #667097;
  font-family: 'Poppins', sans-serif;
    font-size: 18px;
    line-height: 30px;
    letter-spacing: .4px;
}
.heading-content{
  display: flex;
  justify-content: space-between;
  align-items: end;
  padding: 0;
  width: 85%;
  height: auto;
  margin-left: auto;
  margin-right: auto;
}
.button-primary.white {
  border-color: #fff;
  background-color: #fff;
  box-shadow: 0 2px 6px 0 rgba(20, 20, 43, .04);
  color: #2388ff;
  transition: transform .3s, box-shadow .3s;
  text-align: center;
  transform-style: preserve-3d;
  border-radius: 64px;
  padding: 26px 38px 24px;
  font-size: 18px;
  font-weight: 700;
  line-height: 20px;
  text-decoration: none;
  position: relative;
  overflow: hidden;
  border: 1px solid #fff;
  display: inline-block; /* Ensure it behaves like a button */
}

.button-primary.white:hover {
  transform: translateY(-3px); /* Add a hover effect for better UX */
  box-shadow: 0 4px 12px 0 rgba(20, 20, 43, .1); /* Enhance hover effect */
}

.button-icon {
  margin-left: 8px; /* Add some spacing before the icon */
  transition: margin-left .3s; /* Smooth transition for the icon movement */
}

.button-primary.white:hover .button-icon {
  margin-left: 16px; /* Move the icon to the right on hover */
}
.left-content{
  display: flex;
  flex-direction: column;
  gap: 0px;
  padding: 0;
}
.left-content span{
  letter-spacing: .06em;
    text-transform: uppercase;
    margin-bottom: 14px;
    font-size: 16px;
    font-weight: 700;
    line-height: 18px;
}
.left-content h3{
  font-size: 42px;
  font-weight: 700;
  line-height: 52px;
    letter-spacing: .4px;
  margin: 0;
  padding-bottom: 10px;
}
.right-content{
  padding-bottom: 10px;
}
.left-container a{
  font-size: 16px;
  line-height: 18px;
}
.left-container a{
  color: #ff2d46;
  letter-spacing: .06em;
  text-transform: uppercase !important;
  background-color: rgba(0, 0, 0, 0);
  align-items: center;
  padding: 0;
  font-weight: 700;
  line-height: 20px;
  transition: color .3s;
}

This is the output video   url :- https://webcomponents.webbeesolutions.com/wp-content/uploads/2024/06/scrolltrigger-issue.mp4 

 

starting with blank screen  and and actuall section coming  very quickly and  after finishing animation again showing white screen there is not content, only animation container only there 

why happing like these can anyone know me please....

Edited by Fallen
i forgot to add css code
Link to comment
Share on other sites

Hi there! I see you're using React -

Proper cleanup is very important with frameworks, but especially with React. React 18 runs in strict mode locally by default which causes your useEffect() and useLayoutEffect() to get called TWICE.

 

Since GSAP 3.12, we have the useGSAP() hook (the NPM package is here) that simplifies creating and cleaning up animations in React (including Next, Remix, etc). It's a drop-in replacement for useEffect()/useLayoutEffect(). All the GSAP-related objects (animations, ScrollTriggers, etc.) created while the function executes get collected and then reverted when the hook gets torn down.

 

Here is how it works:

const container = useRef(); // the root level element of your component (for scoping selector text which is optional)

useGSAP(() => {
  // gsap code here...
}, { dependencies: [endX], scope: container }); // config object offers maximum flexibility

Or if you prefer, you can use the same method signature as useEffect():

useGSAP(() => {
  // gsap code here...
}, [endX]); // simple dependency Array setup like useEffect()

This pattern follows React's best practices.

 

We strongly recommend reading the React guide we've put together at https://gsap.com/resources/React/

 

If you still need help, here's a React starter template that you can fork to create a minimal demo illustrating whatever issue you're running into. Post a link to your fork back here and we'd be happy to take a peek and answer any GSAP-related questions you have. Just use simple colored <div> elements in your demo; no need to recreate your whole project with client artwork, etc. The simpler the better. 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...