0

Hello everybody i have a question i'm on reactjs i did create a class named Storymap and then i created a function that scrolls 2 divs at the same time

for example and the scroll is in the bot div 'the second div'

  Scrolling = () => {
    var isSyncingLeftScroll = false,
      isSyncingRightScroll = false,
      leftDiv = document.getElementById('top'),
      rightDiv = document.getElementById('bot');

    leftDiv.onscroll = () => {
      if (!isSyncingLeftScroll) {
        isSyncingRightScroll = true;
        rightDiv.scrollLeft = this.scrollLeft;
      }
      isSyncingLeftScroll = false;
    };

    rightDiv.onscroll = () => {
      if (!isSyncingRightScroll) {
        isSyncingLeftScroll = true;
        leftDiv.scrollLeft = this.scrollLeft;
      }
      isSyncingRightScroll = false;
    };
  }

and i call it like that

<div className="divdeux" id="bot" onScroll={this.Scrolling}> 

but it doesn't work !!! for me Thanks to anyone who takes time to review and give feedback.

4
  • Have you tried to use ref to access your DOM nodes instead of document.getElementById ? Commented Apr 3, 2018 at 9:50
  • Possible duplicate of stackoverflow.com/questions/29725828/… Commented Apr 3, 2018 at 10:00
  • Possible duplicate of Update style of a component onScroll in React.js Commented Apr 3, 2018 at 10:00
  • @3Dos instead of id i put ref ='top' and ref ='bot' in each div and then i call it var leftDiv = $('#top'); for example ? Commented Apr 3, 2018 at 10:05

1 Answer 1

1

Something like this should do the job:

  componentDidMount() {
    this.refContainer.addEventListener('scroll', this.onScroll);
  }

  componentWillUnmount() {
    this.refContainer.removeEventListener('scroll', this.onScroll);
  }

  onScroll = () => {
    var isSyncingLeftScroll = false,
      isSyncingRightScroll = false,
      leftDiv = document.getElementById('top'),
      rightDiv = document.getElementById('bot');

    leftDiv.onscroll = () => {
      if (!isSyncingLeftScroll) {
        isSyncingRightScroll = true;
        rightDiv.scrollLeft = this.scrollLeft;
      }
      isSyncingLeftScroll = false;
    };

    rightDiv.onscroll = () => {
      if (!isSyncingRightScroll) {
        isSyncingLeftScroll = true;
        leftDiv.scrollLeft = this.scrollLeft;
      }
      isSyncingRightScroll = false;
    };
  }

  ...

  render() {
    return (
      <div
        className="divdeux"
        id="bot"
        ref={ref => {
          this.refContainer = ref;
        }}
      >
        {/* whatever you have in the div */}
      </div>
    );
  }
Sign up to request clarification or add additional context in comments.

2 Comments

componentWillUnmount what is this ? why did you call it?
You need to remove eventListeners on unmount, this is recommended. I think react should even give warnings when you forget that. It is basic memory management, if your component is unmounted - the event listener doesn't get removed automatically, you need to do that manually. There is a bunch of approuches towards this boilerplatish situation, just google "remove/unbind on react component unmount".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.