简体   繁体   English

在 react.js 中使用 translateY() 反向滚动 Div

[英]Divs Reverse scroll with translateY() in react.js

I'm trying to create with React.js a type of scroll like this one: http://spassky-fischer.fr/ , where two divs are scrolling in inverse directions.我正在尝试使用 React.js 创建一种像这样的滚动: http ://spassky-fischer.fr/ ,其中两个 div 以相反的方向滚动。 They are using transform: translateY() , and I tried to implement it as well but I don't get where I'm wrong here.他们正在使用transform: translateY() ,我也尝试实现它,但我不明白我错在哪里。 Here's the architecture of the projet.这是项目的架构。 The current version is also here: http://noiseless-tendency.surge.sh/当前版本也在这里: http : //noiseless-tendency.surge.sh/

App.js : App.js

ComponentDidMount(){
    window.addEventListener("scroll", this.scrollHandler);
} 

...

scrollHandler = () => {
    this.setState({
      scrollPositionY: window.scrollY
    })
}

...

render() {
    return (
      <div className="App">
          <MainItemsContainer {...this.state} />
      </div>
    );
  }

MainItemsContainer.js : MainItemsContainer.js

render() {

    let style_first = {
      transform: `translateY(${this.props.scrollPositionY})`,
      overflow: "hidden"
    }

    let style_second = {
      transform: `translateY(-${this.props.scrollPositionY})`,
      overflow: "hidden"
    }


    return (
      <div className="main_items_container">
        <section
        style={style_first}
        className="main_items_container_child">
          <ItemsContainer {...this.props}/>
        </section>
        <section 
          style={style_second}
          className="main_items_container_child">
          <ItemsContainer {...this.props}/>
        </section>
      </div>
    );
  }

App.css : App.css :

.main_items_container {
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
}

.main_items_container .main_items_container_child{
  width: 50%;
  height: 100vh;
  overflow: scroll;
}

The sample site you linked actually uses the wheel event rather than the scroll event.您链接的示例站点实际上使用了wheel事件而不是scroll事件。 It looks like they use this library to accomplish it: https://swiperjs.com/demos/ .看起来他们使用这个库来完成它: https : //swiperjs.com/demos/ My understanding is that the scroll event only fires if there's a scrollbar, which is why your event handler didn't fire.我的理解是scroll事件仅在有滚动条时才会触发,这就是您的事件处理程序没有触发的原因。

I've created a Code Sandbox that produces the effect you want in React.我创建了一个代码沙箱,可以在 React 中产生您想要的效果。 It does rely on jQuery to compute the height of the whole element, and to set the initial transformation for the left half.它确实依赖 jQuery 来计算整个元素的高度,并为左半部分设置初始变换。 However, those are just convenience methods, and if you don't want jQuery as a dependency, you can find workarounds for those pretty easily.但是,这些只是方便的方法,如果您不希望 jQuery 作为依赖项,您可以很容易地找到这些方法的解决方法。

So you could use hooks and pass a custom css var through inline styling on state change to update your translateY.因此,您可以使用钩子并通过状态更改时的内联样式传递自定义 css var 来更新您的 translateY。 I have not tested but I hope you get my drift.我还没有测试,但我希望你明白我的意思。

let elRef = useRef(null)

let[ height, setHeight] = useState()

use useLayoutEffect hook to add the event listener使用 useLayoutEffect 钩子添加事件监听器

useLayoutEffect (()=>{
  if(!elRef) return
  elRef.current.addEventListener("scroll", setHeight(elRef.current.scrollY));

return  elRef.current.removeEventListener("scroll", setHeight());
}, )

and put the ref on you outermost div perhaps , so your outer div would look like并将 ref 放在你最外面的 div 上,所以你的外部 div 看起来像

<div className='container' ref={elRef} style{{ --height: height}} > 
 <div className='columOne' > </div>
 <div className='columTwo' > </div>
</div>

in your css (I haven't put all that is required but just showing how you custom css var is used在您的 css 中(我没有列出所有需要的内容,只是展示了如何使用自定义 css var

.container{
display: flex;
flex-direction: row
}

And within that equal sized columns在相同大小的列中

   .columnOne{
      transform: `translateY(calc(var(--height) * 1px)`,
      overflow: "hidden"
    }

 .columnTwo{
      transform: `translateY(-calc(var(--height) * 1px)`,
      overflow: "hidden"
    }

Does that help.这有帮助吗。 Let me know if I could be more clear.让我知道我是否可以更清楚。 Or use styled components and pass a prop in to achieve the same result.或者使用样式化组件并传入一个 prop 来实现相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM