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

I have website; a background image with an input. When I press on the input on my phone, the keyboard o

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745613113a4636068.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信