2

Can I prevent default scroll top to input when it's focused and not visible? Simple example:

<input type="search">
<div style="height: 2000px"></div>

To see what I mean, go to http://jsfiddle.net/PV5tC/ and:

  1. Click on input to make it focused.
  2. Scroll down to the end of the page (input is not visible now).
  3. Start typing anything.
sergdenisov
  • 8,327
  • 9
  • 48
  • 63
  • 1
    Why would you want to prevent the behaviour? Are people supposed to guess what they're typing in? Not everyone is a 400wpm touch-typist. – Rory McCrossan Jul 22 '14 at 15:06
  • It's not exactly what you're asking but you could capture input on the `document` level then alter the value of your input element using those keystrokes. Not sure why you'd want to but it should work. – Mike Cluck Jul 22 '14 at 15:08
  • I have more complex logic on my site. First I simply want to prevent it, do some actions and programmatically call focus on input. – sergdenisov Jul 22 '14 at 15:14
  • I would say this is not possible, because you are trying to break something all browsers, and maybe OS' do by default, aka bad UX idea – Ryan B Jul 22 '14 at 15:16
  • Thanks to all, I've decided to keep things simple and use blur. – sergdenisov Jul 22 '14 at 16:03

3 Answers3

2

position : fixed; will avoid the automatic scroll to input as it won't be in the scrollable area.

Here is a fast written example, based on this answer, and using jQuery, sorry…

CSS

.hiddenInput{position: fixed; opacity: 0;}

jQuery

    var inputTop, inputBottom;
    window.onload = function()
    {
        inputTop = $('#input').offset().top
        inputBottom = inputTop + $('#input').height();
    }

    function isScrolledIntoView(el)
    {
        var docViewTop = $(window).scrollTop();
        var docViewBottom = docViewTop + $(window).height();
        return ((inputBottom <= docViewBottom) && (inputTop >= docViewTop));
    }
    function hideInput()
    {
        var el = $('#input');
        if ( isScrolledIntoView(el) ){
            el.removeClass();
            }else{
            el.addClass('hiddenInput');
            el.css({top: inputTop});
        }
    }
    window.onscroll = hideInput;

working fiddle

Community
  • 1
  • 1
Kaiido
  • 123,334
  • 13
  • 219
  • 285
0

I also cannot think of this as good design. But we all have to do something our way. Try below script if it helps

$('input[type="search"]').on("keypress", function(event){
    event.preventDefault();
    return false;
});

This will allow to keep focus on input box and browser will also not scroll to input box. If you want it cross browser you might need to handle keydown event as well. Let me know if you face any issue in handling other events.


But it will prevent you from typing any value in the input at any moment in any input : then blur is easier

Kaiido
  • 123,334
  • 13
  • 219
  • 285
khagesh
  • 948
  • 1
  • 6
  • 18
0

This is maybe antipattern but this should work. Info here: Disable scrolling when changing focus form elements ipad web app

$('input[type="search"]').on("keydown", function(){
  var oldScroll = $(window).scrollTop();
  $(window).one('scroll', function() {
    $(window).scrollTop(oldScroll);
  });
  // Your ACTION HERE
})