I have a div which looks like below. When a user clicks on alphabets from left, the right side should scroll to that alphabet heading.

<div class="left">
<ul class="letters">
<li>
<a href="#letter-A">A</a>
</li>
<li>
<a href="#letter-B">B</a>
</li>
</ul>
</div>
<div class="right brands-scroll" style="max-height: 320px;overflow-y: scroll;">
<ul>
<li><h3 id="letter-A">A</h3></li>
<li>Apple</li>
<li>Alphanso</li>
<li><h3 id="letter-B">B</h3></li>
<li>Ball</li>
<li>Banana</li>
</ul>
</div>
Jquery code
$('ul.letters li a').on('click', function (t) {
t.preventDefault();
var targetSec = $(this).attr('href');
$('.brands-scroll').stop().animate({
scrollTop: $('#' + targetSec).offset().top
}, 500);
});
I tried this solution, but it didn't work properly jQuery scroll to element
Try this:
$(".letters li a").click(function() {
var container = $('.right'),id = $(this).attr('href'),
scrollTo = $(id);
container.animate({
scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop()
});
});
Click on any of the alphabet in the demo and you can see the result.
Working Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With