2

This js Bin demonstrates an issue I'm seeing with Chrome. When I focus on the first input and tab through the list, focus will move to inputs that are invisible to the user without scrolling it up as it should. You'll see it when you pass item D in the list. If I remove the initial value from each input, then scrolling works as it should. It seems to me that something about the automatic selection of the previous value breaks the scrolling.

Am I doing something wrong here? It works fine in Firefox.

Also, if anyone can suggest a workaround it would be a big help.

2
  • Works fine in IE and Safari also, but not Chrome. Weird. Commented Oct 13, 2015 at 21:58
  • I've got a hack workaround here: jsbin.com/befoka/edit?html,css,js Commented Oct 14, 2015 at 14:17

1 Answer 1

3

You have a nice hack, but it can be simplified:

$(function() {
  $('input').on('focus', function(e) {
    if(this.scrollIntoViewIfNeeded) {
      this.scrollIntoViewIfNeeded();
    }
  });
});

For browsers that don't support scrollIntoViewIfNeeded(), the function won't be called. Chrome does support it, so this solves the problem.

$(function() {
  $('input').on('focus', function(e) {
    this.scrollIntoViewIfNeeded();
  });

  $('input').first().focus();
});
.timepoints {
  list-style: none;
  margin: 0.5em 0;
  height: 10em;
  overflow: auto;
  border: 1px solid black;
  padding: 0.1em
}
.timepoints li {
  border: 1px solid black;
  padding: 0.5em;
  margin-bottom: 0.2em;
  margin-left: 0
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="timepoints">
  <li>
    <input type="text" value="A" />
  </li>
  <li>
    <input type="text" value="B" />
  </li>
  <li>
    <input type="text" value="C" />
  </li>
  <li>
    <input type="text" value="D" />
  </li>
  <li>
    <input type="text" value="E" />
  </li>
  <li>
    <input type="text" value="F" />
  </li>
</ul>

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, much better than mine. I didn't know about scrollIntoViewIfNeeded. Seems to do exactly what I need.
This doesn't fix the problem on Chrome 45 on my Chromebook.
Interesting. Can you console.log this.scrollIntoViewIfNeeded to see if Chromebook supports it?
I spoke too soon. It works in the jsBin when the output is popped out. But doesn't work when output is shown in one of the little strips they use. I don't need that case anyway. It's working fine at test.tarheelgameplay.org. I'll put it in production when I get back home.

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.