防止<输入/>在视线之外,在选择()(或聚焦)时将页面滚动到视图中

Prevent an <input /> out of sight, from scrolling the page into view when selected() (or onFocus)

本文关键字:滚动 视图 聚焦 lt gt 视线之外 防止 选择 输入      更新时间:2023-09-26

我有一个页面,其中有一个JS代码,用鼠标悬停在输入的父级上的jQuery的$element.select()来选择输入的内容。

我想防止浏览器在选中输入时将其滚动到视图中(我想也是焦点?),因为页面是一个缩略图阵列,每个缩略图下面都有输入,如果你不移开鼠标,这会产生连锁反应,一直滚动到页面底部。

试试这个代码

$('#childInput').on('focus',function(e)
{
  e.preventDefault();
 });

$('#parentDiv').on('mouseover',function()
{
   var temp= $('body').scrollTop();
   var temp1=$('body').scrollLeft();
   $('#childInput').select();
   $('body').scrollTop(temp);
   $('body').scrollLeft(temp1);
});