1

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,614
  • 1
  • 18
  • 41

1 Answers1

4

Just restoring scroll position will work

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

JSBin Example

Christophe Marois
  • 6,051
  • 1
  • 28
  • 26