Home Web Front-end HTML Tutorial 虚拟键盘,移动web开发的痛_html/css_WEB-ITnose

虚拟键盘,移动web开发的痛_html/css_WEB-ITnose

Jun 24, 2016 am 11:24 AM

原生APP的弹出虚拟键盘和收回虚拟键盘,输入框始终贴在虚拟键盘的上方。如下图:

如果移动端web也想做类似的功能,可以实现吗? 

你可能会说着:“不就是放一个position: fixed;的输入框在页面底部嘛,有什么好难的?!” 

当时我也是这么想的(⊙﹏⊙),但事实真的是这样吗?下面我们来看一些测试结果。

安卓4.2.1-qq浏览器,测试结果如下:

如图所示,输入框不见了。。。

 

再看看ios的safari:

为何又多了条白带?

还有,收起键盘后,为啥页面下移了。。。

好吧,其实这只是问题的冰山一角。

进入正题,怎么才能解决这些问题呢?

我们可以先从虚拟键盘入手,看看是否可以监听到呼出键盘、收回键盘这两个事件。

部分浏览器可以通过捕捉resize事件知道是否呼出收起虚拟键盘。

测试页:  http://backtonaturedemo.github.io/frontend/case/keyborad/resize.html

下面是测试的结果:

 

iPhone 5s iOS 8.2 : 

(n表示不能触发resize事件,y表示能触发resize事件)

注意: ios的UC浏览器中,点击fixed定位的输入框,呼出键盘后无法输入。

 

Coolpad8720Q Andorid 4.2.1

 

那么监听focus和blur事件又如何呢?

 

测试页:  http://backtonaturedemo.github.io/frontend/case/keyborad/focusblur.html

 

用上述两台机器继续测试,结论如下:

  1. ios和安卓点击输入框都会触发focus事件
  2. ios的所有浏览器点击虚拟键盘的“完成“键或者其他方式收起键盘都会触发blur事件
  3. 安卓手机在点击”收起按钮“或者其他操作收起键盘的时候,没有触发blur事件,焦点还是在输入框上。

下面为测试截图

 

 

所以,可以采取如下方案做web评论发表框

 

示例页面:  http://backtonaturedemo.github.io/frontend/case/keyborad/demo1.html

 

1.  除评论框以外的元素都放在一个父元素,这里父元素是main。

1 <div class="main">2      <img src="/static/imghw/default1.png"  data-src="http://backtonaturedemo.github.io/frontend/case/keyborad/test.png"  class="lazy" alt="">3      <img src="/static/imghw/default1.png"  data-src="http://backtonaturedemo.github.io/frontend/case/keyborad/test1.png"  class="lazy" alt="">4 </div>
Copy after login

2. 评论框和发布按钮放在一个div里

1 <div class="comment">2     <div class="buttons">3         <button class="sure cell">发表</button>4         <button class="cancel cell">取消</button>5     </div>6     <textarea class="input" placeholder="我来说两句"></textarea>7 </div>
Copy after login

注意: 需要在呼出键盘前,纪录下页面滚动条位置。

呼出键盘后,隐藏.main(除评论框以外的元素)。

撑高输入框,取消fixed,加入上外边距(给发表按钮留下位置)。

当输入框blur或点击取消,还原页面。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? Apr 05, 2025 am 06:15 AM

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

How to distinguish between closing a browser tab and closing the entire browser using JavaScript? How to distinguish between closing a browser tab and closing the entire browser using JavaScript? Apr 04, 2025 pm 10:21 PM

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...

See all articles