focus() to input without scrolling

2019-01-23 00:55发布

I have a search input text which I'd like to apply a focus() when loading the page, the problem is that the focus function automatically does a scroll to this field. Any solution to disable this scroll?

<input id="search_terms" type="text" />
<script>
    document.getelementbyId('search-terms').focus();
</script>

6条回答
仙女界的扛把子
2楼-- · 2019-01-23 01:39

As of today, the preferred way to set the focus on an element upon page load is using the autofocus attribute. This does not involve any scrolling.

<input id="search_terms" type="text" autofocus />

The autofocus attrubute is part of the HTML5 standard and is supported by all major browsers, with the only notable exception of Internet Explorer 9 or earlier.

查看更多
干净又极端
3楼-- · 2019-01-23 01:44

There is a new WHATWG standard which allows you you to pass an object to focus() which specifies that you want to prevent the browser from scrolling the element into view:

const element = document.getElementById('search-terms')

element.focus({
  preventScroll: true
});

It is currently supported in Chrome 64 and Edge Insider Preview build 17046.

查看更多
再贱就再见
4楼-- · 2019-01-23 01:49

If you are using jQuery, you can also do this:

$.fn.focusWithoutScrolling = function(){
  var x = window.scrollX, y = window.scrollY;
  this.focus();
  window.scrollTo(x, y);
};

and then

$('#search_terms').focusWithoutScrolling();
查看更多
姐就是有狂的资本
5楼-- · 2019-01-23 01:50

A bit modified version that supports more browsers (incl. IE9)

var cursorFocus = function(elem) {
  var x, y;
  // More sources for scroll x, y offset.
  if (typeof(window.pageXOffset) !== 'undefined') {
      x = window.pageXOffset;
      y = window.pageYOffset;
  } else if (typeof(window.scrollX) !== 'undefined') {
      x = window.scrollX;
      y = window.scrollY;
  } else if (document.documentElement && typeof(document.documentElement.scrollLeft) !== 'undefined') {
      x = document.documentElement.scrollLeft;
      y = document.documentElement.scrollTop;
  } else {
      x = document.body.scrollLeft;
      y = document.body.scrollTop;
  }

  elem.focus();

  if (typeof x !== 'undefined') {
      // In some cases IE9 does not seem to catch instant scrollTo request.
      setTimeout(function() { window.scrollTo(x, y); }, 100);
  }
}
查看更多
够拽才男人
6楼-- · 2019-01-23 01:55

The answers here do not take care of scrolling on the whole hierarchy, but the main scrollbars only. This answer will take care of everything:

    var focusWithoutScrolling = function (el) {
        var scrollHierarchy = [];

        var parent = el.parentNode;
        while (parent) {
            scrollHierarchy.push([parent, parent.scrollLeft, parent.scrollTop]);
            parent = parent.parentNode;
        }

        el.focus();

        scrollHierarchy.forEach(function (item) {
            var el = item[0];

            // Check first to avoid triggering unnecessary `scroll` events

            if (el.scrollLeft != item[1])
                el.scrollLeft = item[1];

            if (el.scrollTop != item[2])
                el.scrollTop = item[2];
        });
    };
查看更多
来,给爷笑一个
7楼-- · 2019-01-23 01:59

Here's a complete solution:

var cursorFocus = function(elem) {
  var x = window.scrollX, y = window.scrollY;
  elem.focus();
  window.scrollTo(x, y);
}

cursorFocus(document.getElementById('search-terms'));
查看更多
登录 后发表回答