jQuery重点没有滚动 [英] jQuery focus without scroll
问题描述
如何将焦点集中到一个HTML元素(例如a),而不要改变当前的滚动设置。
如果我使用:
$('#link')。focus();
并且这个链接在屏幕上是不可见的(如下图所示)浏览器滚动下来显示元素。如何在没有滚动条移动的情况下设置焦点?我已经试过这个,但它会产生一些屏幕闪烁,这是一个黑客,而不是一个优雅的解决方案: / p>
var st = $(document).scrollTop();
$('#link')。focus();
$(document).scrollTop(st);
有人可以帮助我吗?
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b var x = window.scrollX,y = window.scrollY;
this.focus();
window.scrollTo(x,y);
返回这个; //链接性
};
然后
$( '#链接')focusWithoutScrolling();
编辑:
据报道,这在IE10中不起作用。可能的解决方案是使用:
$ $ p $ $ $ $ $ $ .scrollTop();
但是我没有测试过。
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?
Try this:
$.fn.focusWithoutScrolling = function(){
var x = window.scrollX, y = window.scrollY;
this.focus();
window.scrollTo(x, y);
return this; //chainability
};
and then
$('#link').focusWithoutScrolling();
Edit:
It's been reported that this doesn't work in IE10. The probable solution would be to use:
var x = $(document).scrollLeft(), y = $(document).scrollTop();
but I haven't tested it.
这篇关于jQuery重点没有滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!