I have a function that will jump between each instance of a class ("highlighted") every time to call gonext().
Here is the full code:
var currentPos = 0;
function gonext() {
var pos = $(".highlighted").eq(currentPos).position();
console.log(Math.round(pos.top));
$(".highlighted").eq(currentPos).css("color", "red");
currentPos++;
$(".main").scrollTop(pos.top);
}
.main {
height: 100px;
border:1px solid grey;
padding: 10px 20px;
overflow: scroll;
}
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<hr>
<button onclick="gonext()">Got to Next</button>
<hr>
<div class="main">
<div><span class="highlighted">a</span></div>
<div><span class="highlighted">s</span></div>
<div><span class="highlighted">v</span></div>
<div><span class="highlighted">a</span></div>
<div><span class="highlighted">s</span></div>
<div><span class="highlighted">v</span></div>
<div><span class="highlighted">a</span></div>
<div><span class="highlighted">s</span></div>
<div><span class="highlighted">v</span></div>
<div><span class="highlighted">a</span></div>
<div><span class="highlighted">s</span></div>
<div><span class="highlighted">v</span></div>
</div>
The problem is that as soon as it scrolls to the next position it scrolls up and then down.
See the console log positions for more info.
How can I fix this so that it just scrolls to the next one etc...without the up / down jump?