no message
This commit is contained in:
234
contenteditable-accessory.html
Normal file
234
contenteditable-accessory.html
Normal file
@@ -0,0 +1,234 @@
|
||||
<!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>Contenteditable 键盘辅助栏</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;
|
||||
}
|
||||
|
||||
.editable-group {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
[contenteditable="true"] {
|
||||
width: 100%;
|
||||
min-height: 50px;
|
||||
padding: 14px 16px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 12px;
|
||||
font-size: 16px;
|
||||
background-color: white;
|
||||
transition: border-color 0.2s;
|
||||
outline: none;
|
||||
}
|
||||
|
||||
[contenteditable="true"]:focus {
|
||||
border-color: #007aff;
|
||||
box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.2);
|
||||
}
|
||||
|
||||
[contenteditable="true"]:empty:before {
|
||||
content: attr(data-placeholder);
|
||||
color: #999;
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
/* 键盘辅助栏样式 */
|
||||
.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;
|
||||
}
|
||||
|
||||
[contenteditable="true"] {
|
||||
background-color: #2c2c2e;
|
||||
border-color: #3a3a3c;
|
||||
color: white;
|
||||
}
|
||||
|
||||
[contenteditable="true"]:empty:before {
|
||||
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>Contenteditable 键盘辅助栏</h3>
|
||||
|
||||
<div class="editable-group">
|
||||
<div contenteditable="true" id="mainEditable" data-placeholder="点击输入内容..."></div>
|
||||
</div>
|
||||
|
||||
<div class="editable-group">
|
||||
<div contenteditable="true" data-placeholder="另一个可编辑区域..."></div>
|
||||
</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 editables = document.querySelectorAll('[contenteditable="true"]');
|
||||
const keyboardAccessory = document.getElementById("keyboardAccessory");
|
||||
const cancelBtn = document.getElementById("cancelBtn");
|
||||
const clearBtn = document.getElementById("clearBtn");
|
||||
const confirmBtn = document.getElementById("confirmBtn");
|
||||
let activeEditable = null; // 当前聚焦的可编辑元素
|
||||
|
||||
// 可编辑元素聚焦事件:显示辅助栏
|
||||
editables.forEach((editable) => {
|
||||
console.log("editable", editable);
|
||||
|
||||
editable.addEventListener("focus", (e) => {
|
||||
activeEditable = e.target;
|
||||
keyboardAccessory.classList.add("show");
|
||||
});
|
||||
|
||||
editable.addEventListener("blur", () => {
|
||||
// 延迟隐藏,确保按钮点击事件能触发
|
||||
setTimeout(() => {
|
||||
// 检查是否还有其他可编辑元素处于聚焦状态
|
||||
const hasActive = Array.from(editables).some((el) => document.activeElement === el);
|
||||
if (!hasActive) {
|
||||
keyboardAccessory.classList.remove("show");
|
||||
activeEditable = null;
|
||||
}
|
||||
}, 200);
|
||||
});
|
||||
});
|
||||
|
||||
// 取消按钮:取消聚焦(收起键盘)
|
||||
cancelBtn.addEventListener("click", () => {
|
||||
if (activeEditable) {
|
||||
activeEditable.blur();
|
||||
}
|
||||
});
|
||||
|
||||
// 清除按钮:清空当前可编辑元素内容
|
||||
clearBtn.addEventListener("click", () => {
|
||||
if (activeEditable) {
|
||||
activeEditable.textContent = "";
|
||||
activeEditable.focus(); // 保持聚焦状态
|
||||
}
|
||||
});
|
||||
|
||||
// 完成按钮:获取内容并取消聚焦
|
||||
confirmBtn.addEventListener("click", () => {
|
||||
if (activeEditable) {
|
||||
const value = activeEditable.textContent.trim();
|
||||
if (value) {
|
||||
alert(`输入内容:${value}`);
|
||||
} else {
|
||||
alert("内容为空");
|
||||
}
|
||||
activeEditable.blur();
|
||||
}
|
||||
});
|
||||
|
||||
// 监听窗口大小变化,确保辅助栏位置正确
|
||||
window.addEventListener("resize", () => {
|
||||
if (activeEditable) {
|
||||
keyboardAccessory.classList.add("show");
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user