镜像滚动效果On.Click两个容器之间

时间:2016-04-23 21:42:24

标签: javascript jquery onclick target scrolltop

目前,我正在学习如何用javascript和jquery编写。我使用的代码当你点击导航菜单时,它会向下滚动到另一个div内的特定div。但是,我试图将这个容器分成两个独立的容器。因此Left_Container将从底部开始,Right_Container将从顶部开始。我们的想法是在两个容器之间创建一个镜像滚动。而第一个是下降另一个上升。我认为错误在data-target的某个地方,但我的知识还不足以解决我自己的问题。如果有人可以帮助我,我将非常感激。

$(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:提前谢谢你。

致以最诚挚的问候,

George S.

1 个答案:

答案 0 :(得分:1)

这是一个易于理解的两个div的例子,它们相反地相互滚动......

<!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>