我正在尝试使用ScrollMagic&在react中的gsap库,一切都很好,直到我尝试构建我的代码,我尝试了我在网上能找到的所有导入解决方案,但它的行为似乎不一样。
我创建了一个卡片列表,当到达某个部分时,它会水平滚动,在开发版本中,它的行为完全是这样,但当我在生产版本中(构建后(,它会以对角线方式滚动。
以下是我的一些代码以及我如何安装库:
安装gsap:
npm i gsap
安装滚动魔术:
npm i scrollmagic
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;
}
导致这种不需要的行为的问题是css上的perspective
元素,在使用三维动画时需要添加透视图,但在这种情况下,不应该使用它,因为动画只是一个二维滚动,它将垂直滚动转换为水平滚动,我花了几天时间才弄清楚这一点,我希望它能帮助其他人制作出这么好的动画。我的代码还有一些其他问题,但我修复了所有问题,并希望与大家分享,这样它就可以作为任何想要制作类似内容的人的参考。
请在下面找到我的代码的最终版本,并在CodePen:上进行实时演示
由我创建的"React+ScrollMagic+Gsap:Horizontal with pined title to top"的现场演示。