H5軟鍵盤兼容方案

<!DOCTYPE html>

<html lang="en">

<head>

? <meta charset="UTF-8">

? <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">

? <meta http-equiv="X-UA-Compatible" content="ie=edge">

? <title>可能這些是你想要的H5鍵盤兼容方案</title>

? <style>

? ? html,

? ? body {

? ? ? height: 100%;

? ? }

? ? body {

? ? ? margin: 0;

? ? ? text-align: center;

? ? }

? ? .header {

? ? ? padding: 20px 0;

? ? ? box-shadow: 0 1px 1px #dddddd;

? ? }

? ? .content {

? ? ? margin-top: 100px;

? ? ? margin: 100px 0 80px;

? ? }

? ? .content input {

? ? ? border-radius: 4px;

? ? ? border: 1px solid #dddddd;

? ? ? height: 26px;

? ? ? line-height: 26px;

? ? }

? ? .footer {

? ? ? position: fixed;

? ? ? left: 0;

? ? ? right: 0;

? ? ? bottom: 0;

? ? ? padding: 20px 0;

? ? ? box-shadow: 0 -1px 1px #dddddd;

? ? }

? ? .header,

? ? .footer {

? ? ? background-color: #42b983;

? ? ? color: #ffffff;

? ? }

? </style>

</head>

<body>

? <header class="header">I am Header</header>

? <div class="content">

? ? <p>請輸入用戶名</p>

? ? <input type="text" class="input">

? ? <p>請輸入手機(jī)號(hào)</p>

? ? <input type="tel" novalidate="novalidate" pattern="[0-9]*" class="input">

? ? <p>請輸入密碼</p>

? ? <input type="password" class="input">

? ? <p>請重新輸入密碼</p>

? ? <input type="password" class="input">

? </div>

? <footer class="footer">I am Footer</footer>


? <script>


? ? // 判斷設(shè)備類型

? ? var judgeDeviceType = function () {

? ? ? var ua = window.navigator.userAgent.toLocaleLowerCase();

? ? ? var isIOS = /iphone|ipad|ipod/.test(ua);

? ? ? var isAndroid = /android/.test(ua);

? ? ? return {

? ? ? ? isIOS: isIOS,

? ? ? ? isAndroid: isAndroid

? ? ? }

? ? }()

? ? // 獲取到焦點(diǎn)元素滾動(dòng)到可視區(qū)

? ? function activeElementScrollIntoView(activeElement, delay) {

? ? ? var editable = activeElement.getAttribute('contenteditable')

? ? ? // 輸入框、textarea或富文本獲取焦點(diǎn)后沒有將該元素滾動(dòng)到可視區(qū)

? ? ? if (activeElement.tagName == 'INPUT' || activeElement.tagName == 'TEXTAREA' || editable === '' || editable) {

? ? ? ? setTimeout(function () {

? ? ? ? ? activeElement.scrollIntoView();

? ? ? ? }, delay)

? ? ? }

? ? }

? ? // 監(jiān)聽輸入框的軟鍵盤彈起和收起事件

? ? function listenKeybord($input) {

? ? ?var scrollTop=0;

? ? ? if (judgeDeviceType.isIOS) {

? ? ? ? // IOS 鍵盤彈起:IOS 和 Android 輸入框獲取焦點(diǎn)鍵盤彈起

? ? ? ? $input.addEventListener('focus', function () {

? ? ? ? ? console.log('IOS 鍵盤彈起啦!');

? ? ? ? ? scrollTop=document.body.scrollTop || document.documentElement.scrollTop;

? ? ? ? ? $inputs[0].value=scrollTop;


? ? ? ? }, false)

? ? ? ? // IOS 鍵盤收起:IOS 點(diǎn)擊輸入框以外區(qū)域或點(diǎn)擊收起按鈕,輸入框都會(huì)失去焦點(diǎn),鍵盤會(huì)收起,

? ? ? ? $input.addEventListener('blur', () => {

? ? ? ? ? ? console.log('IOS 鍵盤收起啦!');

? ? ? ? ? ? var wechatInfo = window.navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i);

? ? ? ? ? ? if (!wechatInfo) return;

? ? ? ? ? ? var wechatVersion = wechatInfo[1];

? ? ? ? ? ? var version = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);

? ? ? ? ? ? if (+wechatVersion.replace(/\./g, '') >= 674 && +version[1] >= 12) {

? ? ? ? ? ? ? window.scrollTo(0, Math.max(document.body.clientHeight, document.documentElement.clientHeight));

? ? ? ? ? ? }

? ? ? ? })

? ? ? }

? ? ? // Andriod 鍵盤收起:Andriod 鍵盤彈起或收起頁面高度會(huì)發(fā)生變化,以此為依據(jù)獲知鍵盤收起

? ? ? if (judgeDeviceType.isAndroid) {

? ? ? ? var originHeight = document.documentElement.clientHeight || document.body.clientHeight;

? ? ? ? window.addEventListener('resize', function () {

? ? ? ? ? var resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;

? ? ? ? ? if (originHeight < resizeHeight) {

? ? ? ? ? ? console.log('Android 鍵盤收起啦!');

? ? ? ? ? ? // Android 鍵盤收起后操作

? ? ? ? ? ? // 修復(fù)小米瀏覽器下,輸入框依舊被輸入法遮擋問題

? ? ? ? ? ? if (judgeDeviceType.isMiuiBrowser) {

? ? ? ? ? ? ? document.body.style.marginBottom = '0px';

? ? ? ? ? ? }

? ? ? ? ? } else {

? ? ? ? ? ? console.log('Android 鍵盤彈起啦!');

? ? ? ? ? ? // Android 鍵盤彈起后操作

? ? ? ? ? ? // 修復(fù)小米瀏覽器下,輸入框依舊被輸入法遮擋問題

? ? ? ? ? ? if (judgeDeviceType.isMiuiBrowser) {

? ? ? ? ? ? ? document.body.style.marginBottom = '40px';

? ? ? ? ? ? }

? ? ? ? ? ? activeElementScrollIntoView($input, 1000);

? ? ? ? ? }

? ? ? ? ? originHeight = resizeHeight;

? ? ? ? }, false)

? ? ? }

? ? }

? ? var $inputs = document.querySelectorAll('.input');

? ? for (var i = 0; i < $inputs.length; i++) {

? ? ? listenKeybord($inputs[i])

? ? }

? </script>

</body>

</html>


參考地址:https://segmentfault.com/a/1190000018959389

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近一段時(shí)間在做 H5 聊天項(xiàng)目,踩過其中一大坑:輸入框獲取焦點(diǎn),軟鍵盤彈起,要求輸入框吸附(或頂)在輸入法框上。...
    bayi_lzp閱讀 475評論 0 9
  • 轉(zhuǎn)載 1. 問題描述: 最近一段時(shí)間在做 H5 項(xiàng)目,踩過其中一大坑:輸入框獲取焦點(diǎn),軟鍵盤彈起,要求輸入框吸附(...
    星空里的塵埃閱讀 746評論 0 2
  • HTML: <!DOCTYPE html> 可能這些是你想要的H5鍵盤兼容方案 I am ...
    勾起一抹笑容閱讀 1,131評論 0 1
  • // 判斷設(shè)備類型 var judgeDeviceType = function(){ var ua = wind...
    me_5ca2閱讀 1,720評論 0 0
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標(biāo)準(zhǔn)。 注意:講述HT...
    kismetajun閱讀 28,771評論 1 45

友情鏈接更多精彩內(nèi)容