Files
dsProject/dsLightRag/XingJun/move.html

363 lines
13 KiB
HTML
Raw Normal View History

2025-09-09 07:33:51 +08:00
<!DOCTYPE html>
2025-09-09 07:46:47 +08:00
<html lang="en">
2025-09-09 07:33:51 +08:00
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
2025-09-09 07:46:47 +08:00
<title>箭头动画</title>
2025-09-09 07:33:51 +08:00
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
display: flex;
background-color: #f0f0f0;
2025-09-09 14:07:51 +08:00
overflow: hidden;
2025-09-09 07:33:51 +08:00
}
2025-09-09 07:46:47 +08:00
2025-09-09 07:33:51 +08:00
.background-container {
position: relative;
2025-09-09 14:07:51 +08:00
flex: 1;
2025-09-09 07:33:51 +08:00
height: 100vh;
2025-09-09 07:46:47 +08:00
background-image: url('D:\\dsWork\\dsProject\\dsLightRag\\XingJun\\background.png');
2025-09-09 07:33:51 +08:00
background-position: center;
background-repeat: no-repeat;
background-size: contain;
2025-09-09 07:46:47 +08:00
z-index: 1;
2025-09-09 07:33:51 +08:00
}
2025-09-09 07:46:47 +08:00
2025-09-09 14:07:51 +08:00
/* 控制面板样式 */
.control-panel {
width: 200px;
background-color: #fff;
border-left: 1px solid #ccc;
padding: 20px;
height: 100vh;
box-sizing: border-box;
z-index: 2;
overflow-y: auto;
}
.control-section {
margin-bottom: 30px;
}
.control-section h3 {
margin-top: 0;
padding-bottom: 10px;
border-bottom: 1px solid #eee;
}
/* 图片选择按钮样式 */
.image-selector {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
margin-top: 15px;
}
.image-btn {
width: 70px;
height: 70px;
border: 2px solid #ddd;
border-radius: 4px;
cursor: pointer;
overflow: hidden;
transition: all 0.2s;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
.image-btn:hover {
border-color: #4CAF50;
transform: scale(1.05);
}
.image-btn:active {
transform: scale(0.98);
}
/* 箭头图片样式 */
.arrow-image {
position: absolute;
width: 155px;
height: 173px;
background-size: contain;
background-repeat: no-repeat;
z-index: 2;
cursor: move;
}
2025-09-09 14:09:02 +08:00
/* 保存按钮样式 */
.save-btn {
width: 100%;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin-top: 20px;
}
.save-btn:hover {
background-color: #45a049;
}
2025-09-09 14:16:09 +08:00
/* 文字说明样式 */
.text-label {
position: absolute;
padding: 5px 10px;
background-color: rgba(255, 255, 255, 0.8);
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
cursor: move;
z-index: 3;
max-width: 200px;
word-wrap: break-word;
}
/* 新增按钮样式 */
.text-btn {
width: 100%;
padding: 10px;
background-color: #2196F3;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
margin-top: 10px;
}
.text-btn:hover {
background-color: #0b7dda;
}
2025-09-09 07:33:51 +08:00
</style>
</head>
<body>
<div class="background-container">
2025-09-09 14:07:51 +08:00
</div>
<!-- 右侧控制面板 -->
<div class="control-panel">
<div class="control-section">
<h3>增加箭头</h3>
<div class="image-selector">
<div class="image-btn" data-image="./1.png" style="background-image: url('./1.png');"></div>
<div class="image-btn" data-image="./2.png" style="background-image: url('./2.png');"></div>
<div class="image-btn" data-image="./3.png" style="background-image: url('./3.png');"></div>
<div class="image-btn" data-image="./4.png" style="background-image: url('./4.png');"></div>
<div class="image-btn" data-image="./5.png" style="background-image: url('./5.png');"></div>
</div>
2025-09-09 14:09:02 +08:00
<!-- 新增保存按钮 -->
2025-09-09 14:16:09 +08:00
<!-- 新增文字说明按钮 -->
<button class="text-btn" id="addTextBtn">添加文字说明</button>
2025-09-09 14:09:02 +08:00
<button class="save-btn" id="savePositionsBtn">保存位置</button>
2025-09-09 07:39:54 +08:00
</div>
2025-09-09 07:33:51 +08:00
</div>
2025-09-09 14:07:51 +08:00
<script>
// 获取DOM元素
const backgroundContainer = document.querySelector('.background-container');
const imageButtons = document.querySelectorAll('.image-btn');
2025-09-09 14:09:02 +08:00
const saveBtn = document.getElementById('savePositionsBtn');
2025-09-09 14:16:09 +08:00
const addTextBtn = document.getElementById('addTextBtn'); // 新增文字按钮
2025-09-09 14:09:02 +08:00
// 页面加载时从localStorage读取并恢复箭头位置
document.addEventListener('DOMContentLoaded', () => {
2025-09-09 14:16:09 +08:00
loadAllElements(); // 重命名函数,支持多种元素
2025-09-09 14:09:02 +08:00
});
2025-09-09 14:07:51 +08:00
// 为每个图片按钮添加点击事件
imageButtons.forEach(button => {
button.addEventListener('click', () => {
const imagePath = button.getAttribute('data-image');
addImageToContainer(imagePath);
});
});
2025-09-09 14:16:09 +08:00
// 新增:文字按钮点击事件
addTextBtn.addEventListener('click', () => {
const textContent = prompt('请输入文字说明内容:');
if (textContent && textContent.trim() !== '') {
addTextToContainer(textContent);
}
});
2025-09-09 14:09:02 +08:00
// 保存按钮点击事件
2025-09-09 14:16:09 +08:00
saveBtn.addEventListener('click', saveAllElements);
2025-09-09 14:09:02 +08:00
2025-09-09 14:07:51 +08:00
// 添加图片到主区域
2025-09-09 14:09:02 +08:00
function addImageToContainer(imagePath, position = null) {
2025-09-09 14:07:51 +08:00
// 创建新的图片元素
const newImage = document.createElement('div');
newImage.className = 'arrow-image';
newImage.style.backgroundImage = `url('${imagePath}')`;
2025-09-09 14:09:02 +08:00
// 设置位置
if (position) {
// 从保存的数据加载位置
newImage.style.left = position.left;
newImage.style.top = position.top;
} else {
// 随机位置
const containerRect = backgroundContainer.getBoundingClientRect();
const x = Math.random() * (containerRect.width - 155);
const y = Math.random() * (containerRect.height - 173);
newImage.style.left = `${x}px`;
newImage.style.top = `${y}px`;
}
2025-09-09 14:07:51 +08:00
// 添加到容器
backgroundContainer.appendChild(newImage);
2025-09-09 14:09:02 +08:00
// 添加拖拽功能
2025-09-09 14:07:51 +08:00
makeElementDraggable(newImage);
}
2025-09-09 14:09:02 +08:00
// 保存所有箭头位置到localStorage
2025-09-09 14:16:09 +08:00
// 修改:保存所有元素(箭头和文字)
function saveAllElements() {
const elements = [];
// 收集箭头元素
document.querySelectorAll('.arrow-image').forEach(arrow => {
elements.push({
type: 'arrow',
imagePath: arrow.style.backgroundImage.match(/url\('?"?(.*?)'?"?\)/)[1],
left: arrow.style.left,
top: arrow.style.top
});
});
// 新增:收集文字元素
document.querySelectorAll('.text-label').forEach(text => {
elements.push({
type: 'text',
content: text.textContent,
left: text.style.left,
top: text.style.top
});
});
2025-09-09 14:09:02 +08:00
2025-09-09 14:16:09 +08:00
localStorage.setItem('mapElements', JSON.stringify(elements));
alert('所有元素位置已保存!');
2025-09-09 14:09:02 +08:00
}
2025-09-09 14:16:09 +08:00
// 修改:加载所有元素(箭头和文字)
function loadAllElements() {
const savedData = localStorage.getItem('mapElements');
2025-09-09 14:09:02 +08:00
if (savedData) {
try {
2025-09-09 14:16:09 +08:00
const elements = JSON.parse(savedData);
elements.forEach(element => {
if (element.type === 'arrow') {
addImageToContainer(element.imagePath, {
left: element.left,
top: element.top
});
} else if (element.type === 'text') {
addTextToContainer(element.content, {
left: element.left,
top: element.top
});
}
2025-09-09 14:09:02 +08:00
});
} catch (e) {
2025-09-09 14:16:09 +08:00
console.error('加载元素失败:', e);
localStorage.removeItem('mapElements');
2025-09-09 14:09:02 +08:00
}
}
}
2025-09-09 14:16:09 +08:00
// 新增:添加文字到主区域
function addTextToContainer(text, position = null) {
// 创建文字元素
const textElement = document.createElement('div');
textElement.className = 'text-label';
textElement.textContent = text;
textElement.dataset.type = 'text'; // 标记元素类型
// 设置位置
if (position) {
textElement.style.left = position.left;
textElement.style.top = position.top;
} else {
// 默认位置:中央偏上
textElement.style.left = '50%';
textElement.style.top = '30%';
textElement.style.transform = 'translate(-50%, -50%)';
}
// 添加到容器
backgroundContainer.appendChild(textElement);
// 添加拖拽功能
makeElementDraggable(textElement);
}
2025-09-09 14:07:51 +08:00
// 使元素可拖拽
function makeElementDraggable(element) {
let isDragging = false;
let offsetX, offsetY;
2025-09-09 14:16:09 +08:00
// 修改事件绑定将mousemove和mouseup绑定到document
2025-09-09 14:07:51 +08:00
element.addEventListener('mousedown', startDrag);
2025-09-09 14:16:09 +08:00
2025-09-09 14:07:51 +08:00
function startDrag(e) {
isDragging = true;
const rect = element.getBoundingClientRect();
2025-09-09 14:16:09 +08:00
const containerRect = backgroundContainer.getBoundingClientRect();
// 计算相对于容器的偏移量
2025-09-09 14:07:51 +08:00
offsetX = e.clientX - rect.left;
offsetY = e.clientY - rect.top;
2025-09-09 14:16:09 +08:00
// 移除transform影响
element.style.transform = 'none';
2025-09-09 14:07:51 +08:00
element.style.zIndex = '10';
2025-09-09 14:16:09 +08:00
// 添加document事件监听
document.addEventListener('mousemove', drag);
document.addEventListener('mouseup', endDrag);
document.addEventListener('mouseleave', endDrag);
2025-09-09 14:07:51 +08:00
}
2025-09-09 14:16:09 +08:00
2025-09-09 14:07:51 +08:00
function drag(e) {
if (!isDragging) return;
e.preventDefault();
2025-09-09 14:16:09 +08:00
// 计算相对于容器的位置
const containerRect = backgroundContainer.getBoundingClientRect();
const x = e.clientX - containerRect.left - offsetX;
const y = e.clientY - containerRect.top - offsetY;
// 限制在容器内
const maxX = containerRect.width - element.offsetWidth;
const maxY = containerRect.height - element.offsetHeight;
const boundedX = Math.max(0, Math.min(x, maxX));
const boundedY = Math.max(0, Math.min(y, maxY));
element.style.left = `${boundedX}px`;
element.style.top = `${boundedY}px`;
2025-09-09 14:07:51 +08:00
}
2025-09-09 14:16:09 +08:00
2025-09-09 14:07:51 +08:00
function endDrag() {
isDragging = false;
element.style.zIndex = '2';
2025-09-09 14:16:09 +08:00
// 移除document事件监听
document.removeEventListener('mousemove', drag);
document.removeEventListener('mouseup', endDrag);
document.removeEventListener('mouseleave', endDrag);
2025-09-09 14:07:51 +08:00
}
}
</script>
2025-09-09 07:33:51 +08:00
</body>
2025-09-09 13:47:24 +08:00
</html>