1

I want to scroll to a specific index number in a list of child elements.

What is the simplest and effective way to do this.

The only restrictions are where the index is a number from 0 to n-1. And I retrieve index from $(this).index(); when the item was clicked and saved to scroll again at some other point in time like in a memory game.

I was thinking something like this but more eloquent:

   $("child:nth-child(" + index + ")").scroll();

<parent>
 <child/>
 <child/>
 <child/>
...
 <child/> <!-- nth child-->
</parent>
5
  • 2
    You could simulate the clicking of an anchor Commented Sep 17, 2014 at 18:43
  • 2
    Why not just $.scrollTop($(this).offset().top); Commented Sep 17, 2014 at 18:46
  • It doesn't work on all mobile devices running jQuery mobile 1.3.2. But I'll give it a try. At least for some mobile devices. But desktop browsers it does work. Commented Sep 17, 2014 at 19:00
  • Never mind, its actually working ;) Commented Sep 17, 2014 at 19:07
  • Never mind again. I was in debug mode but it doesn't work for my case specific iphone devices in 1.3.2. darn. Commented Sep 17, 2014 at 19:29

1 Answer 1

1

I know this question is very old one. Maybe this answer will help someone.

let index = 3; // find index for the li tag
var rows = document.querySelectorAll('ul#list li');
rows[index].scrollIntoView({
   behavior: 'smooth',
   block: 'start'
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.