I think I understand what you want please try this
and you can enhance the code
$('#dv2').on("scroll", function () {
var lastDivInView = -1
var stop=false
var allCommts=$("#dv2").find("div")
var cnt=allCommts.length
var i=0;
while (!stop && i < cnt) {
if ($(allCommts[i]).offset().top > 0 && $(allCommts[i]).offset().top < $(this).height()) {
lastDivInView = i;
stop = true
}
i++
}
if (lastDivInView>-1)
$("#dv1").find("a")[lastDivInView].scrollIntoView()
});
function ScrollToView(index) {
var dvCommt = $("#dv2").find("div")[index - 1]
dvCommt.scrollIntoView()
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div style="width: 400px">
<div id="dv1" style="float: left; max-height: 40px; width: 45%; border: 1px solid red; overflow: auto">
<a href="#" onclick="ScrollToView(1)">Code 1</a><br />
<a href="#" onclick="ScrollToView(2)">Code 2</a><br />
<a href="#" onclick="ScrollToView(3)">Code 3</a><br />
<a href="#" onclick="ScrollToView(4)">Code 4</a><br />
<a href="#" onclick="ScrollToView(5)">Code 5</a><br />
<a href="#" onclick="ScrollToView(6)">Code 6</a><br />
<a href="#" onclick="ScrollToView(7)">Code 7</a><br />
<a href="#" onclick="ScrollToView(8)">Code 8</a><br />
<a href="#" onclick="ScrollToView(9)">Code 9</a><br />
<a href="#" onclick="ScrollToView(10)">Code 10</a><br />
</div>
<div id="dv2" style="float: left; max-height: 150px; width: 45%; border: 1px solid green; overflow: auto">
<div style="border:1px solid black;margin:10px">
Comments for code 1:aaa bbbbb cccc dddd eee ffff
</div>
<div style="border:1px solid black;margin:10px">
Comments for code 2:aaa bbbbb cccc dddd eee ffff
</div>
<div style="border:1px solid black;margin:10px">
Comments for code 3:aaa bbbbb cccc dddd eee ffff
</div>
<div style="border:1px solid black;margin:10px">
Comments for code 4:aaa bbbbb cccc dddd eee ffff
</div>
<div style="border:1px solid black;margin:10px">
Comments for code 5:aaa bbbbb cccc dddd eee ffff
</div>
<div style="border:1px solid black;margin:10px">
Comments for code 6:aaa bbbbb cccc dddd eee ffff
</div>
<div style="border:1px solid black;margin:10px">
Comments for code 7:aaa bbbbb cccc dddd eee ffff
</div>
<div style="border:1px solid black;margin:10px">
Comments for code 8:aaa bbbbb cccc dddd eee ffff
</div>
<div style="border:1px solid black;margin:10px">
Comments for code 9:aaa bbbbb cccc dddd eee ffff
</div>
<div style="border:1px solid black;margin:10px">
Comments for code 10:aaa bbbbb cccc dddd eee ffff
</div>
</div>
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…