How can I focus to a HTML element (ex. "a") and do not change the current scroll settings.
For ex. if I use:
$('#link').focus();
and this link is not visible in the screen (ex. is bellow the visible area) the browser scrolls down to show the element. How can I set the focus without scrollbar movement? I need to stay the scrollbar in the original place.
I have tried this, but it produces some screen flickering, and it is a hack, not an elegant solution:
var st=$(document).scrollTop();
$('#link').focus();
$(document).scrollTop(st);
Can somebody help me, please?
The reason why this is so hard for you is because you aren't supposed to do it. The browsers are designed to help the user avoid websites that do stuff like this, because a link the has focus will activate by hitting return or space on the keyboard, and users rarely wish to follow a link they are not aware is there.
Try to work with the browser instead of against it, and you will usually end up with happier users.
I have the same problem, but in words: I think a more elegant solution would be to give the focus once the element is in the viewport.
Here's what I copy/pasted (Kudos to ADB from this thread Jquery check if element is visible in viewport)
Try this:
and then
Edit:
It's been reported that this doesn't work in IE10. The probable solution would be to use:
but I haven't tested it.
$('#link').css('position', 'fixed').focus().css('position', 'static')
works in Firefox.(Edit: You should not use this hack)
Try this jQuery solution:
This worked for me