feat: add new draggable elements
This commit is contained in:
parent
3a867c169a
commit
22ad9eb6cc
1 changed files with 76 additions and 65 deletions
|
|
@ -21,6 +21,7 @@ tags:
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
<button id="addFrame">Add</button>
|
||||||
<div id="space"></div>
|
<div id="space"></div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
@ -28,20 +29,22 @@ tags:
|
||||||
<script>
|
<script>
|
||||||
// Get the space element
|
// Get the space element
|
||||||
const space = document.getElementById('space');
|
const space = document.getElementById('space');
|
||||||
|
const addFrameButton = document.getElementById('addFrame');
|
||||||
|
|
||||||
// Create a new div element
|
// Function to create a new div element
|
||||||
const newDiv = document.createElement('div');
|
function createNewDiv() {
|
||||||
newDiv.style.position = 'absolute';
|
const newDiv = document.createElement('div');
|
||||||
newDiv.style.width = '100px';
|
newDiv.style.position = 'absolute';
|
||||||
newDiv.style.height = '100px';
|
newDiv.style.width = '100px';
|
||||||
newDiv.style.background = 'cyan';
|
newDiv.style.height = '100px';
|
||||||
newDiv.style.cursor = 'move';
|
newDiv.style.background = 'red';
|
||||||
|
newDiv.style.cursor = 'move';
|
||||||
|
|
||||||
// Add event listeners for dragging
|
// Add event listeners for dragging
|
||||||
let isDown = false;
|
let isDown = false;
|
||||||
let offset = [0, 0];
|
let offset = [0, 0];
|
||||||
|
|
||||||
newDiv.addEventListener('mousedown', (event) => {
|
newDiv.addEventListener('mousedown', (event) => {
|
||||||
if (event.button === 0) { // Left mouse button
|
if (event.button === 0) { // Left mouse button
|
||||||
isDown = true;
|
isDown = true;
|
||||||
offset = [
|
offset = [
|
||||||
|
|
@ -49,13 +52,13 @@ newDiv.addEventListener('mousedown', (event) => {
|
||||||
newDiv.offsetTop - event.clientY
|
newDiv.offsetTop - event.clientY
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener('mouseup', () => {
|
document.addEventListener('mouseup', () => {
|
||||||
isDown = false;
|
isDown = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener('mousemove', (event) => {
|
document.addEventListener('mousemove', (event) => {
|
||||||
if (isDown) {
|
if (isDown) {
|
||||||
const maxX = window.innerWidth - newDiv.offsetWidth;
|
const maxX = window.innerWidth - newDiv.offsetWidth;
|
||||||
const maxY = window.innerHeight - newDiv.offsetHeight;
|
const maxY = window.innerHeight - newDiv.offsetHeight;
|
||||||
|
|
@ -71,10 +74,10 @@ document.addEventListener('mousemove', (event) => {
|
||||||
newDiv.style.left = `${x}px`;
|
newDiv.style.left = `${x}px`;
|
||||||
newDiv.style.top = `${y}px`;
|
newDiv.style.top = `${y}px`;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Save position and size
|
// Save position and size
|
||||||
const saveData = () => {
|
const saveData = () => {
|
||||||
const data = {
|
const data = {
|
||||||
x: newDiv.offsetLeft,
|
x: newDiv.offsetLeft,
|
||||||
y: newDiv.offsetTop,
|
y: newDiv.offsetTop,
|
||||||
|
|
@ -82,12 +85,12 @@ const saveData = () => {
|
||||||
height: newDiv.offsetHeight
|
height: newDiv.offsetHeight
|
||||||
};
|
};
|
||||||
// Save data to local storage or a database
|
// Save data to local storage or a database
|
||||||
localStorage.setItem('divData', JSON.stringify(data));
|
localStorage.setItem(`divData${newDiv.id}`, JSON.stringify(data));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Load saved data
|
// Load saved data
|
||||||
const loadData = () => {
|
const loadData = () => {
|
||||||
const data = localStorage.getItem('divData');
|
const data = localStorage.getItem(`divData${newDiv.id}`);
|
||||||
if (data) {
|
if (data) {
|
||||||
const parsedData = JSON.parse(data);
|
const parsedData = JSON.parse(data);
|
||||||
newDiv.style.left = `${parsedData.x}px`;
|
newDiv.style.left = `${parsedData.x}px`;
|
||||||
|
|
@ -95,15 +98,23 @@ const loadData = () => {
|
||||||
newDiv.style.width = `${parsedData.width}px`;
|
newDiv.style.width = `${parsedData.width}px`;
|
||||||
newDiv.style.height = `${parsedData.height}px`;
|
newDiv.style.height = `${parsedData.height}px`;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add the new div to the space
|
// Add the new div to the space
|
||||||
space.appendChild(newDiv);
|
newDiv.id = `newDiv${space.children.length}`;
|
||||||
|
space.appendChild(newDiv);
|
||||||
|
|
||||||
// Call the save function when the user stops dragging
|
// Call the save function when the user stops dragging
|
||||||
document.addEventListener('mouseup', saveData);
|
document.addEventListener('mouseup', saveData);
|
||||||
|
|
||||||
// Load saved data on page load
|
// Load saved data on page load
|
||||||
loadData();
|
loadData();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add event listener to the add space button
|
||||||
|
addFrameButton.addEventListener('click', createNewDiv);
|
||||||
|
|
||||||
|
// Create the first new div element
|
||||||
|
createNewDiv();
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue