最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Prevent virtual keyboard from pushing content up - Stack Overflow

matteradmin5PV0评论

I have website; a background image with an input. When I press on the input on my phone, the keyboard opens an everything gets pushed up. I would like instead the content to stay in place and keyboard just to go over the content without affecting(moving) it. I saw this solution:

input.onfocus = function () {
    window.scrollTo(0, 0);
    document.body.scrollTop = 0;
}

But it doesn't seem to work. I'm looking forward to your answers!

I have website; a background image with an input. When I press on the input on my phone, the keyboard opens an everything gets pushed up. I would like instead the content to stay in place and keyboard just to go over the content without affecting(moving) it. I saw this solution:

input.onfocus = function () {
    window.scrollTo(0, 0);
    document.body.scrollTop = 0;
}

But it doesn't seem to work. I'm looking forward to your answers!

Share Improve this question edited Jul 2, 2018 at 15:23 user8866053 asked Jul 2, 2018 at 14:44 srnesrne 331 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You can take a look at this StackOverflow post, but I'll summarize the most useful parts for you:

Start off with the input's CSS as position: fixed;. When in focus, change it to absolute. Here is an example with JS:

if (document.getElementById("fixed") == document.activeElement) {
    document.getElementById("fixed").class += "absolute"
}

Of course, that relies on CSS:

#fixed {
    ...
    position: fixed;
}
#fixed.absolute {
    position: absolute;
}

I hope this helps!

Post a comment

comment list (0)

  1. No comments so far