Edit - here is a fiddle: http://jsfiddle.net/cLahpqoj/1/ and here is an example of what i'm trying to do: https://www.seventh.tv/
perhaps my math on setting door position is bad and screwing things up. noticed the mouse postion and door position are changing places in values. logging:
{doorX: -1066, x: 396, doorY: -1367, y: 164}
{doorX: 396, x: -1065, doorY: 164, y: -1367}
from:
let mouseX = e.offsetX;
let mouseY = e.offsetY;
let doorX = currentDoorRect.x;
let doorY = currentDoorRect.y;
let x = doorX;
let y = doorY;
x = -( x + (mouseX) );
y = -( y + (mouseY) );
let transform = 'translate(' + x + 'px,' + y + 'px)';
currentDoor.style.transform = transform;
console.log( {
doorX,
x,
doorY,
y
});
I'm trying to move a div with the transform:translate(x,y) css property based on the current div position and the mouse position.
I'm logging the x and y values passed to the transform change and noticing for every value change, there is one proceeding one with something like x: -1, y:0 and then immediately back to what the value should be (i.e x:-563, y:424)
I'm using Vue.js and the function is :
<template>
<div ref="test-container" id="test-container" class="h-full relative"
@mousedown.prevent="disableMiddleClickScroll"
@mousemove="moveDoors"
@wheel="disableWheelScroll">
<div
:ref="`door-${index}`"
v-for="index in numDoors" :key="index"
class="door bg-gray-300 p-8 text-center rounded flex-none">
Door
</div>
</div>
</template>
...
moveDoors(e) {
let mouseX = e.offsetX;
let mouseY = e.offsetY;
let currentDoor = this.$refs[`door-1`];
let currentDoorRect = currentDoor.getBoundingClientRect();
let doorX = currentDoorRect.x;
let doorY = currentDoorRect.y;
let x = -( doorX + (mouseX) );
let y = -( doorY + (mouseY) );
console.log( {
x,
y
});
let transform = 'translate(' + x + 'px,' + y + 'px)';
currentDoor.style.transform = transform;
},
the event is called like this:
example of console log:
{x: 1, y: -0}
{x: -547, y: -426}
{x: 1, y: -0}
{x: -545, y: -426}
{x: -0, y: 1}
{x: -544, y: -425}
{x: -0, y: 1}
{x: -546, y: -424}
{x: -1, y: -0}
The container css is:
#test-container {
position: fixed;
left: 0%;
top: 0%;
right: 0%;
bottom: 0%;
}
Please let me know if there's not enough information to debug. I think this is all that is relevant.