javascriptjqueryonclicktargetscrolltop

Mirror scrolling effect On.Click between two containers


At the moment, I am learning how to write in javascript and jquery.I use a code which when you click on the navigation menu it scrolls down to a specific div inside another div. However, I am trying to split this container into two separate ones. So the Left_Container will start from the bottom and the Right_Container will start from the top. The idea is to create a mirror scrolling between the two containers. While the first one is going down the other to go up. I assume that the mistake is somewhere in the data-target but my knowledge isn't enough to fix it by my own. If someone can help me I will be really grateful.

$(document).ready(function() {
	$(".Menu li").on('click', function() {
		$('.Left_Container').animate({
			scrollTop: $($(this).data('target')).position().top +                                       $('.Left_Container').scrollTop()
		}, 'slow');
		$('.Right_Container').animate({
			scrollTop: $($(this).data('target')).position().top +                                       $('.Right_Container').scrollTop()
		}, 'slow');
	});  
});
.Wrapper {
  display: flex;
  position: relative;
  width: 90vw;
  height: 90vh;
  background-color: purple;
}
.Menu {
  position: relative;
  width: 10vw;
  height: 90vh;
  background-color: blue;
}
.Menu li {
  position: relative;
  font-size: 4vw;
  line-height: 5vw;
  text-align: center;
  color: white;
  cursor: pointer;
  list-style-type: none;
}
.Left_Container {
  position: relative;
  width: 43vw;
  height: 90vh;
  background-color: red;
  overflow-y: hidden;
}
.Right_Container {
  position: relative;
  width: 43vw;
  height: 90vh;
  background-color: red;
  overflow-y: hidden;
}
.Box {
  position: relative;
  width: 40vw;
  height: 90vh;
  background-color: purple;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Wrapper">
    <div class="Menu">
        <li data-target="#Left_A,Right_C">A</li>
        <li data-target="#Left_B,Right_B">B</li>
        <li data-target="#Left_C,Right_A">C</li>
    </div>
    <div class="Left_Container">
      <div class="Box" id="Left_C">
          Box C
      </div>
      <div class="Box" id="Left_B">
          Box B
      </div>
      <div class="Box" id="Left_A">
          Box A
      </div>
    </div>
    <div class="Left_Container">
      <div class="Box" id="Right_A">
          Box A
      </div>
      <div class="Box" id="Right_B">
          Box B
      </div>
      <div class="Box" id="Right_C">
          Box C
      </div>
    </div>
</div>

PS: Thank you in advance.

Best regards,

George S.


Solution

  • Here's an easy to understand example of two divs that scroll each other in reverse...

    <!DOCTYPE html>
    <head>
    
    <style type="text/css">
    
    .BoxStyle {
     position: absolute;
     left: 10px;
     width: 100px;
     height: 100px;
     overflow: auto;
     border: 1px solid black;
    }
    
    </style>
    </head>
    
    <body>
    
    
    <div class="BoxStyle" id="Box1" onscroll="Box2.scrollTop=(Box2.scrollHeight-this.scrollTop);">box 1
    <div style="position:absolute; top:500px;">.</div>
    </div>
    
    <br><br><br><br><br><br><br><br>
    
    <div class="BoxStyle" id="Box2" onscroll="Box1.scrollTop=(Box1.scrollHeight-this.scrollTop);">box 2
    <div style="position:absolute; top:500px;">.</div>
    </div>
    
    </body>
    </html>