reactjsgsapscrollmagic

Build version of scrollmagic and gsap in react not the same as dev version


I'm trying to use ScrollMagic & gsap libraries in react, everything was fine until I tried to build my code, I tried all the import solutions I could find on the web but it doesn't seem to behave the same way.

I created a list of cards that scrolls horizontally when some section is reached, on the dev version it behaves exactly that way but when I'm in a production version (after build) it scrolls in diagonal way.

Here's some of my code and how I installed the libraries:

installing gsap:

npm i gsap

installing scrollmagic:

npm i scrollmagic

my code in react:

import React, { Component } from 'react';
import ReactDOM from 'react-dom'
import { TweenMax, TimelineLite, TimelineMax, TweenLite, Linear } from "gsap/all"; // Also works with TweenLite and TimelineLite
import * as ScrollMagic from "scrollmagic/scrollmagic/uncompressed/ScrollMagic"; // Or use scrollmagic-with-ssr to avoid server rendering problems
import { ScrollMagicPluginGsap } from "scrollmagic-plugin-gsap";
import './listCardsH.css';
import Image from '../images/Image';
import Titles from '../titles/Titles';

ScrollMagicPluginGsap(ScrollMagic, TweenMax, TimelineLite);

class ListCardsH extends Component {


    componentDidMount (){
        TweenLite.defaultEase = Linear.easeNone;
        var titles = ReactDOM
                    .findDOMNode(this.refs['title1'])
                    .getBoundingClientRect()
        console.log(titles)
        var controller = new ScrollMagic.Controller();

        // var tl = new TimelineLite();
        // tl.to("#js-slideContainer", 1, {x:'-100%'})
        var tl = new TimelineMax()
            .add(TweenMax.to('#js-slideContainer', 1, {x: '-70%'}))

            new ScrollMagic.Scene({
                triggerElement: '#bigTrigger',
                triggerHook: 0,
                duration: '100%'
            })
            .setPin("#titleId", {pushFollowers: false})
            .addTo(controller);


        new ScrollMagic.Scene({
        triggerElement: "#bigTrigger",
        triggerHook: 0  ,
        duration: "100%"
        })
        .setPin("#js-slideContainer")
        .setTween(tl)
        .addTo(controller);   



}

    render() {
        return (
            <div className="containerWrapper" id="bigTrigger">
                <div className='bigTitle' id='titleId'>
                    <Titles></Titles>
                </div>
                <div className="wrapper" id="js-wrapper" >
                    <div className="sections " id="js-slideContainer">
                        <div className='childCardH section' ref='title1'>
                            <div className='sectionTitle'>
                                <Image  srcImg='https://www.inform.kz/radmin/news/2019/11/03/191103143718887e.jpg'></Image>
                            </div>
                        </div>
                        <div className='childCardH section' ref='title1'>
                            <div className='sectionTitle'>
                                <Image srcImg='https://www.inform.kz/radmin/news/2019/11/03/191103143718887e.jpg'></Image>
                            </div>
                        </div>
                        <div className='childCardH section' ref='title1'>
                            <div className='sectionTitle'>
                                <Image srcImg='https://www.inform.kz/radmin/news/2019/11/03/191103143718887e.jpg'></Image>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        );
    }
}

export default ListCardsH;

CSS

.wrapper {
  width: 130%;
  height: 100%;
  perspective: 1000;
  -webkit-perspective: 1000;
  perspective-origin: 0;
  -webkit-perspective-origin: 0;
  overflow: hidden;
}

.section {
  height: 120%;
  width: calc( 100% / 3);
  float: left;
  position: relative;

}
.sections {
  width: 100%;
  height: 50vh; 
  /* Change this height to move the images up and down */
}
.sectionTitle {
  position: absolute;
  width: 40vw;
  top: 50%;
  left: 120%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  font-size: 30px;
  color: #fff;
}

.bigTitle {
  width: 100vw;
  text-align:center;
  padding-right: 10vw;
}

Solution

  • The problem that was causing that unwanted behavior was the perspective element on the css, adding a perspective is necessary when using a 3d animation but in this case it should be not used since the animation is just a 2d scroll, that transforms the verticall scroll to a horizontal one, it took me couple of days to figure this out and I hope it will help others build such nice animations. There were some other issues with my code but I fixed everything and wanted to share this so it can be a reference to anyone who would want to make something similar.

    Please find down below the final version of my code with a live demo on CodePen:

    Live Demo Of 'React + ScrollMagic + Gsap : Horizontal with pined title to top' created by me.