While developing a new feature for my music player, I encountered an issue with centering the track/div upon clicking in the scrollable parent div. Sometimes it doesn't scroll to the center as intended and instead scrolls erratically to the top of the same spot when navigating down and selecting a lower track. The goal of this feature is to provide users with a seamless experience when exploring tracks, eliminating the need for manual scrolling and improving overall user experience. Can anyone offer suggestions on how to ensure that when a track is clicked, it smoothly scrolls to and vertically centers the track in the middle of the parent div? (This functionality will also be extended to the next and prev buttons, as demonstrated in the live demo)
Live Demo:
$(".playbox").on('click', function () {
if (!$(this).hasClass('playboxplaylight') && $(this) !== nowplay) {$('.scrollit').animate({
scrollTop: $(this).offset().top - $(this).height()
},1000);}