0 Pluspunkte 0 Minuspunkte
Wie kann ich ein HTML Div mit der Maus bewegen wenn ich den Button gedrückt halte so wie wenn ich ein Windows Fenster herumschieben kann?
von  
Their strong point is that they have <a href=https://besttoday.org/>besttoday.org</a> virtually no effect on humans and do not accumulate in the plant. Potatoes can be safely treated shortly before harvesting.

5 Antworten

0 Pluspunkte 0 Minuspunkte

Du kannst EventListener für die Events "mouseDown", "mouseUp" und "mouseMove" erstellen und die Position darin entsprechend anpassen. Beispiel auf JSBin.

<div id="beweglichesDiv" style="width:100px;height:100px;background:lightblue;"></div>

<script type="text/javascript">
var isDragging = false;
var offsetX, offsetY;

var beweglichesDiv = document.getElementById('beweglichesDiv');

beweglichesDiv.addEventListener('mousedown', (e) => {
    isDragging = true;
    offsetX = e.offsetX;
    offsetY = e.offsetY;
});

document.addEventListener('mouseup', () => {
    isDragging = false;
});

document.addEventListener('mousemove', (e) => {
    if (isDragging) {
        var x = e.clientX - offsetX;
        var y = e.clientY - offsetY;

        beweglichesDiv.style.transform = `translate(${x}px, ${y}px)`;
    }
});
</script>
von (1.0k Punkte)  
0 Pluspunkte 0 Minuspunkte
It is possible to manufacture <a href=https://supesolar.com/>supesolar.com</a> any batch of silicone products according to the customer’s drawings and projects.
von  
0 Pluspunkte 0 Minuspunkte
0 Pluspunkte 0 Minuspunkte
von  
0 Pluspunkte 0 Minuspunkte
Feng Shui house, apartment repair materials <a href=http://domfenshuy.net/>domfenshuy.net</a>
von