228 lines
7.0 KiB
HTML
228 lines
7.0 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
|
|
<title>H5 键盘辅助栏</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
|
padding: 20px;
|
|
padding-bottom: 60px; /* 预留辅助栏空间 */
|
|
background-color: #f5f5f7;
|
|
}
|
|
|
|
.container {
|
|
max-width: 600px;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
h3 {
|
|
color: #1d1d1f;
|
|
margin-bottom: 30px;
|
|
text-align: center;
|
|
}
|
|
|
|
.input-group {
|
|
margin-bottom: 20px;
|
|
}
|
|
|
|
.input-field {
|
|
width: 100%;
|
|
padding: 14px 16px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 12px;
|
|
font-size: 16px;
|
|
background-color: white;
|
|
transition: border-color 0.2s;
|
|
}
|
|
|
|
.input-field:focus {
|
|
outline: none;
|
|
border-color: #007aff;
|
|
box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.2);
|
|
}
|
|
|
|
.input-field::placeholder {
|
|
color: #999;
|
|
}
|
|
|
|
/* 键盘辅助栏样式 */
|
|
.keyboard-accessory {
|
|
position: fixed;
|
|
bottom: 0;
|
|
left: 0;
|
|
width: 100%;
|
|
height: 55px;
|
|
background-color: white;
|
|
border-top: 1px solid #eee;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
padding: 0 16px;
|
|
transition: transform 0.3s ease;
|
|
transform: translateY(100%); /* 默认隐藏 */
|
|
z-index: 9999;
|
|
}
|
|
|
|
.keyboard-accessory.show {
|
|
transform: translateY(0); /* 显示时移回底部 */
|
|
}
|
|
|
|
.accessory-btn {
|
|
padding: 8px 20px;
|
|
border: none;
|
|
border-radius: 20px;
|
|
font-size: 15px;
|
|
font-weight: 500;
|
|
cursor: pointer;
|
|
transition: opacity 0.2s;
|
|
}
|
|
|
|
.accessory-btn:hover {
|
|
opacity: 0.9;
|
|
}
|
|
|
|
.cancel-btn {
|
|
color: #666;
|
|
background-color: #f5f5f5;
|
|
}
|
|
|
|
.clear-btn {
|
|
color: #333;
|
|
background-color: #eee;
|
|
}
|
|
|
|
.confirm-btn {
|
|
color: white;
|
|
background-color: #007aff;
|
|
}
|
|
|
|
/* 深色模式适配 */
|
|
@media (prefers-color-scheme: dark) {
|
|
body {
|
|
background-color: #1c1c1e;
|
|
}
|
|
|
|
h3 {
|
|
color: #f5f5f7;
|
|
}
|
|
|
|
.input-field {
|
|
background-color: #2c2c2e;
|
|
border-color: #3a3a3c;
|
|
color: white;
|
|
}
|
|
|
|
.input-field::placeholder {
|
|
color: #8e8e93;
|
|
}
|
|
|
|
.keyboard-accessory {
|
|
background-color: #1c1c1e;
|
|
border-top-color: #3a3a3c;
|
|
}
|
|
|
|
.cancel-btn {
|
|
color: #d1d1d6;
|
|
background-color: #2c2c2e;
|
|
}
|
|
|
|
.clear-btn {
|
|
color: #d1d1d6;
|
|
background-color: #3a3a3c;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h3>键盘辅助栏示例</h3>
|
|
|
|
<div class="input-group">
|
|
<input type="text" class="input-field" id="mainInput" placeholder="点击输入内容..." />
|
|
</div>
|
|
|
|
<div class="input-group">
|
|
<input type="text" class="input-field" placeholder="另一个输入框..." />
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 键盘辅助栏 -->
|
|
<div class="keyboard-accessory" id="keyboardAccessory">
|
|
<button class="accessory-btn cancel-btn" id="cancelBtn">取消</button>
|
|
<button class="accessory-btn clear-btn" id="clearBtn">清除</button>
|
|
<button class="accessory-btn confirm-btn" id="confirmBtn">完成</button>
|
|
</div>
|
|
|
|
<script>
|
|
// 获取DOM元素
|
|
const inputs = document.querySelectorAll(".input-field");
|
|
const keyboardAccessory = document.getElementById("keyboardAccessory");
|
|
const cancelBtn = document.getElementById("cancelBtn");
|
|
const clearBtn = document.getElementById("clearBtn");
|
|
const confirmBtn = document.getElementById("confirmBtn");
|
|
let activeInput = null; // 当前聚焦的输入框
|
|
|
|
// 输入框聚焦事件:显示辅助栏
|
|
inputs.forEach((input) => {
|
|
input.addEventListener("focus", (e) => {
|
|
activeInput = e.target;
|
|
keyboardAccessory.classList.add("show");
|
|
});
|
|
|
|
input.addEventListener("blur", () => {
|
|
// 延迟隐藏,确保按钮点击事件能触发
|
|
setTimeout(() => {
|
|
if (!document.activeElement.classList.contains("input-field")) {
|
|
keyboardAccessory.classList.remove("show");
|
|
activeInput = null;
|
|
}
|
|
}, 200);
|
|
});
|
|
});
|
|
|
|
// 取消按钮:收起键盘
|
|
cancelBtn.addEventListener("click", () => {
|
|
if (activeInput) {
|
|
activeInput.blur();
|
|
}
|
|
});
|
|
|
|
// 清除按钮:清空当前输入框内容
|
|
clearBtn.addEventListener("click", () => {
|
|
if (activeInput) {
|
|
activeInput.value = "";
|
|
activeInput.focus(); // 保持聚焦状态
|
|
}
|
|
});
|
|
|
|
// 完成按钮:提交内容并收起键盘
|
|
confirmBtn.addEventListener("click", () => {
|
|
if (activeInput) {
|
|
const value = activeInput.value.trim();
|
|
if (value) {
|
|
alert(`输入内容:${value}`);
|
|
} else {
|
|
alert("输入框为空");
|
|
}
|
|
activeInput.blur();
|
|
}
|
|
});
|
|
|
|
// 监听窗口大小变化,确保辅助栏位置正确
|
|
window.addEventListener("resize", () => {
|
|
if (activeInput) {
|
|
keyboardAccessory.classList.add("show");
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|