<!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ī)號</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)元素滾動到可視區(qū)
? ? function activeElementScrollIntoView(activeElement, delay) {
? ? ? var editable = activeElement.getAttribute('contenteditable')
? ? ? // 輸入框禾嫉、textarea或富文本獲取焦點(diǎn)后沒有將該元素滾動到可視區(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)擊收起按鈕,輸入框都會失去焦點(diǎn),鍵盤會收起壶笼,
? ? ? ? $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 鍵盤彈起或收起頁面高度會發(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>