2

I'd like to focus an element (a slider/carousel) upon page load, to allow immediate control of the slider using arrow keys. JQuery's .focus() works, but automatically scrolls the element into view. This is a problem on limited viewports, where the top of the page is unexpectedly scrolled past.

Is it possible to focus an element without jumping to it?

cmeeren
  • 3,890
  • 2
  • 20
  • 50

1 Answers1

4

Just restoring scroll position will work

var position = $(window).scrollTop();
$('#your-element').focus()
$(window).scrollTop(position);

JSBin Example

Christophe Marois
  • 6,471
  • 1
  • 30
  • 32